INF = 10 ** 7 import sys input = sys.stdin.readline sys.setrecursionlimit(100000000) dy = (-1,0,1,0) dx = (0,1,0,-1) from collections import deque def main(): h,w = map(int,input().split()) grid = [list(input()) for _ in range(h)] q = deque() q.append((0,0)) dist = [[INF] * w for _ in range(h)] dist[0][0] = 0 while q: i,j = q.popleft() for k in range(4): y = i + dy[k] x = j + dx[k] if y < 0 or y >= h: continue if x < 0 or x >= w: continue if grid[y][x] == 'k': if dist[y][x] > dist[i][j] + 1 + y + x: dist[y][x] = dist[i][j] + 1 + y + x q.append((y,x)) else: if dist[y][x] > dist[i][j] + 1: dist[y][x] = dist[i][j] + 1 q.append((y,x)) print(dist[-1][-1]) if __name__ == '__main__': main()