#include using namespace std; typedef long long ll; // #define DEBUG int main() { #ifdef DEBUG cout << "DEBUG MODE" << endl; ifstream in("input.txt"); //for debug cin.rdbuf(in.rdbuf()); //for debug #endif int h, w; const int inf = 1e9; cin >> h >> w; string field[h]; int dist[h][w]; int ans[h][w]; for (int i = 0; i < h; i++) for (int j = 0; j < h; j++) ans[i][j] = inf; ans[0][0] = 0; for (int i = 0; i < h; i++) cin >> field[i]; for (int i = 1; i < h; i++) { ans[i][0] = ans[i - 1][0] + 1; if (field[i][0] == 'k') ans[i][0] += i; } for (int j = 1; j < w; j++) { ans[0][j] = ans[0][j - 1] + 1; if (field[0][j] == 'k') ans[0][j] += j; } for (int i = 1; i < h; i++) { for (int j = 1; j < w; j++) { ans[i][j] = min(ans[i][j - 1], ans[i - 1][j]) + 1; if (field[i][j] == 'k') ans[i][j] += i + j; } } // for (int i = 0; i < h; i++) // { // for (int j = 0; j < w; j++) // cout << ans[i][j] << " "; // cout << endl; // } cout << ans[--h][--w] << endl; return 0; }