#include #define PI 3.14159265358979323846 #define MAXINF (1e18L) #define INF (1e9L) #define EPS (1e-9) #define MOD ((ll)(1e9+7)) #define REP(i, n) for(int i=0;i=0;--i) #define ALL(v) v.begin(),v.end() #define FIND(v,x) (binary_search(ALL(v),(x))) #define SORT(v) sort(ALL(v)) #define RSORT(v) sort(ALL(v));reverse(ALL(v)) #define DEBUG(x) cerr<<#x<<": "<void pr(A a){cout << (a) << endl;} templatevoid pr(A a,B b){cout << a << " " ;pr(b);} templatevoid pr(A a,B b,C c){cout << a << " " ;pr(b,c);} templatevoid pr(A a,B b,C c,D d){cout << a << " " ;pr(b,c,d);} template inline bool chmin(T& a, T b){return a>b ? a=b, true : false;} template inline bool chmax(T& a, T b){return a pii; typedef pair pll; int main(void) { int H,W; cin >> H >> W; vector S(H); for (int i = 0; i < H; i++) { cin >> S[i]; } queue q; vector> c(H, vector(W, -1)); c[0][0] = 0; q.push(pii(0, 0)); while(!q.empty()){ auto f = q.front();q.pop(); int dx[4]={-1,1,0,0}, dy[4]={0,0,-1,1}; for (int i = 0; i < 4; i++) { int dh = f.fi + dx[i]; int dw = f.se + dy[i]; if(0 <= dh && dh < H && 0 <= dw && dw < W){ int next = c[f.fi][f.se] + 1; if(S[dh][dw] == 'k') next *= 2; if(c[dh][dw] == -1){ c[dh][dw] = next; q.push(pii(dh,dw)); }else if(c[dh][dw] > next){ c[dh][dw] = next; q.push(pii(dh,dw)); } } } } pr(c[H-1][W-1]); }