This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std; 
#define int long long 
#define ld long double
#define fi first 
#define se second 
#define pb push_back
#define bupol __builtin_popcount
signed main(){
    ios_base::sync_with_stdio(false); 
    cin.tie(NULL);
    int r, c, d, s; cin >> r >> c >> d >> s;
    char grid[r+5][c+5]; 
    int tmp[r+5][c+5]; 
    memset(tmp, 0, sizeof tmp); 
    int add[c+5]; 
    memset(add,0,sizeof add); 
    vector<pair<int,int>>v; 
    for(int i = 1; i <= r; i++){
        string s; cin >> s; 
        for(int j = 0; j < s.length(); j++){
            grid[i][j+1] = s[j]; 
            if(s[j] == 'S'){
                tmp[max(1ll,i-d)][max(1ll,j+1ll-d)]++; 
                tmp[max(1ll,i-d)][min(c+1,j+2+d)]--; 
                tmp[min(r+1,i+d+1ll)][max(1ll,j+1ll-d)]--;
                tmp[min(r+1,i+d+1ll)][min(c+1,j+2+d)]++; 
            }
        }
    }
    int ans = 0; 
    for(int i = 1; i <= r; i++){
        int cnt = 0; 
        for(int j = 1; j <= c; j++){
            //cout << "here >> " << i << " " << j << " " << tmp[i][j] << endl; 
            add[j] += tmp[i][j]; 
            cnt += add[j]; 
            if(grid[i][j] == 'M' && cnt >= s){
                ans++; 
            }
        }
    }
    cout << ans << endl; 
}   
Compilation message (stderr)
mushrooms.cpp: In function 'int main()':
mushrooms.cpp:21:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 |         for(int j = 0; j < s.length(); j++){
      |                        ~~^~~~~~~~~~~~| # | 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... |