// author: khba
#include "bits/stdc++.h"
using namespace std;
#ifdef khba
#include "C:\Users\Asus\Desktop\khba\debug.h"
#else
#define print(...) 42
#endif
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int32_t main() {
#ifdef khba
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
vector<string> a(n);
for (string& s : a) cin >> s;
vector<vector<int>> dp(n, vector<int>(m, 1e9));
dp[0][0] = 1;
int ans = 0;
deque <pair<int, int>> q;
q.emplace_back(0, 0);
while (q.size()) {
auto [u, v] = q.front(); q.pop_front();
ans = max(ans, dp[u][v]);
for (int dir = 0; dir < 4; ++dir) {
int x = u + dx[dir], y = v + dy[dir];
if (0 <= x and 0 <= y and x < n and y < m and a[x][y] != '.' and dp[x][y] == 1e9) {
if (a[x][y] == a[u][v]) q.emplace_front(x, y), dp[x][y] = dp[u][v];
else q.emplace_back(x, y), dp[x][y] = dp[u][v] + 1;
}
}
}
cout << ans;
return 0;
}