#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair PL; typedef pair P; typedef pair E; const int INF = (1<<30); const ll INFLL = (1ll<<60); const ll MOD = (ll)(1e9+7); #define l_ength size void mul_mod(ll& a, ll b){ a *= b; a %= MOD; } void add_mod(ll& a, ll b){ a = (a, greater > pq; int main(void){ int dx[4]={-1,0,0,1},dy[4]={0,-1,1,0},i,j,k,x,y; ll c,e; PL tmp; fill(dist[0],dist[2020],PL(INFLL,INFLL)); dist[0][0] = PL(0ll,0ll); pq.push(E(dist[0][0],P(0,0))); cin >> h >> w; for(i=0; i> a[i]; } while(!pq.empty()){ i = pq.top().second.first; j = pq.top().second.second; c = pq.top().first.first; e = pq.top().first.second; pq.pop(); if(dist[i][j] < PL(c,e)){ continue; } for(k=0; k<4; ++k){ x = i + dx[k]; y = j + dy[k]; if(!check(x,y)){ continue; } tmp = PL(c+1,e+1); if(a[x][y]=='k'){ tmp.first += tmp.second; } if(dist[x][y] > tmp){ dist[x][y] = tmp; pq.push(E(dist[x][y],P(x,y))); } } } cout << dist[h-1][w-1].first << endl; return 0; }