#pragma comment (linker, "/STACK:256000000") #define _CRT_SECURE_NO_WARNINGS #include "bits/stdc++.h" using namespace std; typedef string::const_iterator State; #define eps 1e-11L #define MAX_MOD 1000000007LL #define GYAKU 500000004LL #define MOD 998244353LL #define seg_size 262144 #define pb push_back #define mp make_pair typedef long long ll; #define REP(a,b) for(long long (a) = 0;(a) < (b);++(a)) #define ALL(x) (x).begin(),(x).end() void init() { iostream::sync_with_stdio(false); cout << fixed << setprecision(100); } #define int ll int dist[3000][3000]; void solve() { int h, w; cin >> h >> w; REP(i, h) { REP(q, w) { dist[i][q] = 1e18; } } dist[0][0] = 0; REP(i, h) { string s; cin >> s; REP(q, s.length()) { int cost = 1; if (s[q] == 'k') { cost += llabs(i + q); } if (i != 0) { dist[i][q] = min(dist[i - 1][q] + cost, dist[i][q]); } if (q != 0) { dist[i][q] = min(dist[i][q - 1] + cost, dist[i][q]); } } } cout << dist[h - 1][w - 1] << endl; } #undef int int main() { init(); solve(); }