#!/usr/bin/env pypy3 import array import heapq import itertools INF = 10 ** 8 def compute(height, width, is_sea): dist = [array.array("L", (INF for _ in range(width))) for _ in range(height)] pq = [] for r, c in itertools.product(range(height), range(width)): if is_sea[r][c]: dist[r][c] = 0 pq.append((0, (r, c))) heapq.heapify(pq) while pq: _, (r0, c0) = heapq.heappop(pq) for dr, dc in itertools.product((1, 0, -1), repeat=2): if dr == dc == 0: continue r, c = r0 + dr, c0 + dc if r < 0 or r >= height or c < 0 or c >= height: continue if is_sea[r][c]: continue new_length = dist[r0][c0] + 1 if new_length < dist[r][c]: dist[r][c] = new_length heapq.heappush(pq, (new_length, (r, c))) rc = itertools.product(range(height), range(width)) return max(dist[r][c] for r, c in rc) def main(): height, width = map(int, input().split()) is_sea = [array.array("B", (True for _ in range(width + 2)))] for _ in range(height): row = array.array("B", (True, )) row.extend(map(lambda x: x == ".", input())) row.append(True) is_sea.append(row) is_sea.append(array.array("B", (True for _ in range(width + 2)))) print(compute(height + 2, width + 2, is_sea)) if __name__ == '__main__': main()