#include <bits/stdc++.h>
using namespace std;
#define int long long
#define bp '\n'
#define vp cout<<'\n';
int vst[4005][4005];
signed main(){
cin.tie(nullptr)->sync_with_stdio(false);
int n,m,mx=1,o,p;
cin>>n>>m;
vector<string>a(n);
for(auto&e:a){
cin>>e;
}
pair<int,int>dir[]={{1,0},{0,1},{-1,0},{0,-1}};
priority_queue<array<int,3>,vector<array<int,3>>,greater<array<int,3>>>pq;
pq.push({1,0,0});
while(!pq.empty()){
auto [dist,i,j]=pq.top();
pq.pop();
if(vst[i][j] or (i==n-1 and j==m-1))continue;
vst[i][j]=1;
mx=max(mx,dist);
for(auto&[f,s]:dir){
o=f+i,p=s+j;
if(o<0 or o>=n or p<0 or p>=m or vst[o][p] or a[o][p]=='.')continue;
int to=dist;
if(a[o][p]!=a[i][j])++to;
pq.push({to,o,p});
}
}
cout<<mx;
return 0;
}