#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; string s[2020]; long long score[2020][2020]; int main() { int h, w; cin >> h >> w; for (int i = 0; i < h; i++) { cin >> s[i]; for (int j = 0; j < w; j++) { score[i][j] = -1; } } score[0][0] = 0; set> st; priority_queue, vector>, greater>> que; long long ans = 1000000007; que.push(make_tuple(0, 0, 0)); while (!que.empty()) { tuple p = que.top(); int x = get<1>(p); int y = get<2>(p); long long z = get<0>(p); que.pop(); if (score[x][y] != -1 && z > score[x][y]) { continue; } if (x == h - 1 && y == w - 1) { cout << z << endl; return 0; } if (x != h - 1) { if (s[x + 1][y] == 'k') { if (score[x + 1][y] == -1 || score[x + 1][y] > (z + 1) + x + 1 + y) { que.push(make_tuple((z + 1) + x + 1 + y, x + 1, y)); score[x + 1][y] = (z + 1) + x + 1 + y; } } else { if (score[x + 1][y] == -1 || score[x + 1][y] > (z + 1)) { que.push(make_tuple(z + 1, x + 1, y)); score[x + 1][y] = z + 1; } } } if (y != w - 1) { if (s[x][y + 1] == 'k') { if (score[x][y + 1] == -1 || score[x][y + 1] > (z + 1) + x + y + 1) { que.push(make_tuple((z + 1) + x + y + 1, x, y + 1)); score[x][y + 1] = (z + 1) + x + 1 + y; } } else { if (score[x][y + 1] == -1 || score[x][y + 1] > (z + 1)) { que.push(make_tuple(z + 1, x, y + 1)); score[x][y + 1] = z + 1; } } } } }