#include using namespace std; using i64 = long; signed main() { int h, w; cin >> h >> w; vector a(h); for(auto& x : a) cin >> x; vector> dist(h, vector(w, 1e9)); dist[0][0] = 0; priority_queue, vector>, greater>> que; que.emplace(0, 0, 0); vector dx{0, 1}; vector dy{1, 0}; while(!que.empty()){ int d, x, y; tie(d, x, y) = que.top(); que.pop(); if(dist[x][y] != d) continue; for(int i = 0; i < 2; ++i){ int nx = x + dx[i]; int ny = y + dy[i]; if(nx < 0 || ny < 0 || nx >= h || ny >= w) continue; int nd = d + 1; if(a[nx][ny] == 'k'){ nd = d + 1 + (nx + ny); } if(nd < dist[nx][ny]){ dist[nx][ny] = nd; que.emplace(nd, nx, ny); } } } cout << dist.back().back() << endl; }