#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> 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 <typename T> void dijkstra(int s,vector<vector<pair<int,T>>>& edges,vector<T>& dist) { priority_queue<pair<T,int>,vector<pair<T,int>>,greater<pair<T,int>>> que; dist[s] = 0; que.push(make_pair(0,s)); while (!que.empty()) { pair<ll,int> p = que.top(); que.pop(); int v = p.second; if (dist[v] < p.first) continue; for (pair<int,T>& 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<int> dist(h*w,INF); vector<vector<P>> 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<int>(0,edges,dist); cout << dist[h*w-1] << endl; }