# via https://juppy.hatenablog.com/entry/2018/11/01/%E8%9F%BB%E6%9C%AC_python_%E5%85%A8%E7%82%B9%E5%AF%BE%E6%9C%80%E7%9F%AD%E7%B5%8C%E8%B7%AF%E6%B3%95%EF%BC%88%E3%83%AF%E3%83%BC%E3%82%B7%E3%83%A3%E3%83%AB%E3%83%95%E3%83%AD%E3%82%A4%E3%83%89%E6%B3%95

h, w = map(int, input().split())
ss = []
for hh in range(h):
    ss += list(input())
n = h * w

def warshall_floyd(d):
    #d[i][j]: iからjへの最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d

d = [[0] * n for _ in range(n)]

#print(d)
for i in range(n):
    for j in range(n):
        if i != j:
            d[i][j] = float('inf')
        elif i == j and ss[i] == '#':
            d[i][j] = float('inf')
#print(d)

for hh in range(h):
    for ww in range(w):
        i = hh * w + ww
        upper = i - w
        lower = i + w
        left = i - 1 if ww > 0 else -1
        right = i + 1 if ww < w-1 else -1
        neighbors = [m for m in [upper, lower, left, right] if m in range(0, n)]
        for neighbor in neighbors:
            if ss[i] != ss[neighbor]:
                d[i][neighbor] = float('inf')
                d[neighbor][i] = float('inf')                
            else:
                d[i][neighbor] = 1
                d[neighbor][i] = 1
#print(d)
wf = warshall_floyd(d)
#print(wf)
ans = max([max([c for c in row if c < n]) for row in wf])
print(ans)

