import sys

from collections import deque



read = sys.stdin.read

readline = sys.stdin.readline



H, W = list(map(int, readline().split()))

S = [list(eval(input())) for i in range(H)]





def bfs(grid, start):

    distance = [[0] * W for _ in range(H)]

    queue = deque([start])

    visited = [[False] * W for _ in range(H)]

    visited[start[0]][start[1]] = True

    while queue:

        x, y = queue.popleft()



        for i, j in ([1, 0], [-1, 0], [0, 1], [0, -1]):

            new_y, new_x = y + j, x + i

            if 0 <= new_y < W and 0 <= new_x < H:

                if grid[new_x][new_y] == '.' and not visited[new_x][new_y]:

                    distance[new_x][new_y] = distance[x][y] + 1

                    visited[new_x][new_y] = True

                    queue.append((new_x, new_y))

    return distance





answer = 0

limit = H - 1 + W - 1

for i in range(H):

    for j in range(W):

        if S[i][j] == '#':

            continue



        cnt = 0

        for dx, dy in ([1, 0], [-1, 0], [0, 1], [0, -1]):

            new_y, new_x = dy + j, dx + i

            if 0 <= new_y < W and 0 <= new_x < H:

                if S[new_x][new_y] == '.':

                    cnt += 1



        if cnt > 2:

            continue



        tmp = max(list(map(max, bfs(S, (i, j)))))

        if tmp > answer:

            answer = tmp

        if answer == limit:

            print(answer)

            exit()



print(answer)

import sys

from collections import deque



read = sys.stdin.read

readline = sys.stdin.readline



H, W = list(map(int, readline().split()))

S = [list(eval(input())) for i in range(H)]





def bfs(grid, start):

    distance = [[0] * W for _ in range(H)]

    queue = deque([start])

    visited = [[False] * W for _ in range(H)]

    visited[start[0]][start[1]] = True

    while queue:

        x, y = queue.popleft()



        for i, j in ((1, 0), (-1, 0), (0, 1), (0, -1)):

            new_y, new_x = y + j, x + i

            if 0 <= new_y < W and 0 <= new_x < H:

                if grid[new_x][new_y] == '.' and not visited[new_x][new_y]:

                    distance[new_x][new_y] = distance[x][y] + 1

                    visited[new_x][new_y] = True

                    queue.append((new_x, new_y))

    return distance





answer = 0

for i in range(H):

    for j in range(W):

        if S[i][j] == '#':

            continue



        cnt = 0

        for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)):

            new_y, new_x = dy + j, dx + i

            if 0 <= new_y < W and 0 <= new_x < H:

                if S[new_x][new_y] == '.':

                    cnt += 1



        if cnt > 2:

            continue



        tmp = max(list(map(max, bfs(S, (i, j)))))

        if tmp > answer:

            answer = tmp



print(answer)

import sys
from collections import deque

read = sys.stdin.read
readline = sys.stdin.readline
H, W = list(map(int, readline().split()))
S = [list(eval(input())) for i in range(H)]


def bfs(grid, start):
    distance = [[0] * W for _ in range(H)]
    queue = deque([start])
    visited = [[False] * W for _ in range(H)]
    visited[start[0]][start[1]] = True
    while queue:
        x, y = queue.popleft()
        for i, j in ([1, 0], [-1, 0], [0, 1], [0, -1]):
            new_y, new_x = y + j, x + i
            if 0 <= new_y < W and 0 <= new_x < H:
                if grid[new_x][new_y] == "." and not visited[new_x][new_y]:
                    distance[new_x][new_y] = distance[x][y] + 1
                    visited[new_x][new_y] = True
                    queue.append((new_x, new_y))
    return distance


answer = 0
limit = H - 1 + W - 1
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            continue
        cnt = 0
        for dx, dy in ([1, 0], [-1, 0], [0, 1], [0, -1]):
            new_y, new_x = dy + j, dx + i
            if 0 <= new_y < W and 0 <= new_x < H:
                if S[new_x][new_y] == ".":
                    cnt += 1
        if cnt > 2:
            continue
        tmp = max(list(map(max, bfs(S, (i, j)))))
        if tmp > answer:
            answer = tmp
        if answer == limit:
            print(answer)
            exit()
print(answer)

import sys
from collections import deque

read = sys.stdin.read
readline = sys.stdin.readline
H, W = list(map(int, readline().split()))
S = [list(eval(input())) for i in range(H)]


def bfs(grid, start):
    distance = [[0] * W for _ in range(H)]
    queue = deque([start])
    visited = [[False] * W for _ in range(H)]
    visited[start[0]][start[1]] = True
    while queue:
        x, y = queue.popleft()
        for i, j in ((1, 0), (-1, 0), (0, 1), (0, -1)):
            new_y, new_x = y + j, x + i
            if 0 <= new_y < W and 0 <= new_x < H:
                if grid[new_x][new_y] == "." and not visited[new_x][new_y]:
                    distance[new_x][new_y] = distance[x][y] + 1
                    visited[new_x][new_y] = True
                    queue.append((new_x, new_y))
    return distance


answer = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            continue
        cnt = 0
        for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)):
            new_y, new_x = dy + j, dx + i
            if 0 <= new_y < W and 0 <= new_x < H:
                if S[new_x][new_y] == ".":
                    cnt += 1
        if cnt > 2:
            continue
        tmp = max(list(map(max, bfs(S, (i, j)))))
        if tmp > answer:
            answer = tmp
print(answer)

-        for i, j in ([1, 0], [-1, 0], [0, 1], [0, -1]): +        for i, j in ((1, 0), (-1, 0), (0, 1), (0, -1)): -limit = H - 1 + W - 1 -        for dx, dy in ([1, 0], [-1, 0], [0, 1], [0, -1]): +        for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)): -        if answer == limit: -            print(answer) -            exit()