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>
#define ll long long
#define pii pair<int,int>
using namespace std;
int h, w;
const int maxn = 4000;
char a[maxn + 3][maxn + 3];
int d[maxn + 3][maxn + 3];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
bool inside(int i, int j)
{
    return (i >= 1 && i <= h && j >= 1 && j <= w && a[i][j] != '.');
}
int main()
{
    //freopen("TRACK.INP", "r", stdin);
    //freopen("TRACK.OUT", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> h >> w;
    for(int i = 1; i <= h; i++)
    {
        for(int j = 1; j <= w; j++)
        {
            cin >> a[i][j];
        }
    }
    d[1][1] = 1;
    deque<pair<int,int>>dq;
    dq.push_front({1, 1});
    int ans = 1;
    while(!dq.empty())
    {
        pii p = dq.front();
        dq.pop_front();
        ans = max(ans, d[p.first][p.second]);
        for(int i = 0; i < 4; ++i)
        {
            int i1 = p.first + dx[i];
            int j1 = p.second + dy[i];
            if(inside(i1, j1) && d[i1][j1] == 0)
            {
                if(a[i1][j1] == a[p.first][p.second])
                {
                    d[i1][j1] = d[p.first][p.second];
                    dq.push_front({i1, j1});
                }
                else{
                    d[i1][j1] = d[p.first][p.second] + 1;
                    dq.push_back({i1, j1});
                }
            }
        }
    }
    cout << ans;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |