# -*- coding: utf-8 -*-
from itertools import product
import numpy as np
def solve():
    H, W = map(int, input().split())
    M = np.zeros((H,W), dtype=int)
    for h in range(H):
        M[h] = np.array(list(int(x=='.') for x in input()))

    Dist = float('inf')*np.ones((H*W, H*W))
    
    u = M&np.roll(M, 1, axis=0)&np.concatenate((np.zeros((1,W), dtype=int),np.ones((H-1,W), dtype=int)), axis=0)
    d = M&np.roll(M, -1, axis=0)&np.concatenate((np.ones((H-1,W), dtype=int),np.zeros((1,W), dtype=int)), axis=0)
    l = M&np.roll(M, 1, axis=1)&np.concatenate((np.zeros((H,1), dtype=int),np.ones((H,W-1), dtype=int)), axis=1)
    r = M&np.roll(M, -1, axis=1)&np.concatenate((np.ones((H,W-1), dtype=int),np.zeros((H,1), dtype=int)), axis=1)

    hu, wu = np.where(u)
    Dist[hu*W+wu, (hu-1)*W+wu] = 1
    hd, wd = np.where(d)
    Dist[hd*W+wd, (hd+1)*W+wd] = 1
    hl, wl = np.where(l)
    Dist[hl*W+wl, hl*W+wl-1] = 1
    hr, wr = np.where(r)
    Dist[hr*W+wr, hr*W+wr+1] = 1

    for v3 in range(H*W):
        Dist = np.minimum(Dist, Dist[:,v3:v3+1] + Dist[v3:v3+1,:])

    Dist[Dist==float('inf')] = -1

    return str(int(Dist.max()))

if __name__ == '__main__':
    print(solve())

