from collections import deque def bfs(): for i in range(H): for j in range(W): if C[i][j] == '.': C[i][j] = '#' Queue = deque() Queue.append((i, j)) s = [(i, j)] while Queue: x,y = Queue.popleft() for i in range(4): nx,ny = x+dx[i],y+dy[i] if C[nx][ny] == '.': C[nx][ny] = '#' Queue.append((nx, ny)) s.append((nx, ny)) return s dx = [1, -1, 0, 0] dy = [0, 0, 1, -1] W,H = map(int,input().split()) C = [list(input()) for _ in range(H)] s = bfs() t = bfs() ans = 1<<16 for sx,sy in s: for tx,ty in t: ans = min(ans, abs(sx-tx) + abs(sy-ty) - 1) print(ans)