#pragma GCC optimize ("Ofast") #include using namespace std; inline void rd(int &x){ int k; int m=0; x=0; for(;;){ k = getchar_unlocked(); if(k=='-'){ m=1; break; } if('0'<=k&&k<='9'){ x=k-'0'; break; } } for(;;){ k = getchar_unlocked(); if(k<'0'||k>'9'){ break; } x=x*10+k-'0'; } if(m){ x=-x; } } inline void rd(char &c){ int i; for(;;){ i = getchar_unlocked(); if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF){ break; } } c = i; } inline int rd(char c[]){ int i; int sz = 0; for(;;){ i = getchar_unlocked(); if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF){ break; } } c[sz++] = i; for(;;){ i = getchar_unlocked(); if(i==' '||i=='\n'||i=='\r'||i=='\t'||i==EOF){ break; } c[sz++] = i; } c[sz]='\0'; return sz; } inline void wt_L(char a){ putchar_unlocked(a); } inline void wt_L(int x){ int s=0; int m=0; char f[10]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ putchar_unlocked('-'); } while(s--){ putchar_unlocked(f[s]+'0'); } } template inline S chmin(S &a, T b){ if(a>b){ a=b; } return a; } int X; int Y; char A[2000][2002]; priority_queue>> q; int mincost[2000][2000]; int main(){ int i; int sx; int sy; int nx; int ny; int dist; int ndist; int cost; int ncost; int d; int dx[4] = {-1,1,0,0}; int dy[4] = {0,0,-1,1}; int res = 1073709056; rd(X); rd(Y); { int Lj4PdHRW; for(Lj4PdHRW=(0);Lj4PdHRW<(X);Lj4PdHRW++){ rd(A[Lj4PdHRW]); } } for(i=(0);i<(X);i++){ int j; for(j=(0);j<(Y);j++){ mincost[i][j] = 1073709056; } } q.push(make_pair(-1,make_pair((X-1)*Y+Y-1,1))); while(q.size()){ dist = q.top().first; sx = q.top().second.first / Y; sy = q.top().second.first % Y; cost = q.top().second.second; q.pop(); if(sx==0 && sy==0){ chmin(res, -dist-cost); continue; } if(mincost[sx][sy] <= cost){ continue; } mincost[sx][sy] = cost; for(d=(0);d<(4);d++){ nx = sx + dx[d]; ny = sy + dy[d]; if(nx < 0 || ny < 0 || nx >= X || ny >= Y){ continue; } if(A[nx][ny]=='k'){ ncost = cost +1; } else{ ncost = cost +0; } ndist = dist - ncost; q.push(make_pair(ndist,make_pair(nx*Y+ny,ncost))); } } wt_L(res); wt_L('\n'); return 0; } // cLay varsion 20200112-1 // --- original code --- // int X, Y; // char A[2000][2002]; // // priority_queue>> q; // int mincost[2000][2000]; // // { // int sx, sy, nx, ny, dist, ndist, cost, ncost, d; // int dx[4] = {-1,1,0,0}; // int dy[4] = {0,0,-1,1}; // int res = int_inf; // rd(X,Y,A(X)); // rep(i,X) rep(j,Y) mincost[i][j] = int_inf; // q.push(make_pair(-1,make_pair((X-1)*Y+Y-1,1))); // while(q.size()){ // dist = q.top().first; // sx = q.top().second.first / Y; // sy = q.top().second.first % Y; // cost = q.top().second.second; // q.pop(); // if(sx==0 && sy==0) res = X || ny >= Y) continue; // ncost = cost + if[A[nx][ny]=='k', 1, 0]; // ndist = dist - ncost; // q.push(make_pair(ndist,make_pair(nx*Y+ny,ncost))); // } // } // wt(res); // }