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>
#define ll long long
#define pb push_back
#define pp pop_back
#define mp make_pair
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define pll pair<ll,ll> 
#define pii pair<int,int> 
#define vl vector<ll> 
#define nikah ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const ll maxn = 5e5+7, modn1 = 1e9+7, modn2 = 998244353;
using namespace std;
// kalo kamu ngga emas nanti nadya merasa bersalah, ayo san demi nadya!!!
// Kalo emang soalnya susah, pindah soal dulu 
// Jangan sampe diem, pikirin sesuatu, pasti bisa!!!
// Cobain semua approach, jangan ngestuck di satu pemikiran
// Jangan overthinking, jangan panik, tenangin pikiranmu
ll t,r,c,d,s;
vector<pll>v;
void solve() {
  cin>>r>>c>>d>>s;
  ll a[r+2][c+2];
  memset(a,0,sizeof a);
  for (ll i=1; i<=r; i++) {
    for (ll j=1; j<=c; j++) {
      char x; cin>>x;
      if (x == 'M') {
        v.pb({i,j});
      } else if (x == 'S') {
        a[i][j]++;
      }
      a[i][j] += a[i-1][j] + a[i][j-1] - a[i-1][j-1];
    }
  }
  ll ans = 0;
  for (auto p : v) {
    ll x1 = max((ll)1, p.fi - d);
    ll y1 = max((ll)1, p.se - d);
    ll x2 = min(r, p.fi + d);
    ll y2 = min(c, p.se + d);
    ll o = a[x2][y2] - a[x2][y1-1] - a[x1-1][y2] + a[x1-1][y1-1];
  //  cout<<o<<endl;
    if (a[x2][y2] - a[x2][y1-1] - a[x1-1][y2] + a[x1-1][y1-1] >= s) ans++;
  }
  cout<<ans;
}
int main () {
  nikah
  t = 1;
//  cin>>t;
  while (t--) {
    solve();
  //  cout<<endl;
  }
}
Compilation message (stderr)
mushrooms.cpp: In function 'void solve()':
mushrooms.cpp:47:8: warning: unused variable 'o' [-Wunused-variable]
   47 |     ll o = a[x2][y2] - a[x2][y1-1] - a[x1-1][y2] + a[x1-1][y1-1];
      |        ^| # | 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... |