from collections import deque
h, w = [int(x) for x ininput().split()]
maze = []
for y inrange(h+2):
if y == 0or y == h+1:
maze.append([1for x inrange(w+2)])
else:
line = [1]
for x, c inenumerate(input(), 1):
if c == "#":
line.append(1)
else:
line.append(0)
line.append(1)
maze.append(line)
defsolve(queue, maze):
end = set()
max_point = 0while queue:
x, y, point = queue.pop()
end.add((x, y)) # ★ここが遅くなっている理由です
max_point = max(max_point, point)
if maze[y+1][x] == 0:
ifnot (x, y+1) in end:
queue.appendleft((x, y+1, point+1))
if maze[y-1][x] == 0:
ifnot (x, y-1) in end:
queue.appendleft((x, y-1, point+1))
if maze[y][x+1] == 0:
ifnot (x+1, y) in end:
queue.appendleft((x+1, y, point+1))
if maze[y][x-1] == 0:
ifnot (x-1, y) in end:
queue.appendleft((x-1, y, point+1))
return max_point
max_count = 0for y inrange(1, h+1):
for x inrange(1, w+1):
if maze[y][x] == 0:
queue = deque()
queue.appendleft((x, y, 0))
max_count = max(max_count, solve(queue, maze))
print(max_count)