from collections import deque



def bfs(grid, start, wall, h, w):

    visited = [[False for _ in range(w+2)] for _ in range(h+2)]

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



    queue = deque([])

    queue.append([start[0], start[1], 0])

    dxy = [(1,0), (-1,0), (0,1), (0,-1)]

    cost = 0

    while queue:

        y,x,cnt = queue.popleft()

        for dy, dx in dxy:

            y_,x_ = y+dy,x+dx

            if grid[y_][x_] == wall or visited[y_][x_]:

                continue

            queue.append([y_,x_,cnt+1])

            if cnt+1 > cost:

                cost = cnt+1

            visited[y_][x_] = True

    return cost



h,w = list(map(int, input().split()))

wall = '#'

edges = [wall] * (w+2)

grid = [edges]

for i in range(h):

    s = eval(input())

    grid.append([wall] + list(s) + [wall])

grid.append(edges)





cand = {2:[], 3:[], 4:[]}

dxy = [(1,0), (-1,0), (0,1), (0,-1)]

for y in range(1,h+1):

    for x in range(1,w+1):

        if grid[y][x] == wall:

            continue

        wl = 0

        for dy, dx in dxy:

            y_,x_ = y+dy,x+dx

            if grid[y_][x_] == wall:

                wl += 1

        if wl > 1:

            cand[wl].append((y,x))



ans = 0

starts = []

for i in [4,3,2]:

    if cand[i] != []:

        starts = cand[i]

        break

for start in starts:

    cnt = bfs(grid, start, wall, h, w)

    if cnt > ans:

        ans = cnt

print(ans)

from collections import deque



def bfs(grid, sy, sx, wall):

    queue = deque([])

    queue.append([sy, sx, 0])

    visited = {}

    visited[(sy,sx)] = 0

    dxy = [(1,0), (-1,0), (0,1), (0,-1)]

    while queue:

        y,x,cnt = queue.popleft()

        for dy, dx in dxy:

            y_,x_ = y+dy,x+dx

            if grid[y_][x_] == wall or (y_,x_) in visited:

                continue

            queue.append([y_,x_,cnt+1])

            visited[(y_,x_)] = cnt+1

    return max(visited.values())



h,w = list(map(int, input().split()))

wall = '#'

edges = [wall] * (w+2)

grid = [edges]

for i in range(h):

    s = eval(input())

    grid.append([wall] + list(s) + [wall])

grid.append(edges)



starts = []

dxy = [(1,0), (-1,0), (0,1), (0,-1)]

for y in range(1,h+1):

    for x in range(1,w+1):

        if grid[y][x] == wall:

            continue

        cnt = 0

        for dy, dx in dxy:

            y_,x_ = y+dy,x+dx

            if grid[y_][x_] == wall:

                cnt += 1

        if cnt > 1:

            starts.append((y,x))



ans = 0

for y,x in starts:

    tmp = bfs(grid, y, x, wall)

    if ans < tmp:

        ans = tmp

print(ans)

from collections import deque


def bfs(grid, start, wall, h, w):
    visited = [[False for _ in range(w + 2)] for _ in range(h + 2)]
    visited[start[0]][start[1]] = True
    queue = deque([])
    queue.append([start[0], start[1], 0])
    dxy = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    cost = 0
    while queue:
        y, x, cnt = queue.popleft()
        for dy, dx in dxy:
            y_, x_ = y + dy, x + dx
            if grid[y_][x_] == wall or visited[y_][x_]:
                continue
            queue.append([y_, x_, cnt + 1])
            if cnt + 1 > cost:
                cost = cnt + 1
            visited[y_][x_] = True
    return cost


h, w = list(map(int, input().split()))
wall = "#"
edges = [wall] * (w + 2)
grid = [edges]
for i in range(h):
    s = eval(input())
    grid.append([wall] + list(s) + [wall])
grid.append(edges)
cand = {2: [], 3: [], 4: []}
dxy = [(1, 0), (-1, 0), (0, 1), (0, -1)]
for y in range(1, h + 1):
    for x in range(1, w + 1):
        if grid[y][x] == wall:
            continue
        wl = 0
        for dy, dx in dxy:
            y_, x_ = y + dy, x + dx
            if grid[y_][x_] == wall:
                wl += 1
        if wl > 1:
            cand[wl].append((y, x))
ans = 0
starts = []
for i in [4, 3, 2]:
    if cand[i] != []:
        starts = cand[i]
        break
for start in starts:
    cnt = bfs(grid, start, wall, h, w)
    if cnt > ans:
        ans = cnt
print(ans)

from collections import deque


def bfs(grid, sy, sx, wall):
    queue = deque([])
    queue.append([sy, sx, 0])
    visited = {}
    visited[(sy, sx)] = 0
    dxy = [(1, 0), (-1, 0), (0, 1), (0, -1)]
    while queue:
        y, x, cnt = queue.popleft()
        for dy, dx in dxy:
            y_, x_ = y + dy, x + dx
            if grid[y_][x_] == wall or (y_, x_) in visited:
                continue
            queue.append([y_, x_, cnt + 1])
            visited[(y_, x_)] = cnt + 1
    return max(visited.values())


h, w = list(map(int, input().split()))
wall = "#"
edges = [wall] * (w + 2)
grid = [edges]
for i in range(h):
    s = eval(input())
    grid.append([wall] + list(s) + [wall])
grid.append(edges)
starts = []
dxy = [(1, 0), (-1, 0), (0, 1), (0, -1)]
for y in range(1, h + 1):
    for x in range(1, w + 1):
        if grid[y][x] == wall:
            continue
        cnt = 0
        for dy, dx in dxy:
            y_, x_ = y + dy, x + dx
            if grid[y_][x_] == wall:
                cnt += 1
        if cnt > 1:
            starts.append((y, x))
ans = 0
for y, x in starts:
    tmp = bfs(grid, y, x, wall)
    if ans < tmp:
        ans = tmp
print(ans)

-def bfs(grid, start, wall, h, w): -    visited = [[False for _ in range(w + 2)] for _ in range(h + 2)] -    visited[start[0]][start[1]] = True +def bfs(grid, sy, sx, wall): -    queue.append([start[0], start[1], 0]) +    queue.append([sy, sx, 0]) +    visited = {} +    visited[(sy, sx)] = 0 -    cost = 0 -            if grid[y_][x_] == wall or visited[y_][x_]: +            if grid[y_][x_] == wall or (y_, x_) in visited: -            if cnt + 1 > cost: -                cost = cnt + 1 -            visited[y_][x_] = True -    return cost +            visited[(y_, x_)] = cnt + 1 +    return max(visited.values()) -cand = {2: [], 3: [], 4: []} +starts = [] -        wl = 0 +        cnt = 0 -                wl += 1 -        if wl > 1: -            cand[wl].append((y, x)) +                cnt += 1 +        if cnt > 1: +            starts.append((y, x)) -starts = [] -for i in [4, 3, 2]: -    if cand[i] != []: -        starts = cand[i] -        break -for start in starts: -    cnt = bfs(grid, start, wall, h, w) -    if cnt > ans: -        ans = cnt +for y, x in starts: +    tmp = bfs(grid, y, x, wall) +    if ans < tmp: +        ans = tmp