#include #define rep(i, n) for(int(i) = 0; (i) < (n); (i)++) #define FOR(i, m, n) for(int(i) = (m); (i) < (n); (i)++) #define ALL(v) (v).begin(), (v).end() #define LLA(v) (v).rbegin(), (v).rend() #define PB push_back #define MP(a, b) make_pair((a), (b)) using namespace std; template inline vector make_vec(size_t a, T val) { return vector(a, val); } template inline auto make_vec(size_t a, Ts... ts) { return vector(a, make_vec(ts...)); } template inline T read() { T t; cin >> t; return t; } template inline vector readv(size_t sz) { vector ret(sz); rep(i, sz) cin >> ret[i]; return ret; } template inline tuple reads() { return {read()...}; } template struct edge { int to; T cost; edge(int t, T c) : to(t), cost(c) {} }; using ll = long long; using pii = pair; using pll = pair; using Graph = vector>; template using WGraph = vector>>; const int INF = 1 << 30; const ll LINF = 1LL << 60; const int MOD = 1e9 + 7; using tpl = tuple; const int dx[] = {0, 1}; const int dy[] = {1, 0}; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); auto [H, W] = reads(); auto grid = readv(H); auto dist = make_vec(H, W, INF); dist[0][0] = 0; priority_queue, greater> que; que.push({0, 0, 0}); while(!que.empty()) { auto [d, x, y] = que.top(); que.pop(); rep(i, 2) { int nx = x + dx[i], ny = y + dy[i]; if(nx < 0 || nx >= W || ny < 0 || ny >= H) continue; int cost = (grid[ny][nx] == 'k' ? ny + nx + 1 : 1); if(dist[ny][nx] > d + cost) { dist[ny][nx] = d + cost; que.push({d + cost, nx, ny}); } } } cout << dist[H - 1][W - 1] << endl; }