from collections import deque INF = 1<<60 direction = [(-1,0),(0,1),(1,0),(0,-1)] H, W = map(int, input().split()) S = [input() for _ in range(H)] que = deque() que.append((0, 0)) visited = [[INF]*W for _ in range(H)] visited[0][0] = 0 MIN = 1<<60 while que: h, w = que.popleft() if H-2 <= h or W-2 <= w: MIN = min(MIN, visited[h][w]-h-w) for dh, dw in direction: nh, nw = h+dh, w+dw if 0<=nh