import sys
import copy
import math
import bisect
import pprint
import bisect
from functools import reduce
from copy import deepcopy

glist = []


def makelist(num):
    if num == max:
        glist.append(list)
    else:
        list1 = copy.deepcopy(list)
        list1[num] = -1
        makelist(max, num + 1, list1)
        list2 = copy.deepcopy(list)
        list2[num] = 1
        makelist(max, num + 1, list2)


def checkcost(h, w):
    if h - 1 > -1:
        if glist[h - 1][w] == -1 or glist[h - 1][w] > glist[h][w] + 1:
            glist[h - 1][w] = glist[h][w] + 1
            checkcost(h - 1, w)

    if w - 1 > -1:
        if glist[h][w - 1] == -1 or glist[h][w - 1] > glist[h][w] + 1:
            glist[h][w - 1] = glist[h][w] + 1
            checkcost(h, w - 1)

    if h + 1 < maxh:
        if glist[h + 1][w] == -1 or glist[h + 1][w] > glist[h][w] + 1:
            glist[h + 1][w] = glist[h][w] + 1
            checkcost(h + 1, w)

    if w + 1 < maxw:
        if glist[h][w + 1] == -1 or glist[h][w + 1] > glist[h][w] + 1:
            glist[h][w + 1] = glist[h][w] + 1
            checkcost(h, w + 1)


maxh = 0
maxw = 0


def clacmaxxostaddress(maxh, maxw):
    max = -1
    maxh2 = -1
    maxj2 = -1
    for i in range(maxh):
        for j in range(maxw):
            if max < glist[i][j]:
                max = glist[i][j]
                maxh2 = i
                maxj2 = j
    return maxh2, maxj2


def clacmaxxost(maxh, maxw):
    max = -1
    maxh2 = -1
    maxj2 = -1
    for i in range(maxh):
        for j in range(maxw):
            if max < glist[i][j]:
                max = glist[i][j]
                maxh2 = i
                maxj2 = j
    return max


if __name__ == '__main__':
    a, b = map(int, input().split())
    maze = []
    maxh = a
    maxw = b
    for i in range(a):
        s = input()
        list = []
        for j in s:
            list.append(j)
        maze.append(list)
    address_h = -1
    address_w = -1
    for i in range(a):
        for j in range(b):
            if maze[i][j] == ".":
                address_h = i
                address_w = j
                break
        if maze[i][j] == ".":
            address_h = i
            address_w = j
            break

    glist = [[-1] * a for i in range(b)]
    glist[address_h][address_w] = 0
    checkcost(address_h, address_w)
    checkh, checkw = clacmaxxostaddress(a, b)
    glist = [[-1] * a for i in range(b)]
    glist[checkh][checkw] = 0

    checkcost(checkh, checkw)
    print(clacmaxxost(a, b))