#include "bits/stdc++.h" using namespace std; #define int long long #define FOR(i, a, b) for(int i=(a);i<(b);i++) #define RFOR(i, a, b) for(int i=(b-1);i>=(a);i--) #define REP(i, n) for(int i=0; i<(n); i++) #define RREP(i, n) for(int i=(n-1); i>=0; i--) #define ALL(a) (a).begin(),(a).end() #define UNIQUE_SORT(l) sort(ALL(l)); l.erase(unique(ALL(l)), l.end()); #define CONTAIN(a, b) find(ALL(a), (b)) != (a).end() #define array2(type, x, y) array, x> #define vector2(type) vector > #define out(...) printf(__VA_ARGS__) int dxy[] = {0, 1, 0, -1, 0}; void solve(); signed main() { #if DEBUG std::ifstream in("input.txt"); std::cin.rdbuf(in.rdbuf()); #endif cin.tie(0); ios::sync_with_stdio(false); solve(); return 0; } /*================================*/ #if DEBUG #define SIZE 5 #else #define SIZE 2000 #endif int H,W; // 合計コスト(route, trick) int C[SIZE][SIZE][2]; // トリックコスト(route, trick) int T[SIZE][SIZE][2]; int K[SIZE][SIZE]; typedef pair pos; void solve() { cin>>H>>W; char c; REP(h,H)REP(w,W) { cin>>c; K[h][w]=(c=='k'); } REP(h,SIZE)REP(w,SIZE)REP(i,2) C[h][w][i]=INT_MAX; REP(i,2) C[0][0][i]=0; REP(i,2) T[0][0][i]=0; queue que; que.push(pos(0,0)); while(!que.empty()) { pos p = que.front(); que.pop(); REP(d,4) { int h = p.first; int w = p.second; int nh = h + dxy[d]; int nw = w + dxy[d+1]; if (nh<0||nw<0||nh>=H||nw>=W) continue; // 合計コスト int routeCost = C[h][w][0] + 1; int trickCost = C[h][w][1] + (K[nh][nw] ? routeCost : 0); if (trickCost + routeCost < C[nh][nw][0]+C[nh][nw][1]) { C[nh][nw][0] = routeCost; C[nh][nw][1] = trickCost; que.push(pos(nh,nw)); } } } cout << C[H-1][W-1][0]+C[H-1][W-1][1] << endl; }