from heapq import * H, W = map(int, input().split()) A = [list(input()) for _ in range(H)] Q = [] A[0][0] = 0 heappush(Q, (0, 0, 0)) dir = [[1, 0], [0, 1]] cnt = 0 while Q: while Q and Q[0][0] == cnt: _, x, y = heappop(Q) for xd, yd in dir: x1 = x + xd y1 = y + yd if 0 <= x1 < H and 0 <= y1 < W: if A[x1][y1] == ".": A[x1][y1] = cnt + 1 heappush(Q, (cnt + 1, x1, y1)) elif A[x1][y1] == "k": A[x1][y1] = A[x][y] + 1 + x1 + y1 heappush(Q, (A[x][y] + 1 + x1 + y1, x1, y1)) cnt += 1 print(A[H - 1][W - 1])