#include using namespace std; typedef long long ll; typedef pair P; #define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0) #define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0) const int INF = 1<<30; template void dijkstra(int s,vector>>& edges,vector& dist) { priority_queue,vector>,greater>> que; dist[s] = 0; que.push(make_pair(0,s)); while (!que.empty()) { pair p = que.top(); que.pop(); int v = p.second; if (dist[v] < p.first) continue; for (pair& e : edges[v]) { if (dist[e.first] > dist[v]+e.second) { dist[e.first] = dist[v]+e.second; que.push(make_pair(dist[e.first],e.first)); } } } } int main() { int h,w; cin >> h >> w; string a[2000]; for (int i = 0;i < h;++i) cin >> a[i]; vector dist(h*w,INF); vector> edges(h*w); for (int i = 0;i < h;++i) for (int j = 1;j < w;++j) { if (a[i][j] == 'k') edges[i*w+j-1].push_back(P(i*w+j,i+j+1)); else edges[i*w+j-1].push_back(P(i*w+j,1)); } for (int i = 1;i < h;++i) for (int j = 0;j < w;++j) { if (a[i][j] == 'k') edges[(i-1)*w+j].push_back(P(i*w+j,i+j+1)); else edges[(i-1)*w+j].push_back(P(i*w+j,1)); } dijkstra(0,edges,dist); cout << dist[h*w-1] << endl; }