import heapq import sys input = sys.stdin.readline sys.setrecursionlimit(10 ** 7) inf = 10**18 DX = (-1, 0, 1, 0, -1, -1, 1, 1) DY = (0, 1, 0, -1, -1, 1, -1, 1) DX = DX[:4] DY = DY[:4] H, W = map(int, input().split()) G = [input().rstrip() for _ in range(H)] dist = [[inf] * W for _ in range(H)] dist[0][0] = 0 que = [(0, 0, 0)] while que: ds, x, y = heapq.heappop(que) if dist[x][y] < ds: continue d = ds + 1 for dx, dy in zip(DX, DY): nx = x + dx ny = y + dy dk = ds + 1 + nx + ny if 0 <= nx < H and 0 <= ny < W: if G[nx][ny] == "." and dist[nx][ny] > d: dist[nx][ny] = d heapq.heappush(que, (d, nx, ny)) elif G[nx][ny] == "k" and dist[nx][ny] > dk: dist[nx][ny] = dk heapq.heappush(que, (dk, nx, ny)) # if True: # import numpy as np # print(np.array(dist)[:10, :10]) print(dist[H-1][W-1])