#include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; #define int long long #define double long double typedef vector VI; typedef pair >pii; typedef pairP; typedef vector VP; typedef vector VS; typedef priority_queue PQ; templatebool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } templatebool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } #define fore(i,a) for(auto &i:a) #define REP(i,n) for(int i=0;i, greater > q2; int H, W; VS S(2010); int dx[] = { 0,1 }; int dy[] = { 1,0 }; int d[2010][2010]; int cnt[2010][2010]; bool is_in(int x, int y) { return (0 <= x && 0 <= y && x < H&&y < W); } signed main() { cin.tie(0); ios::sync_with_stdio(false); cin >> H >> W; REP(i, H)cin >> S[i]; priority_queue, greater >Q; REP(i, H)REP(j, W)d[i][j] = INF; Q.push(make_pair(0, P(0, 0))); d[0][0] = 0; while (!Q.empty()) { int x = Q.top().second.first; int y = Q.top().second.second; int t = Q.top().first; Q.pop(); if (d[x][y] < t)continue; REP(k, 2) { int tox = x + dx[k], toy = y + dy[k]; if (!is_in(tox, toy))continue; bool F = 0; int tmp = d[x][y] + 1; if (S[tox][toy] == 'k') { tmp *= 2; tmp -= cnt[x][y]; F = 1; } if (d[tox][toy] > tmp) { d[tox][toy] = tmp; cnt[tox][toy] += cnt[x][y] + F; Q.push(make_pair(tmp, P(tox, toy))); } } } cout << d[H - 1][W - 1] << endl; return 0; }