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;
using ll = long long;
int n, m, ans;
bool tr1, tr2;
vector<string> a;
vector<vector<bool>> vis;
bool chk(int x, int y) {
    return 0 <= min(x, y) && x < n && y < m && !vis[x][y];
}
void dfs(int x, int y) {
    if (vis[x][y]) return;
    vis[x][y] = true;
    if (a[x][y] == 'R') {
        ans += tr1; tr1 = false;
    }
    else {
        ans += tr2; tr2 = false;
    }
    if (chk(x - 1, y)) dfs(x - 1, y);
    if (chk(x + 1, y)) dfs(x + 1, y);
    if (chk(x, y - 1)) dfs(x, y - 1);
    if (chk(x, y + 1)) dfs(x, y + 1);
}
int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    cin >> n >> m;
    a.assign(n, "");
    for (auto &i : a) cin >> i;
    vis.assign(n, vector<bool>(m, false));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (!chk(i, j)) continue;
            tr1 = tr2 = true;
            dfs(i, j);
        }
    }
    cout << ans;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |