from heapq import heappop,heappush H,W = map(int,input().split()) A = [input() for i in range(H)] inf = 10**10 dis = [[inf]*W for i in range(H)] dis[0][0] = 0 h = [[0,0,0,0]] dx = [1,0,-1,0] dy = [0,1,0,-1] while h: d,d2,x,y = heappop(h) if dis[x][y] != d: continue for i in range(4): nx = x+dx[i] ny = y+dy[i] if 0 <= nx < H and 0 <= ny < W: p = int(A[nx][ny] == "k") if dis[nx][ny] > d + 1 + p*(d2+1): dis[nx][ny] = d + 1 + p*(d2+1) heappush(h,[dis[nx][ny],d2+1,nx,ny]) print(dis[-1][-1])