#include<bits/stdc++.h>
using namespace std;
int h, w, ans;
bitset<4005> vis[4005];
vector<string> v(1);
deque<complex<int>> dq[2];
const complex<int> dir[]{complex<int>(1, 0), complex<int>(0, 1), complex<int>(-1, 0), complex<int>(0, -1)};
bool bfs(bool bl){
bool flag = 0;
while(dq[bl].size()){
complex<int> c = dq[bl][0];
dq[bl].pop_front();
for(complex<int> c1: dir){
int x = c.real(), y = c.imag(), x1 = (c1 += c).real(), y1 = c1.imag();
if(x1 <= 0 or x1 >= h or y1 <= 0 or y1 >= w or v[x1][y1] == '.' or vis[x1][y1]) continue;
vis[x1][y1] = 1;
if(v[x][y] == v[x1][y1]) dq[bl].push_back(c1);
else{
dq[!bl].push_back(c1);
flag = 1;
}
}
}return flag;
}signed main(){
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> h >> w;
for(int i = 1; i <= h; ++i){
string s;
cin >> s;
s.insert(s.begin(), ' ');
v.push_back(s);
}dq[0].push_back(complex<int>(1, 1));
for(bool bl = 0; bfs(bl); bl = !bl, ++ans);
cout << ans;
return 0;
}