#! /usr/bin/env python # -*- coding: utf-8 -*- import os import sys import itertools import math from collections import Counter, defaultdict class Main(object): def __init__(self): pass def solve(self): ''' insert your code ''' w, h = map(int, raw_input().split()) board = [] for i in range(h): row = list(raw_input()) board.append(row) cave = set() d = [(0, 1), (1, 0), (0, -1), (-1, 0)] flg = False for i in range(h): for j in range(w): if board[i][j] == '.': s = [(i, j, 0)] while s: y, x, num = s.pop() # print y, x board[y][x] = '&' cave.add((y, x, 0)) for dy, dx in d: if 0 <= y + dy < h and 0 <= x + dx < w and (not (y + dy, x + dx, 0) in cave) and board[y+dy][x+dx] == '.': s.append((y + dy, x + dx, 0)) flg = True break if flg: break # print cave # for i in range(len(board)): # print ''.join(board[i]) # print board q = list(cave) used = set() for y, x, num in q: used.add((y, x)) while q: y, x, num = q.pop(0) # print y, x, num if board[y][x] == '.': print num - 1 break board[y][x] = str(num) used.add((y, x)) # for i in range(len(board)): # print ''.join(board[i]) for dy, dx in d: if 0 <= y + dy < h and 0 <= x + dx < w and (not (y + dy, x + dx) in used): # for i in range(num+1): # if (y + dy, x + dx, i) in cave: # break if board[y+dy][x+dx] == '#' or board[y+dy][x+dx] == '.': q.append((y + dy, x + dx, num + 1)) return None if __name__ == '__main__': m = Main() m.solve()