# coding:utf-8
int_max = 2 ** 63 - 1

def get_cost(start, end):
    return 1 if start == '.' and end == '.' else 2 ** 63 - 1


def warshall_floyd(n, ad_table):
    """
    ワーシャルフロイド法
    :param n: 頂点の数
    :param ad_table: 隣接行列
    """
    for k in range(n):            # 経由する頂点
        for i in range(n):        # 始点
            for j in range(n):    # 終点
                ad_table[i][j] = min(ad_table[i][j], ad_table[i][k] + ad_table[k][j])

    return ad_table


def main():

    H, W = map(int, input().split(' '))
    ad_table = [[int_max for _ in range(H*W)] for _ in range(H*W)]
    maze = []
    for _ in range(H):
        maze.append(input())

    for si in range(H):
        for sj in range(W):
            for ti in range(H):
                for tj in range(W):
                    if si == ti and sj == tj:
                        ad_table[(si*W)+sj][(ti*W)+tj] = 0
                    elif (abs(si - ti) == 1 and sj - tj == 0) or (si - ti == 0 and abs(sj - tj) == 1):
                        ad_table[(si*W)+sj][(ti*W)+tj] = get_cost(maze[si][sj], maze[ti][tj])

    res = warshall_floyd(H*W, ad_table)
    ans = -1
    for l in res:
        for n in l:
            if n != int_max and ans < n:
                ans = n
    print(ans)

if __name__ == '__main__':
    main()