#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define F first
#define S second
const int N = 3100;
int dp[N][N][2];
char a[N][N];
void solve(){
    int n, m;
    cin>>n>>m;
    for(int i = 4; i <= n + 3; i++){
        for(int j = 4; j <= m + 3; j++){
            cin>>a[i][j];
        }
    }
    int ans = 0;
    for(int i = 4; i <= n + 3; i++){
        int f = i, s = 4;
        while(f >= 4 && m <= m + 3){
            bool ok = 0, ok1 = 0;
            if(a[f][s] == 'W' && a[f - 1][s] == 'G' && a[f - 2][s] == 'R') ok = 1;
            if(a[f][s] == 'W' && a[f][s - 1] == 'G' && a[f][s - 2] == 'R') ok1 = 1;
            dp[f][s][1] = max(dp[f + 1][s - 1][0], dp[f + 1][s - 1][1]) + ok;
		    dp[f][s][0] = max(dp[f + 3][s - 3][1], dp[f + 1][s - 1][0]) + ok1;
		    f--;
		    s++;
        }
        f++;
        s--;
        ans += max(dp[f][s][0], dp[f][s][1]);
    }
    for (int i = 5; i <= m + 3; i++) {
		int f = n + 3, s = i;
		while (f >= 4 && s <= m + 3) {
			bool ok = 0, ok1 = 0;
			if (a[f][s] == 'W' && a[f - 1][s] == 'G' && a[f - 2][s] == 'R') ok = 1;
			if (a[f][s] == 'W' && a[f][s - 1] == 'G' && a[f][s - 2] == 'R') ok1 = 1;
			dp[f][s][1] = max(dp[f + 1][s - 1][0], dp[f + 1][s - 1][1]) + ok;
			dp[f][s][0] = max(dp[f + 3][s - 3][1], dp[f + 1][s - 1][0]) + ok1;
			f--, s++;
		}
		f++, s--;
		ans += max(dp[f][s][0], dp[f][s][1]);
	}
	cout<<ans<<'\n';
}
signed main(){
    ios_base :: sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t = 1;
    //cin>>t;
    while(t--){
        solve();
    }
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |