#include using namespace std::literals::string_literals; using i64 = std::int_fast64_t; using std::cout; using std::endl; using std::cin; template std::vector make_v(size_t a){return std::vector(a);} template auto make_v(size_t a,Ts... ts){ return std::vector(ts...))>(a,make_v(ts...)); } int main() { int h, w; scanf("%d%d", &h, &w); std::vector s(h); for(int i = 0; i < h; i++) cin >> s[i]; auto dp = make_v(h, w); for(int i = 0; i < h; i++) for(int j = 0; j < w; j++) dp[i][j] = 1LL << 60; dp[0][0] = 0; std::vector dx = {0, -1, 0, 1}; std::vector dy = {1, 0, -1, 0}; std::priority_queue>> qu; qu.push({0, {0, 0}}); while(!qu.empty()) { auto p = qu.top(); qu.pop(); int x = p.second.first, y = p.second.second, from = p.first; for(int k = 0; k < 4; k++) { int nx = x + dx[k], ny = y + dy[k]; if(nx < 0 or ny < 0 or nx >= h or ny >= w) continue; if(s[nx][ny] == '.') { if(dp[nx][ny] <= dp[x][y] + 1) continue; dp[nx][ny] = dp[x][y] + 1; qu.push({dp[nx][ny], {nx, ny}}); } else if(s[nx][ny] == 'k') { if(dp[nx][ny] <= dp[x][y] + (x + y) + 2) continue; dp[nx][ny] = dp[x][y] + (x + y) + 2; qu.push({dp[nx][ny], {nx, ny}}); } } } printf("%lld\n", dp[h - 1][w - 1]); return 0; }