#define _USE_MATH_DEFINES #include using namespace std; //template #define rep(i,a,b) for(int i=(a);i<(b);i++) #define rrep(i,a,b) for(int i=(a);i>(b);i--) #define ALL(v) (v).begin(),(v).end() typedef long long int ll; const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12; void tostr(ll x,string& res){while(x)res+=('0'+(x%10)),x/=10; reverse(ALL(res)); return;} template inline bool chmax(T& a,T b){ if(a inline bool chmin(T& a,T b){ if(a>b){a=b;return 1;}return 0; } //template end typedef pair P; typedef pair T; int dx[]={0,1},dy[]={1,0}; char g[2010][2010]; int res[2010][2010]; int main(){ int h,w; scanf("%d%d",&h,&w); rep(i,0,h)rep(j,0,w){ cin>>g[i][j]; res[i][j]=inf; } res[0][0]=0; queue que; que.push({{0,0},{0,0}}); while(!que.empty()){ T t=que.front(); que.pop(); int d=t.first.first,add=t.first.second; int x=t.second.first,y=t.second.second; if(res[x][y]!=d+add)continue; rep(i,0,2){ int tx=x+dx[i],ty=y+dy[i],td=d+1,tadd=add; if(tx<0||tx>=h||ty<0||ty>=w)continue; if(g[tx][ty]=='k')tadd+=td; if(chmin(res[tx][ty],td+tadd))que.push({{td,tadd},{tx,ty}}); } } printf("%d\n",res[h-1][w-1]); return 0; }