from collections import deque def bfs(sx, sy): queue = deque([]) queue.append((sx, sy)) visited = [[-1 for _ in range(W)] for _ in range(H)] visited[sx][sy] = 1 while len(queue) > 0: current_x, current_y = queue.popleft() for next_x, next_y in [(current_x-1, current_y), (current_x+1, current_y), (current_x, current_y-1), (current_x, current_y+1)]: if 0 <= next_x < H and 0 <= next_y < W: if C[next_x][next_y] == '.' and visited[next_x][next_y] == -1: visited[next_x][next_y] = 1 queue.append((next_x, next_y)) return visited W, H = map(int, input().split()) C = [input() for _ in range(H)] for i in range(H): for j in range(W): if C[i][j] == '.': amx, amy = i, j break visitedA = bfs(amx, amy) holeA = [] for i in range(H): for j in range(W): if visitedA[i][j] == 1: holeA.append((i, j)) for i in range(H): for j in range(W): if C[i][j] == '.' and visitedA[i][j] == -1: bmx, bmy = i, j break visitedB = bfs(bmx, bmy) holeB = [] for i in range(H): for j in range(W): if visitedB[i][j] == 1: holeB.append((i, j)) ans = 10 ** 18 for Ax, Ay in holeA: for Bx, By in holeB: ans = min(ans, abs(Ax-Bx) + abs(Ay-By) - 1) print(ans)