#include #define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i)) using namespace std; template inline void chmin(T & a, U const & b) { a = min(a, b); } template auto make_table(X x, T a) { return vector(x, a); } template auto make_table(X x, Y y, Z z, Zs... zs) { auto cont = make_table(y, z, zs...); return vector(x, cont); } template istream & operator >> (istream & in, vector & xs) { REP (i, xs.size()) { in >> xs[i]; } return in; } int64_t solve(int h, int w, const vector & f) { auto dp = make_table(h, w, INT64_MAX); dp[0][0] = 0; REP (y, h) REP (x, w) { if (y - 1 >= 0) chmin(dp[y][x], dp[y - 1][x] + 1); if (x - 1 >= 0) chmin(dp[y][x], dp[y][x - 1] + 1); if (f[y][x] == 'k') dp[y][x] += y + x; } return dp[h - 1][w - 1]; } int main() { int h, w; cin >> h >> w; vector f(h); cin >> f; cout << solve(h, w, f) << endl; return 0; }