#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for(int i = 0; i < (int)n; ++i)
#define FOR(i, a, b) for(int i = a; i < (int)b; ++i)
#define rrep(i, n) for(int i = ((int)n - 1); i >= 0; --i)

using ll = long long;
using ld = long double;

const ll INF = 1e18;
const int Inf = 1e9;
const double EPS = 1e-9;
const int MOD = 1e9 + 7;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(0);
    int h, w;
    cin >> h >> w;
    int dx[2] = {0, 1};
    int dy[2] = {1, 0};
    vector<string> s(h);
    vector<vector<ll> > dist(h, vector<ll>(w, INF));
    rep (i, h) cin >> s[i];
    queue<pair<int, int> > q;
    q.push({0, 0});
    dist[0][0] = 0;
    while (!q.empty()) {
        int x, y;
        x = q.front().first, y = q.front().second;
        q.pop();
        rep (i, 2) {
            int nx, ny;
            nx = x + dx[i];
            ny = y + dy[i];
            if (nx >= h || ny >= w) continue;
            ll tmp = dist[x][y] + 1;
            if (s[nx][ny] == 'k') tmp += nx + ny;
            if (tmp >= dist[nx][ny]) continue;
            dist[nx][ny] = tmp;
            q.push({nx, ny});
        }
    }
    cout << dist[h - 1][w - 1] << endl;
    
    return 0;
}