#include using namespace std; const int dh[2] = {0, 1}, dw[2] = {1, 0}; vector dijkstra(const vector>> &G, int s){ struct RadixHeap { using P = pair; int bsr(long long x){ return (x == 0) ? -1 : 63-__builtin_clz(x); } vector

v[65]; long long last, sz; RadixHeap() : last(0), sz(0) { } bool empty () { return (sz == 0); } void push(long long x, int u) { sz++; v[bsr(x^last)+1].emplace_back(x, u); } P pop () { if (!v[0].size()) { int i = 1; while (!v[i].size()) { i++; } last = min_element(v[i].begin(), v[i].end())->first; for (auto x : v[i]) { v[bsr(x.first^last)+1].emplace_back(x); } v[i].clear(); } P res = v[0].back(); sz--; v[0].pop_back(); return res; } }; const long long inf = INT64_MAX; vector ret(G.size(), inf); ret[s] = 0; RadixHeap rh; rh.push(ret[s], s); while (!rh.empty()) { long long dist; int node; tie(dist, node) = rh.pop(); if (ret[node] < dist) continue; for (auto &e : G[node]) { int next_node; long long next_dist; tie(next_node, next_dist) = e; if (ret[next_node] > ret[node] + next_dist){ ret[next_node] = ret[node] + next_dist; rh.push(ret[next_node], next_node); } } } return ret; } int main() { int H, W; cin >> H >> W; vector S(H); for (int i = 0; i < H; i++) cin >> S[i]; auto idx = [&](int h, int w) { return h*W+w; }; vector>> G(H*W); for (int h = 0; h < H; h++) for (int w = 0; w < W; w++) { int from = idx(h, w); for (int i = 0; i < 2; i++) { int nh = h + dh[i], nw = w + dw[i], to = idx(nh, nw); if (nh < 0 || H <= nh || nw < 0 || W <= nw) continue; if (S[nh][nw] == 'k') G[from].emplace_back(to, nh+nw+1); else G[from].emplace_back(to, 1); } } vector dist = dijkstra(G, 0); cout << dist[idx(H-1, W-1)] << endl; return 0; }