#include <bits/stdc++.h>
using namespace std;
#define int long long
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
int solve() {
    int n, m;
    cin >> n >> m;
    
    vector<string> v(n);
    for (int i = 0; i < n; i++) cin >> v[i];
    
    vector<vector<bool>> vis(n, vector<bool>(m, false));
    vis[0][0] = true;
    deque<tuple<int, int, int>> q;
    q.push_front({0, 0, 0});
    
    int ans = 0;
    while (!q.empty()) {
        auto [d, x, y] = q.front();
        q.pop_front();
        
        ans = max(ans, d);
        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i], ny = y + dy[i];
            if (nx >= 0 and nx < n and ny >= 0 and ny < m and !vis[nx][ny]) {
                if (v[nx][ny] == '.') continue;
                if (v[nx][ny] == v[x][y]) q.push_front({d, nx, ny});
                else q.push_back({d + 1, nx, ny});
                vis[nx][ny] = true;
            }
        }
    }
    return ans + 1;
}
int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    cout << solve();
    return 0;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |