// Online C++ compiler to run C++ program online
#include <bits/stdc++.h>
using namespace std;
int h,w;
queue<pair<int,int>> missing;
void dfs(pair<int,int> node, vector<vector<char>>& grid, vector<vector<int>>& comp){
vector<int> posx = {0,0,1,-1};
vector<int> posy = {1,-1,0,0};
int x = node.first;
int y = node.second;
for(int i=0;i<4;i++){
int newX = x+posx[i];
int newY = y+posy[i];
if(newX<0 || newY<0 || newX>=h || newY>=w) continue;
if(comp[newX][newY]==-1){
if(grid[x][y]==grid[newX][newY]){
comp[newX][newY] = comp[x][y];
dfs({newX,newY}, grid, comp);
}else if(grid[newX][newY]!='.'){
comp[newX][newY]=comp[x][y]+1;
missing.push({newX,newY});
}
}
}
}
int main() {
cin>>h>>w;
vector<vector<char>> grid(h,vector<char>(w));
vector<vector<int>> comp(h,vector<int>(w,-1));
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
cin>>grid[i][j];
}
}
comp[0][0] = 0;
int maxId = 0;
missing.push({0,0});
while(!missing.empty()){
pair<int,int> node = missing.front();missing.pop();
dfs(node,grid, comp);
maxId = max(maxId,comp[node.first][node.second]);
}
cout<<maxId+1<<endl;
return 0;
}