#include <bits/stdc++.h>
#define int long long
using namespace std;
char a[4005][4005];
int depth[4005][4005],dx[]={1,-1,0,0},dy[]={0,0,1,-1};
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
//freopen("snow.in","r",stdin);
int n,m; cin>>n>>m;
for (int i=1;i<=n;i++) {
for (int j=1;j<=m;j++) cin>>a[i][j];
}
int ans=1;
deque<pair<int,int>> todo;
depth[1][1] = 1;
todo.push_back({1,1});
while (!todo.empty()) {
pair<int,int> tem=todo.front(); todo.pop_front();
if (a[tem.first][tem.second]=='.') continue;
ans = max(ans,depth[tem.first][tem.second]);
for (int i=0;i<4;i++) {
int nx=tem.first+dx[i],ny=tem.second+dy[i];
if (nx>n || ny>m || nx<1 || ny<1 || depth[nx][ny]!=0) continue;
if (a[tem.first][tem.second]==a[nx][ny]) {
depth[nx][ny] = depth[tem.first][tem.second];
todo.push_front({nx,ny});
}
else {
depth[nx][ny] = depth[tem.first][tem.second]+1;
todo.push_back({nx,ny});
}
}
}
cout<<ans;
return 0;
}