def solve():
    from collections import deque
    from sys import stdin
    f_i = stdin
    
    H, W = map(int, f_i.readline().split())
    
    maze = '#' * (W + 2)
    for i in range(H):
        line = f_i.readline().rstrip()
        maze += '#' + line + '#'
    maze += '#' * (W + 2)
    
    move = (1, -1, W + 2, -W - 2)
    start = maze.index('.')
    unvisited = [True] * len(maze)
    unvisited[start] = False
    
    q = deque([(start, 0)])
    while q:
        pos, cnt = q.popleft()
        
        for mv in move:
            next_pos = pos + mv
            if maze[next_pos] == '.' and unvisited[next_pos]:
                q.append((next_pos, cnt + 1))
                unvisited[next_pos] = False
    
    start = pos
    unvisited = [True] * len(maze)
    unvisited[start] = False
    
    q = deque([(start, 0)])
    while q:
        pos, cnt = q.popleft()
        
        for mv in move:
            next_pos = pos + mv
            if maze[next_pos] == '.' and unvisited[next_pos]:
                q.append((next_pos, cnt + 1))
                unvisited[next_pos] = False
    
    print(cnt)

solve()