#include using namespace std; const int dh[2] = {0, 1}, dw[2] = {1, 0}; template vector dijkstra(const vector>> &G, int s){ using P = pair; const T inf = numeric_limits::max(); vector ret(G.size(), inf); ret[s] = 0; priority_queue, greater

> que; que.emplace(ret[s], s); while (!que.empty()) { T dist; int node; tie(dist, node) = que.top(); que.pop(); if (ret[node] < dist) continue; for (auto &e : G[node]) { int next_node; T next_dist; tie(next_node, next_dist) = e; if (ret[next_node] > ret[node] + next_dist) { ret[next_node] = ret[node] + next_dist; que.emplace(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; }