시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율
1 초 512 MB 2 1 1 100.000%

문제

Lim Li the Crab is running a mushroom plantation in her backyard. Her mushroom plantation can be modelled as a grid of R rows and C columns, and each grid square of her mushroom plantation can either be empty, contain a mushroom, or contain a sprinkler. For example, her mushroom plantation could look like this:

Figure 1: A mushroom farm with R = 5 and C = 5.

The distance between a sprinkler and a mushroom is defined as the maximum of their separation in the two axes. In other words, if the mushroom is located at row Xm and column Ym while the sprinkler is located at row Xs and column Ys, their distance will be max(|Xs − Xm|, |Ys − Ym|). Sprinklers only have a limited range, so a sprinkler can only water a mushroom if the distance between them is at most D. For example, if D = 1, the areas reachable by the two sprinklers will be:

Figure 2: Diagram showing the range of the sprinklers.

Mushrooms can only grow and be harvested if enough sprinklers are watering it. Specifically, a mushroom will be harvestable if at least K sprinklers are watering it. Count the number of harvestable mushrooms Lim Li can collect in her plantation.

입력

The first line of input will contain four integers: R, the number of rows, C, the number of columns, D, the maximum distance between a sprinkler and a watered mushroom, and K, the minimum number of sprinklers required for a mushroom to be harvestable.

The next R lines of input will contain C characters each, containing a grid representing the mushroom plantation. Each character will represent the contents of a particular grid square, in the following way:

  • ‘.’ represents an empty grid square,
  • ‘M’ represents a grid square containing a mushroom,
  • ‘S’ represents a grid square containing a sprinkler.

출력

The output should contain one line with one integer, the maximum number of mushrooms Lim Li can harvest.

제한

  • 2 ≤ RC ≤ 500000,
  • 1 ≤ D ≤ max(R, C),
  • 1 ≤ K ≤ RC,
  • there will be at least one mushroom,
  • there will be at least one sprinkler.

예제 입력 1

5 5 1 1
....M
.M...
..S..
.S...
...M.

예제 출력 1

1

Since the range of each sprinkler is only 1, meaning sprinklers can only reach adjacent squares, only the mushroom at (2, 2) is watered.

예제 입력 2

4 4 4 1
....
.M..
..MM
...S

예제 출력 2

3

Since the range of each sprinkler is 4, the lone sprinkler on the plantation can water all the mushrooms.

예제 입력 3

1 8 5 2
SM..MM.S

예제 출력 3

2

Each mushroom requires both sprinklers to be within range, since K = 2. Only two mushrooms satisfy this condition, the second and third mushrooms from the left.

예제 입력 4

5 5 2 2
....M
.M...
..S..
.S...
...M.

예제 출력 4

2

Since the range of each sprinkler is 2, the mushroom at (2, 2) and the mushroom at (5, 4) can be watered by both sprinklers.