#include <bits/stdc++.h>
#define TASK "ZOO"
#define int long long
#define double long double
#define fi first
#define se second
#define pb push_back
#define ii pair<int, int>
#define vi vector<int>
#define vvi vector<vi>
#define vii vector<ii>
#define reset(f, x) memset(f, x, sizeof(f))
#define all(x) x.begin(), x.end()
#define sz(x) (long long)x.size()
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define FORV(v, H) for (auto &v: H)
#define __builtin_popcount __builtin_popcountll
#define BIT(mask, i) ((mask >> i) & 1ll)
#define MASK(i) (1ll << (i))
#define ONBIT(mask, i) (mask  (1ll << (i)))
#define OFFBIT(mask, i) (mask &~ (1ll << (i)))
#define mid(l, r) ((l + r) >> 1)
#define left(id) (id << 1)
#define right(id) ((id << 1)  1)
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define UNIQUE(x) sort(all(x)); x.resize(distance(x.begin(), unique(all(x))))
#define BUG1(a) cout << a << '\n'
#define BUG2(a, b) cout << a << " " << b << '\n'
#define BUG3(a, b, c) cout << a << " " << b << " " << c << '\n'
#define BUG4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << '\n'
#define BUG5(a, b, c, d, e) cout << a << " " << b << " " << c << " " << d << " " << e << '\n'
#define BUG6(a, b, c, d, e, f) cout << a << " " << b << " " << c << " " << d << " " << e << " " << f << '\n'
using namespace std;
template <class X, class Y> bool maximize(X &A, const Y &B){
    if (A < B) return A = B, true;
    return false;
}
template <class X, class Y> bool minimize(X &A, const Y &B){
    if (A > B) return A = B, true;
    return false;
}
const int oo  = 1e18;
const int MOD = 1e9 + 7;
const int MAXN = 1e3 + 5;
const int LOG = 16;
const int base = 311;
const int BLOCK = 317;
const int dx[] = {0, 0, 1, 0, -1};
const int dy[] = {0, 1, 0, -1, 0};
int M, N;
char a[MAXN][MAXN];
bool vis[MAXN][MAXN];
int cc[MAXN][MAXN];
vvi G;
vi dist;
void DFS(int x, int y, char ch, int id){
    vis[x][y] = 1;
    cc[x][y] = id;
    FOR(k, 1, 4){
        int Dx = x + dx[k], Dy = y + dy[k];
        if (Dx < 1 || Dy < 1 || Dx > M || Dy > N) continue;
        if (!vis[Dx][Dy] && a[Dx][Dy] == ch) DFS(Dx, Dy, ch, id);
    }
}
int BFS(int S){
    int ans = 0;
    queue<int> qu;
    qu.push(S);
    dist[S] = 0;
    while (sz(qu)){
        int u = qu.front();
        qu.pop();
        FORV(v, G[u]){
            if (dist[v] > dist[u] + 1){
                dist[v] = dist[u] + 1;
                maximize(ans, dist[v]);
                qu.push(v);
            }
        }
    }
    return ans;
}
main(){
    if (fopen(TASK".inp", "r")){
        freopen(TASK".inp", "r", stdin);
        freopen(TASK".out", "w", stdout);
    }
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> M >> N;
    FOR(x, 1, M){
        FOR(y, 1, N){
            cin >> a[x][y];
        }
    }
    int id = 0;
    FOR(x, 1, M){
        FOR(y, 1, N){
            if (!vis[x][y] && a[x][y] != '*') DFS(x, y, a[x][y], ++id);
        }
    }
    G.resize(id + 1);
    FOR(x, 1, M){
        FOR(y, 1, N){
            if (a[x][y] == '*') continue;
            FOR(k, 1, 4){
                int Dx = x + dx[k], Dy = y + dy[k];
                if (a[Dx][Dy] == '*') continue;
                if (Dx < 1 || Dy < 1 || Dx > M || Dy > N || cc[x][y] == cc[Dx][Dy]) continue;
                G[cc[x][y]].push_back(cc[Dx][Dy]);
            }
        }
    }
    dist.assign(id + 1, +oo);
    cout << BFS(cc[M][N]) + 1 << '\n';
    return 0;
}
/*
     ___  __    ___  _______   ___  ___          ________  ___  ________          ___  ___  ___  ___      ___    ___
    \  \\  \ |\  \|\  ___ \ |\  \|\  \        |\   ____\|\  \|\   __  \        |\  \|\  \|\  \|\  \    |\  \  / / |
    \ \  \/  /|\ \  \ \   __/|\ \  \\\  \       \ \  \___|\ \  \ \  \|\  \       \ \  \\\  \ \  \\\  \   \ \  \/ / /
     \ \   ___  \ \  \ \  \_|/_\ \  \\\  \       \ \  \  __\ \  \ \   __  \       \ \   __  \ \  \\\  \   \ \   / /
      \ \  \\ \  \ \  \ \  \_|\ \ \  \\\  \       \ \  \|\  \ \  \ \  \ \  \       \ \  \ \  \ \  \\\  \   \/  / /
       \ \__\\ \__\ \__\ \_______\ \_______\       \ \_______\ \__\ \__\ \__\       \ \__\ \__\ \_______\__/  / /
        \|__| \|__|\|__|\|_______|\|_______|        \|_______|\|__|\|__|\|__|        \|__|\|__|\|_______|\___/ /
                                                                                                        \|___|/
    Author: Kieu Gia Huy a.k.a kiwi
    From: C.H.V with luv <3
    ...
*/
컴파일 시 표준 에러 (stderr) 메시지
zoo.cpp:92:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   92 | main(){
      | ^~~~
zoo.cpp: In function 'int main()':
zoo.cpp:94:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   94 |         freopen(TASK".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
zoo.cpp:95:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   95 |         freopen(TASK".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |