#include <bits/stdc++.h>
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization("unroll-loops")
// #pragma ("reroll")
#define skip continue;
#define gold ios_base::sync_with_stdio(false);cin.tie(NULL);
#define xa "\n"
#define int long long
#define all(a) a.begin(), a.end()
#define pb push_back
#define F first
#define S second
using namespace std;
const int mod = 1e9 + 7;
const int N = 2e5 + 7;
const int MAX = 1e9;
const int inf = 1e18;
int gcd(int a, int b) { if (b == 0) return a; else if(a == 0) return b; else return gcd(b, a % b); }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
void solve() {
    int n, m, d, k;
    cin >> n >> m >> d >> k;
    if(n != 1){
        char x[n + 1][m + 1];
        int cnt[n + 1][m + 1];
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                cin >> x[i][j];
                cnt[i][j] = 0;
            }
        }
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                if(x[i][j] == 'S'){
                    for(int l = max(1ll, i - d); l <= min(n, i + d); l++){
                        for(int r = max(1ll, j - d); r <= min(m, j + d); r++){
                            cnt[l][r]++;
                        }
                    }
                }
            }
        }
        int ans = 0;
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                if(x[i][j] == 'M' && cnt[i][j] >= k) ans++;
            }
        }
        cout << ans;
        return;
    }
    n = m;
    char x[n + 1];
    int cnt[n + 1];
    for(int i = 1; i <= n; i++){
        cin >> x[i];
        cnt[i] = 0;
    }
    for(int i = 1; i <= n; i++){
        if(x[i] == 'S'){
            cnt[max(1ll, i - d)]++;
            cnt[i + d + 1]--;
        }
    }
    int take = 0;
    int ans = 0;
    for(int i = 1; i <= n; i++){
        take += cnt[i];
        if(x[i] == 'M' && take >= k) ans++;
    }
    cout << ans;
}
signed main() {
    gold;
    //freopen("rmq.in", "r", stdin);
    //freopen("rmq.out", "w", stdout);
    int t = 1;
    int ti = 1;
    //cin >> t;
    while (t--) {
        //cout << "Case " << ti << ": ";
        solve();
        ti++;
    }
}
| # | 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... |