#include using namespace std; int H, W; char board[2001][2001]; int dp[2001][2001]; int main(void) { cin.tie(0); ios::sync_with_stdio(false); cin >> H >> W; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { cin >> board[i][j]; } } for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { dp[i][j] = 1e9; } } dp[0][0] = 0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (i == 0 && j == 0) continue; int cost = 0; if (board[i][j] == 'k') { cost += abs(i) + abs(j); } if (i - 1 >= 0) { dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1 + cost); } if (j - 1 >= 0) { dp[i][j] = min(dp[i][j], dp[i][j - 1] + 1 + cost); } } } cout << dp[H - 1][W - 1] << '\n'; return 0; }