from collections import deque w, h = map(int, input().split()) C = [input() for _ in range(h)] br = False stack = [] queue = deque() dist = [[-1] * w for _ in range(h)] for i in range(h): for j in range(w): if C[i][j] == ".": stack.append((i, j)) queue.append((i, j)) dist[i][j] = 0 br = True break if br: break directions = [(0, 1), (0, -1), (1, 0), (-1, 0)] while stack: i, j = stack.pop() for di, dj in directions: ni = i + di nj = j + dj if ni == -1 or nj == -1 or ni == h or nj == w: continue if dist[ni][nj] != -1 or C[ni][nj] == "#": continue dist[ni][nj] = 0 stack.append((ni, nj)) queue.append((ni, nj)) while queue: i, j = queue.popleft() for di, dj in directions: ni = i + di nj = j + dj if ni == -1 or nj == -1 or ni == h or nj == w: continue if dist[ni][nj] != -1: continue if C[ni][nj] == ".": print(dist[i][j]) queue.clear() break dist[ni][nj] = dist[i][j] + 1 queue.append((ni, nj))