import sys

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines

from collections import deque 

INF = 100000

dir = ((1, 0), (-1, 0), (0, 1), (0, -1)) 

def main():

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

    maze = []

    for _ in range(H):

        s = readline().rstrip().decode()

        maze.append(s) 

    

    

    def bfs(si, sj):

        dq = deque()

        dq.append((si, sj))        

        dist = [[INF for _ in range(W)] for _ in range(H)]

        dist[si][sj] = 0

        while len(dq)>0:

            i, j = dq.popleft()

            for di, dj in dir:

                nxi = i+di

                nxj = j+dj

                if (nxi<0) or (nxj<0) or (nxi>=H) or (nxj>=W):

                    continue

                if maze[nxi][nxj] == '#':

                    continue

                if dist[nxi][nxj] != INF:

                    continue

                dist[nxi][nxj] = dist[i][j]+1

                dq.append((nxi, nxj))

        res = 0

        for i in range(H):

            for j in range(W):

                if dist[i][j] == INF:

                    continue

                res = max(dist[i][j], res)        

        return res



    ans = 0

    for si in range(H):

        for sj in range(W):

            if maze[si][sj] == '#':

                continue

            ans = max(bfs(si, sj), ans)



    print(ans)



if __name__ == '__main__':

    main()

import sys

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines



from collections import deque 

dir = ((-1, 0), (1, 0), (0, -1), (0, 1))

def main():

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

    grid = []

    for _ in range(h):

        grid.append(readline().rstrip().decode())



    INF = int(1e9)

    mx = 0

    for si in range(h):

        for sj in range(w):

            if grid[si][sj] == '#':

                continue            

            dq = deque()

            dq.append((si, sj))

            dist = [[-1 for _ in range(w)] for _ in range(h)]

            dist[si][sj] = 0

            while len(dq) > 0:

                ni, nj = dq.popleft()

                for d in dir:

                    x, y = ni+d[0], nj+d[1]

                    if (x < 0) or (y < 0) or (x >= h) or (y >= w):

                        continue

                    if grid[x][y] == '#':

                        continue

                    if dist[x][y] != -1:

                        continue 

                    dist[x][y] = dist[ni][nj]+1 

                    dq.append((x, y))



            for i in range(h):

                for j in range(w):

                    if dist[i][j] == -1: 

                        continue

                    mx = max(mx, dist[i][j])

    print(mx)

if __name__ == '__main__':

    main()
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import deque

INF = 100000
dir = ((1, 0), (-1, 0), (0, 1), (0, -1))


def main():
    H, W = list(map(int, readline().split()))
    maze = []
    for _ in range(H):
        s = readline().rstrip().decode()
        maze.append(s)

    def bfs(si, sj):
        dq = deque()
        dq.append((si, sj))
        dist = [[INF for _ in range(W)] for _ in range(H)]
        dist[si][sj] = 0
        while len(dq) > 0:
            i, j = dq.popleft()
            for di, dj in dir:
                nxi = i + di
                nxj = j + dj
                if (nxi < 0) or (nxj < 0) or (nxi >= H) or (nxj >= W):
                    continue
                if maze[nxi][nxj] == "#":
                    continue
                if dist[nxi][nxj] != INF:
                    continue
                dist[nxi][nxj] = dist[i][j] + 1
                dq.append((nxi, nxj))
        res = 0
        for i in range(H):
            for j in range(W):
                if dist[i][j] == INF:
                    continue
                res = max(dist[i][j], res)
        return res

    ans = 0
    for si in range(H):
        for sj in range(W):
            if maze[si][sj] == "#":
                continue
            ans = max(bfs(si, sj), ans)
    print(ans)


if __name__ == "__main__":
    main()

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import deque

dir = ((-1, 0), (1, 0), (0, -1), (0, 1))


def main():
    h, w = list(map(int, readline().split()))
    grid = []
    for _ in range(h):
        grid.append(readline().rstrip().decode())
    INF = int(1e9)
    mx = 0
    for si in range(h):
        for sj in range(w):
            if grid[si][sj] == "#":
                continue
            dq = deque()
            dq.append((si, sj))
            dist = [[-1 for _ in range(w)] for _ in range(h)]
            dist[si][sj] = 0
            while len(dq) > 0:
                ni, nj = dq.popleft()
                for d in dir:
                    x, y = ni + d[0], nj + d[1]
                    if (x < 0) or (y < 0) or (x >= h) or (y >= w):
                        continue
                    if grid[x][y] == "#":
                        continue
                    if dist[x][y] != -1:
                        continue
                    dist[x][y] = dist[ni][nj] + 1
                    dq.append((x, y))
            for i in range(h):
                for j in range(w):
                    if dist[i][j] == -1:
                        continue
                    mx = max(mx, dist[i][j])
    print(mx)


if __name__ == "__main__":
    main()

-INF = 100000 -dir = ((1, 0), (-1, 0), (0, 1), (0, -1)) +dir = ((-1, 0), (1, 0), (0, -1), (0, 1)) -    H, W = list(map(int, readline().split())) -    maze = [] -    for _ in range(H): -        s = readline().rstrip().decode() -        maze.append(s) - -    def bfs(si, sj): -        dq = deque() -        dq.append((si, sj)) -        dist = [[INF for _ in range(W)] for _ in range(H)] -        dist[si][sj] = 0 -        while len(dq) > 0: -            i, j = dq.popleft() -            for di, dj in dir: -                nxi = i + di -                nxj = j + dj -                if (nxi < 0) or (nxj < 0) or (nxi >= H) or (nxj >= W): -                    continue -                if maze[nxi][nxj] == "#": -                    continue -                if dist[nxi][nxj] != INF: -                    continue -                dist[nxi][nxj] = dist[i][j] + 1 -                dq.append((nxi, nxj)) -        res = 0 -        for i in range(H): -            for j in range(W): -                if dist[i][j] == INF: -                    continue -                res = max(dist[i][j], res) -        return res - -    ans = 0 -    for si in range(H): -        for sj in range(W): -            if maze[si][sj] == "#": +    h, w = list(map(int, readline().split())) +    grid = [] +    for _ in range(h): +        grid.append(readline().rstrip().decode()) +    INF = int(1e9) +    mx = 0 +    for si in range(h): +        for sj in range(w): +            if grid[si][sj] == "#": -            ans = max(bfs(si, sj), ans) -    print(ans) +            dq = deque() +            dq.append((si, sj)) +            dist = [[-1 for _ in range(w)] for _ in range(h)] +            dist[si][sj] = 0 +            while len(dq) > 0: +                ni, nj = dq.popleft() +                for d in dir: +                    x, y = ni + d[0], nj + d[1] +                    if (x < 0) or (y < 0) or (x >= h) or (y >= w): +                        continue +                    if grid[x][y] == "#": +                        continue +                    if dist[x][y] != -1: +                        continue +                    dist[x][y] = dist[ni][nj] + 1 +                    dq.append((x, y)) +            for i in range(h): +                for j in range(w): +                    if dist[i][j] == -1: +                        continue +                    mx = max(mx, dist[i][j]) +    print(mx)