#include<bits/stdc++.h>
using namespace std;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int depth[4010][4010];
char a[4010][4010];
int n, m;
bool check(int x, int y) {
return (x > 0 && x <= n && y > 0 && y <= m && a[x][y] != '.');
}
int main() {
cin >> n >> m;
int ans = 1;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> a[i][j];
}
}
deque<pair<int, int>> q;
q.push_back({1, 1});
depth[1][1] = 1;
while(!q.empty()){
pair<int, int> cur = q.front();
q.pop_front();
ans = max(ans, depth[cur.first][cur.second]);
for(int i = 0; i < 4; i++){
int x = cur.first + dx[i], y = cur.second + dy[i];
if(check(x, y) && depth[x][y] == 0){
if(a[x][y] == a[cur.first][cur.second]){
depth[x][y] = depth[cur.first][cur.second];
q.push_front({x, y});
}else{
depth[x][y] = depth[cur.first][cur.second] + 1;
q.push_back({x, y});
}
}
}
}
cout << ans;
return 0;
}