from collections import deque

n, m = map(int, input().split())
d = []

mas = []
    for i in range(n):
        mas.extend(list(map(list, input().split())))
        d.append([float("inf")] * m)


def tansaku(sx, sy, d):

    dx = [1, 0, -1, 0]
    dy = [0, 1, 0, -1]

    que = deque([])
    que.append((sx, sy))
    d[sx][sy] = 0
    while que:
        # キューの先頭を取り出す
        p = que.popleft()
        for i in range(4):
            # 移動4方向をループ
            nx = p[0] + dx[i]
            ny = p[1] + dy[i]
            if (
                0 <= nx < n
                and 0 <= ny < m
                and mas[nx][ny] != "#"
                and d[nx][ny] == float("inf")
            ):
                # 移動できるならキューに入れ，その点の距離を p からの距離 +1 で確定する
                que.append((nx, ny))
                d[nx][ny] = d[p[0]][p[1]] + 1
        d_max = max(list([max(d[0]), max(d[n-1]), max(d[:][0]), max(d[:][m-1])]))
    return d_max

dis = 0
for i in range(n):
    dis_i = tansaku(i, 0,d)
    if dis_i > dis:
        dis = dis_i
    dis_i = tansaku(i, m-1,d)
    if dis_i > dis:
        dis = dis_i

for i in range(m):
    dis_i = tansaku(0, i,d)
    if dis_i > dis:
        dis = dis_i
    dis_i = tansaku(n-1, i,d)
    if dis_i > dis:
        dis = dis_i

print(dis)