#include <bits/stdc++.h>
#define pb push_back
using namespace std;
int main() {
    int R, C, D, K;
    cin >> R >> C >> D >> K;
    vector<vector<char>> grid(R, vector<char>(C));
    vector<pair<int, int>> splinter;
    for (int i = 0; i < R; i ++) {
        for (int j = 0; j < C; j ++) {
            char temp;
            cin >> temp;
            grid[i][j] = temp;
            if (temp == 'S') {
                splinter.pb({i, j});
            }
        }
    }
    vector<vector<int>> mushroom(R, vector<int>(C));
    int harvestable_mushroom = 0;
    for (int z = 0; z < splinter.size(); z ++) {
        int x = splinter[z].first, y = splinter[z].second;
        int first_row = max(0, x-D), last_row = min(R-1, x+D);
        int first_col = max(0, y-D), last_col = min(C-1, y+D);
        for (int i = first_row; i <= last_row; i ++) {
            for (int j = first_col; j <= last_col; j ++) {
                if (grid[i][j] == 'M') {
                    mushroom[i][j]++;
                }
                if (mushroom[i][j] >= K) {
                    grid[i][j] == '.';
                    harvestable_mushroom ++;
                }
            }
        }
    }
    cout << harvestable_mushroom << endl;
}
// 5 5 1 1
// ....M
// .M...
// ..S..
// .S...
// ...M.
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |