#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+1, vector<int>(C+1, 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);
        
        if (R < C) {
            for (int i = first_row; i <= last_row; i ++) {
                mushroom[i][first_col] ++;
                mushroom[i][min(last_col+1, C)] --;
            }
        }
        else {
            for (int i = first_col; i <= last_col; i ++) {
                mushroom[first_row][i] ++;
                mushroom[min(last_row+1, R)][i] --;
            }
        }
    }
    if (R < C) {
        for (int i = 0; i < R; i ++) {
            for (int j = 1; j < C; j ++) {
                mushroom[i][j] += mushroom[i][j-1];
            }
        }
    }
    else {
        for (int i = 0; i < C; i ++) {
            for (int j = 1; j < R; j ++) {
                mushroom[j][i] += mushroom[j-1][i];
            }
        }
    }
    int harvestable_mushroom = 0;
    for (int i = 0; i < R; i ++) {
        for (int j = 0; j < C; j ++) {
            if (grid[i][j] == 'M' && mushroom[i][j] >= K) {
                harvestable_mushroom ++;
            }
        }
    }
    cout << harvestable_mushroom;
}
| # | 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... |