W,H = map(int, input().split()) C = [input() for _ in range(H)] reached = [[0 for _ in range(W)] for __ in range(H)] empty = [] def search(x, y): if x <= 0 or H <= x-1 or y <= 0 or W <= y-1 or C[x][y] == '#': return if reached[x][y]: return reached[x][y] = True empty.append((x, y)) search(x+1, y) search(x-1, y) search(x, y+1) search(x, y-1) for h in range(H): for w in range(W): if C[h][w] == '.': x1, y1 = h, w break else: continue break search(x1, y1) empty1 = empty[:] empty.clear() for h in range(H): for w in range(W): t = (h, w) if C[h][w] == '.' and t not in empty1: x2, y2 = h, w break else: continue break search(x2, y2) ans = [] for p in empty1: for q in empty: ans.append(abs(p[0] - q[0]) + abs(p[1] - q[1])) print(min(ans)-1)