#include using namespace std; int main() { int h, w; cin >> h >> w; string a[h]; for(int i = 0; i < h; ++i) cin >> a[i]; priority_queue, vector>, greater>> pq; int64_t dp[h][w]; for(int i = 0; i < h; ++i) for(int j = 0; j < w; ++j) dp[i][j] = 1e18; dp[0][0] = 0; pq.emplace(0, 0, 0); while(not pq.empty()) { int64_t cost; int y, x; tie(cost, y, x) = pq.top(); pq.pop(); if(cost > dp[y][x]) continue; const int dy[] = {1, 0}; const int dx[] = {0, 1}; for(int d = 0; d < 2; ++d) { int ny = y + dy[d]; if(h <= ny) continue; int nx = x + dx[d]; if(w <= nx) continue; int64_t ncost = cost + 1; if(a[ny][nx] == 'k') { ncost += (ny + nx); } if(ncost >= dp[ny][nx]) continue; dp[ny][nx] = ncost; pq.emplace(ncost, ny, nx); } } cout << dp[h - 1][w - 1] << '\n'; return 0; }