#pragma GCC optimize("Ofast") #include using namespace std; #define INF (1e9) #define rep(i, n) for(int i = 0; i < (int)(n); i++) #define all(x) x.begin(),x.end() const long double PI = acos(-1.0L); const long long MOD = 1000000007LL; // const long long MOD = 998244353LL; typedef long long ll; typedef pair pii; typedef pair pll; template inline bool chmax(T &a, T b) { if (a < b) { a = b; return true;} return false; } template inline bool chmin(T &a, T b) { if (a > b) { a = b; return true;} return false; } //////////////////////////////////////////////////////////////////////////////////////////////////////////// int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int H,W; cin >> H >> W; vector S(H); rep(i,H) cin >> S[i]; vector> dp(H, vector(W)); rep(i,H) { rep(j,W) { if (i == 0 && j == 0) continue; if (i == 0) { if (S[i][j] == 'k') dp[i][j] = dp[i][j-1]+i+j+1; else dp[i][j] = dp[i][j-1]+1; } else if (j == 0) { if (S[i][j] == 'k') dp[i][j] = dp[i-1][j]+i+j+1; else dp[i][j] = dp[i-1][j]+1; } else { if (S[i][j] == 'k') dp[i][j] = min(dp[i-1][j],dp[i][j-1])+i+j+1; else dp[i][j] = min(dp[i-1][j],dp[i][j-1])+1; } } } cout << dp[H-1][W-1] << endl; }