#include <bits/stdc++.h>
#define d1(x)                cout << #x << " : " << x << endl << flush
#define d2(x, y)             cout << #x << " : " << x << "   " << #y << " : " << y << endl << flush
#define d3(x, y, z)          cout << #x << " : " << x << "   " << #y << " : " << y << "   " << #z << " : " << z << endl << flush
#define d4(x, y, z, a)       cout << #x << " : " << x << "   " << #y << " : " << y << "   " << #z << " : " << z << "    "<< #a << " : " << a << endl << flush
#define ld                   long double
#define ll                   long long
#define pb                   push_back
#define arr(x)               array <int, x>
#define endl                 '\n'
// #define int                  long long
using namespace std;
const int INF = 1e9 + 24 + (33 / 10); // 33 ---> 34
const int MAXN = 4e3 + 24 + (33 / 10); // 33 ---> 34
int dx[4] = {1, -1, -0, -0};
int dy[4] = {-0, -0, 1, -1};
int n, m;
vector <arr(2)> v;
bool vis[MAXN][MAXN];
int a[MAXN][MAXN];
int ans = 0;
void dfs(int x, int y){
    vis[x][y] = 1;
    v.pb({x, y});
    for(int i = 0; i < 4; i++){
        int nx = x + dx[i];
        int ny = y + dy[i];
        if(a[nx][ny] == a[x][y] && !vis[nx][ny])
            dfs(nx, ny);
    }
}
void solve(vector <arr(2)> vec){
    if(vec.empty())
        return;
    
    v.clear();
    ans++;
    for(auto [x, y] : vec){
        for(int i = 0; i < 4; i++){
            int nx = x + dx[i];
            int ny = y + dy[i];
            if(a[nx][ny] && !vis[nx][ny])
                dfs(nx, ny);
        }
    }
    solve(v);
}
signed main(){
	
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n >> m;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            char eli;
            cin >> eli;
            
            if(eli == 'F')
                a[i][j] = 2;
            
            else if(eli == 'R')
                a[i][j] = 1;
        }
    }
    dfs(1, 1);
    solve(v);
    cout << ans << endl;
}
// Man hamooonam ke ye rooz...
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |