#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const long long MAX = 5100000; const long long INF = 1LL << 60; const long long mod = 1000000007LL; //const long long mod = 998244353LL; using namespace std; typedef unsigned long long ull; typedef long long ll; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); ll H, W; cin >> H >> W; vector vs(H); for (ll i = 0; i < H; i++) cin >> vs[i]; vector> dp(H, vector(W, INF)); ll dh[2] = { 1,0 }; ll dw[2] = { 0,1 }; dp[0][0] = 0; for (ll i = 0; i < H; i++) { for (ll j = 0; j < W; j++) { for (ll k = 0; k < 2; k++) { ll nh = i + dh[k]; ll nw = j + dw[k]; if (nh >= H || nw >= W) continue; if (vs[nh][nw] == 'k') { chmin(dp[nh][nw], dp[i][j] + 1 + nh + nw); } else { chmin(dp[nh][nw], dp[i][j] + 1); } } } } cout << dp[H - 1][W - 1] << endl; return 0; }