#include #include #include #include #include #include #include #include #include using namespace std; struct aaa{aaa(){cin.tie(nullptr); ios::sync_with_stdio(false); cout< P; typedef tuple TP; int dx[4]={1,0,-1,0}; int dy[4]={0,1,0,-1}; void bfs(int h, int w, vector& a, vector>& ans) { priority_queue, greater> q; q.emplace(0, 0, P(0,0)); while(!q.empty()) { int c,kc,x,y; P p; tie(c, kc, p) = q.top(); q.pop(); tie(x,y) = p; if (x < 0 || x >= w) continue; if (y < 0 || y >= h) continue; if (ans[y][x] >= 0) continue; ans[y][x] = c; if (a[y][x] == 'k') { c = c*2 + 1 - kc; kc++; } else c++; for (int i=0; i<4; i++) { int xi = x + dx[i]; int yi = y + dy[i]; if (xi < 0 || xi >= w) continue; if (yi < 0 || yi >= h) continue; q.emplace(c, kc, P(xi, yi)); } } } int main() { int h,w; cin >> h >> w; vector a(h); for (int i=0; i> a[i]; vector> ans(h, vector (w,-1)); bfs(h,w,a,ans); cout << ans[h-1][w-1] << endl; }