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 ll long long
#define lld double
#define int ll
#define usaco(fname) freopen(#fname ".in","r",stdin);freopen(#fname ".out","w",stdout);
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
// const ll INF = 1e18;
const int INF = 1e9;
const int mod = 1e9+7;
const lld PI = acos(-1.0);
int di[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dj[] = {0, 0, 1, -1, 1, -1, 1, -1};
#define debug(x) cout << #x << ": " << x << endl;
#define add(a, b) a += b, a %= mod
#define mul(a, b) ((a % mod) * (b % mod)) % mod
#define all(x) x.begin(),x.end()
void solve() {
  int n,m,d,s;cin>>n>>m>>d>>s;
  vector<vector<int>> pref(n, vector<int>(m));
  vector<pair<int,int>> a;
  for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) {
    char c;cin>>c;
    if (c == 'M') a.push_back({i, j}); 
    else if (c == 'S') pref[i][j]++;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (i-1 >= 0) pref[i][j] += pref[i-1][j];
      if (j-1 >= 0) pref[i][j] += pref[i][j-1];
      if (i-1 >= 0 && j-1 >= 0) pref[i][j] -= pref[i-1][j-1];
    }
  }
  function<int(int, int, int, int)> get = [&](int i1, int j1, int i2, int j2) {
    i1 = max(i1, 0ll);
    j1 = max(j1, 0ll);
    i2 = min(i2, n-1);
    j2 = min(j2, m-1);
    int sum = 0;
    sum += pref[i2][j2];
    if (j1-1 >= 0) sum -= pref[i2][j1-1];
    if (i1-1 >= 0) sum -= pref[i1-1][j2];
    if (i1-1 >= 0 && j1-1 >= 0) sum += pref[i1-1][j1-1];
    return sum;
  };
  int ans = 0;
  for (auto [i, j] : a) if (get(i-d, j-d, i+d, j+d) >= s) ans++;
  cout << ans << endl;
}
int32_t main() {
  ios_base::sync_with_stdio(0);cin.tie(0);
  int t=1;
  while(t--) solve();
  return 0;
}
| # | 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... |