#include #define REP(i, n) for(int i = 0;i < n;i++) #define VSORT(v) sort(v.begin(), v.end()) #define VRSORT(v) sort(v.rbegin(), v.rend()) #define ll long long using namespace std; typedef pair P; typedef pair LP; typedef pair PP; typedef pair LPP; //typedef vectorvec; //typedef vectorvec; //typedef vector mat; typedef vector> Graph; const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; const int INF = 1000000000; const ll LINF = 1000000000000000000;//1e18 const ll MOD = 1000000007; const double PI = acos(-1.0); const double EPS = 1e-10; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template inline void add(T &a, T b){a = ((a+b) % MOD + MOD) % MOD;}; char fi[2020][2020]; int dist1[2020][2020]; int dist2[2020][2020]; int main(){ cin.tie(0); ios::sync_with_stdio(false); int H, W; cin >> H >> W; REP(i,H) REP(j,W) cin >> fi[i][j]; REP(i,2020) REP(j,2020){ dist1[i][j] = INF; dist2[i][j] = INF; } dist1[0][0] = 0; dist2[0][0] = 0; priority_queue, vector>, greater>> q; q.emplace(0, 0, 0); while(!q.empty()){ int h, w, d; tie(d, h, w) = q.top(); q.pop(); //cout << h << " " << w << " " << d << endl; REP(i,2){ int nh = h + dx[i], nw = w + dy[i]; if(nh < 0 || nh >= H || nw < 0 || nw >= W) continue; if(dist1[nh][nw] > d + 1){ dist1[nh][nw] = d + 1; q.emplace(d + 1, nh, nw); } } } q.emplace(0, 0, 0); while(!q.empty()){ int h, w, d; tie(d, h, w) = q.top(); q.pop(); //cout << h << " " << w << " " << d << endl; REP(i,2){ int nh = h + dx[i], nw = w + dy[i]; if(nh < 0 || nh >= H || nw < 0 || nw >= W) continue; if(fi[nh][nw] == 'k'){ if(dist2[nh][nw] > d + 1 + dist1[nh][nw]){ dist2[nh][nw] = d + 1 + dist1[nh][nw]; q.emplace(dist2[nh][nw], nh, nw); } } else{ if(dist2[nh][nw] > d + 1){ dist2[nh][nw] = d + 1; q.emplace(dist2[nh][nw], nh, nw); } } } } /*REP(i,H) REP(j,W){ if(fi[i+1][j] == 'k') chmin(dist2[i+1][j], 2 * dist1[i][j] +1); else if(fi[i][j+1] == 'k') chmin(dist2[i][j+1], 2 * dist1[i][j] +1); else{ chmin(dist2[i+1][j], dist2[i][j] + 1); chmin(dist2[i][j+1], dist2[i][j] + 1); } } */ cout << dist2[H-1][W-1] << endl; }