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;
#define int long long
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define pb push_back
#define F first
#define S second
#define endl "\n"
const int INF = 1e18+10;
const int MOD = 1e9+7;
int n, m, d[4040][4040];
string t[4040];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> m;
    for(int i = 0; i < n; i++) cin >> t[i];
    deque<pair<int, int>> q;
    d[0][0] = 1;
    q.push_front({0, 0});
    while(!q.empty()) {
        int x = q.front().F, y = q.front().S; q.pop_front();
        for(int i = 0; i < 4; i++) {
            int xx = x + dx[i];
            int yy = y + dy[i];
            if(xx < 0 or xx >= n or yy < 0 or yy >= m) continue;
            if(t[xx][yy] == '.' or d[xx][yy] != 0) continue;
            if(t[xx][yy] == t[x][y]) {
                d[xx][yy] = d[x][y];
                q.push_front({xx, yy});
            } else {
                d[xx][yy] = d[x][y] + 1;
                q.push_back({xx, yy});
            }
        }
    }
    int resp = 0;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            resp = max(resp, d[i][j]);
        }
    }
    cout << resp << endl;
    return 0;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |