from collections import deque INF = 1<<16 W,H = map(int,input().split()) C = [list(input()) for _ in range(H)] x = 0 y = 0 for i in range(H): for j in range(W): if C[i][j] == '.': x = i y = j Queue = deque() Queue2 = deque() Queue2.append((x, y)) cost = [[INF] * W for _ in range(H)] dx = [1, -1, 0, 0] dy = [0, 0, 1, -1] while Queue2: x,y = Queue2.popleft() cost[x][y] = 0 Queue.append((x, y)) for i in range(4): nx,ny = x+dx[i],y+dy[i] if C[nx][ny] == '.' and cost[nx][ny] == INF: Queue2.append((nx, ny)) ans = INF while Queue: x,y = Queue.popleft() for i in range(4): nx,ny = x+dx[i],y+dy[i] if nx < 0 or H <= nx or ny < 0 or W <= ny: continue if C[nx][ny] == '#' and cost[x][y] + 1 < cost[nx][ny]: cost[nx][ny] = cost[x][y] + 1 Queue.append((nx, ny)) elif C[nx][ny] == '.' and cost[nx][ny] == INF: ans = min(ans, cost[x][y]) print(ans)