#include<bits/stdc++.h>
using namespace std;
const int N=100005;
vector<int> g[N];
pair<int,int> dir[4] = {{-1,0}, {1,0}, {0,-1}, {0,1}};
int n,m;
bool in(int x, int y) {
if(x>=0 && x<n && y>=0 && y<m) return true;
else return false;
}
void solve() {
cin>>n>>m;
string grid[n];
for(int i=0; i<m; i++) cin>>grid[i];
vector<vector<int>> dpth(n,vector<int>(m,n*m+1));
deque<pair<int,int>> q;
q.push_front({0,0});
dpth[0][0]=1;
int mx=1;
while(!q.empty()) {
auto tp=q.front(); q.pop_front();
auto [x,y]=tp;
mx=max(mx,dpth[x][y]);
for(int d=0; d<4; d++) {
int nx=dir[d].first+x, ny=dir[d].second+y;
if(in(nx,ny) && dpth[nx][ny]>dpth[x][y]+1 && grid[nx][ny]!='.') {
dpth[nx][ny]=dpth[x][y]+ (grid[nx][ny]!=grid[x][y]);
if(grid[nx][ny]!=grid[x][y]) q.push_back({nx,ny});
else q.push_front({nx,ny});
}
}
}
// for(int i=0;i<n;i++) {
// for(int j=0; j<m; j++) cout<<dpth[i][j]<<" ";
// cout<<"\n";
// }
cout<<mx<<"\n";
}
int main() {
// #ifndef ONLINE_JUDGE
// freopen("revegetate.in","r",stdin);
// freopen("revegetate.out","w",stdout);
// #endif
ios_base::sync_with_stdio(false), cin.tie(NULL);
int t=1;
//cin>>t;
while(t--) solve();
}