#include using namespace std; int main() { ios::sync_with_stdio(false); int H, W; cin >> H >> W; vector G(H); for (int i = 0; i < H; ++i) { cin >> G[i]; } const int INF = 0x3f3f3f3f; vector> dp(H, vector(W, INF)); set> pq; pq.emplace(dp[0][0] = 0, 0, 0); while (pq.size()) { int d; int x, y; tie(d, x, y) = *pq.begin(); pq.erase(pq.begin()); if (d != dp[x][y]) continue; static int dx[] = { 1, 0, -1, 0 }; static int dy[] = { 0, 1, 0, -1 }; for (int di = 0; di < 4; ++di) { int nx = x + dx[di]; int ny = y + dy[di]; if (nx < 0 || nx == H || ny < 0 || ny == W) continue; int nd = d + (G[x][y] == 'k' ? x + y : 0) + 1; if (nd < dp[nx][ny]) pq.emplace(dp[nx][ny] = nd, nx, ny); } } cout << dp[H - 1][W - 1] << endl; return 0; }