#include using namespace std; /* #include #include using namespace __gnu_pbds; */ #define int long long #define REP(i,n) for(int i = 0;i < (int)(n);i++) #define RREP(i,n) for(int i = (int)n-1;i >= 0;i--) #define FOR(i,s,n) for(int i = s;i < (int)n;i++) #define RFOR(i,s,n) for(int i = (int)n-1;i >= s;i--) #define ALL(a) a.begin(),a.end() #define IN(a, x, b) (a<=x && xinline bool CHMAX(T&a,T b){if(ainline bool CHMIN(T&a,T b){if(a>b){a = b;return true;}return false;} templateistream&operator >>(istream&is,vector&vec){for(T&x:vec)is>>x;return is;} templateinline void in(T&t){cin>>t;} templateinline void in(T&t,Ts&...ts){cin>>t;in(ts...);} templateinline void out(T t){cout<inline void out(T t,Ts... ts){cout<vectormv(size_t a){return vector(a);} templateauto mv(size_t a,Ts... ts){return vector(ts...))>(a,mv(ts...));} templatetypename enable_if::value==0>::type fill(T&t,const V&v){t=v;} templatetypename enable_if::value!=0>::type fill(T&t,const V&v){for(auto &e:t)fill(e,v);} constexpr long long INF = 1e18; signed main(){ int H,W; in(H,W); auto s = mv(H); in(s); auto dist = mv(H,W); fill(dist,INF); #define P2 pair> priority_queue,greater>que; dist[0][0] = 0; que.push({dist[0][0],{0,0}}); int dx[] = {0,1,0,-1}; int dy[] = {1,0,-1,0}; while(que.size()){ int cost = que.top().first; int i = que.top().second.first; int j = que.top().second.second; que.pop(); if(dist[i][j]!=cost)continue; REP(k,4){ int ni = i + dy[k]; int nj = j + dx[k]; if(!IN(0,ni,H))continue; if(!IN(0,nj,W))continue; if(s[ni][nj]=='k'){ int ncost = cost + 1 + ni + nj; if(CHMIN(dist[ni][nj],ncost)){ que.push({dist[ni][nj],{ni,nj}}); } }else{ int ncost = cost + 1; if(CHMIN(dist[ni][nj],ncost)){ que.push({dist[ni][nj],{ni,nj}}); } } } } out(dist[H-1][W-1]); }