import heapq H,W = map(int, input().split()) L = [input() for _ in range(H)] inf = 10**15 dist = [[inf]*W for _ in range(H)] h = [] heapq.heappush(h, (0, 0, 0, 0)) while dist[H-1][W-1] == inf: temp = heapq.heappop(h) d = temp[0] i = temp[1] j = temp[2] sub_d = temp[3] if i > 0: if L[i-1][j] == "." and dist[i-1][j] > d+1: dist[i-1][j] = d+1 heapq.heappush(h, (d+1, i-1, j, sub_d+1)) elif L[i-1][j] == "k" and dist[i-1][j] > d+2+sub_d: dist[i-1][j] = d+2+sub_d heapq.heappush(h, (d+2+sub_d, i-1, j, sub_d+1)) if i < H-1: if L[i+1][j] == "." and dist[i+1][j] > d+1: dist[i+1][j] = d+1 heapq.heappush(h, (d+1, i+1, j, sub_d+1)) elif L[i+1][j] == "k" and dist[i+1][j] > d+2+sub_d: dist[i+1][j] = d+2+sub_d heapq.heappush(h, (d+2+sub_d, i+1, j, sub_d+1)) if j > 0: if L[i][j-1] == "." and dist[i][j-1] > d+1: dist[i][j-1] = d+1 heapq.heappush(h, (d+1, i, j-1, sub_d+1)) elif L[i][j-1] == "k" and dist[i][j-1] > d+2+sub_d: dist[i][j-1] = d+2+sub_d heapq.heappush(h, (d+2+sub_d, i, j-1, sub_d+1)) if j < W-1: if L[i][j+1] == "." and dist[i][j+1] > d+1: dist[i][j+1] = d+1 heapq.heappush(h, (d+1, i, j+1, sub_d+1)) elif L[i][j+1] == "k" and dist[i][j+1] > d+2+sub_d: dist[i][j+1] = d+2+sub_d heapq.heappush(h, (d+2+sub_d, i, j+1, sub_d+1)) print(dist[H-1][W-1])