Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
S = [list('#' * (W + 2))]
for _ in range(H):
    S.append(list('#' + input() + '#'))
S.append(list('#' * (W + 2)))

dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]

inf = float('inf')

N = W * H
l = [[inf for _ in range(N)] for _ in range(N)]

for w in range(1, W + 1):
    for h in range(1, H + 1):
        if S[h][w] != '#':
            l[(h - 1) * W + w - 1][(h - 1) * W + w - 1] = 0
            for x, y in zip(dx, dy):
                if S[h + y][w + x] != '#':
                    l[(h - 1) * W + w - 1][(h + y - 1) * W + w + x - 1] = 1

for k in range(N):
    for i in range(N):
        for j in range(N):
            l[i][j] = min(l[i][j], l[i][k] + l[k][j])

ans = -1

for i in range(N):
    for j in range(N):
        k = l[i][j]
        if k != inf:
            ans = max(ans, k)

print(ans)


Fixed submission:
H, W = map(int, input().split())
S = []
for _ in range(H):
    S.append(list(input()))

dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]

inf = float('inf')

import queue

ans = 0

for h in range(H):
    for w in range(W):
        if S[h][w] != '#':
            q = queue.Queue()
            d = [[inf for _ in range(W)] for _ in range(H)]
            # 距離を保存
            q.put([h, w])
            d[h][w] = 0
            # スタートをキューに、距離を0に
            while not q.empty():
                p = q.get()
                # if p == g:
                #     break
                for i in range(4):
                    nx = p[1] + dx[i]
                    ny = p[0] + dy[i]
                    if nx >= 0 and nx < W and ny >= 0 and ny < H and S[ny][nx] != '#' and d[ny][nx] == inf:
                        q.put([ny, nx])
                        d[ny][nx] = d[p[0]][p[1]] + 1
            for hh in range(H):
                for ww in range(W):
                    if d[hh][ww] != inf:
                        ans = max(ans, d[hh][ww])

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
S = [list('#' * (W + 2))]
for _ in range(H):
    S.append(list('#' + input() + '#'))
S.append(list('#' * (W + 2)))

dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]

inf = float('inf')

N = W * H
l = [[inf for _ in range(N)] for _ in range(N)]

for w in range(1, W + 1):
    for h in range(1, H + 1):
        if S[h][w] != '#':
            l[(h - 1) * W + w - 1][(h - 1) * W + w - 1] = 0
            for x, y in zip(dx, dy):
                if S[h + y][w + x] != '#':
                    l[(h - 1) * W + w - 1][(h + y - 1) * W + w + x - 1] = 1

for k in range(N):
    for i in range(N):
        for j in range(N):
            l[i][j] = min(l[i][j], l[i][k] + l[k][j])

ans = -1

for i in range(N):
    for j in range(N):
        k = l[i][j]
        if k != inf:
            ans = max(ans, k)

print(ans)


Fixed submission:
H, W = map(int, input().split())
S = []
for _ in range(H):
    S.append(list(input()))

dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]

inf = float('inf')

import queue

ans = 0

for h in range(H):
    for w in range(W):
        if S[h][w] != '#':
            q = queue.Queue()
            d = [[inf for _ in range(W)] for _ in range(H)]
            # 距離を保存
            q.put([h, w])
            d[h][w] = 0
            # スタートをキューに、距離を0に
            while not q.empty():
                p = q.get()
                # if p == g:
                #     break
                for i in range(4):
                    nx = p[1] + dx[i]
                    ny = p[0] + dy[i]
                    if nx >= 0 and nx < W and ny >= 0 and ny < H and S[ny][nx] != '#' and d[ny][nx] == inf:
                        q.put([ny, nx])
                        d[ny][nx] = d[p[0]][p[1]] + 1
            for hh in range(H):
                for ww in range(W):
                    if d[hh][ww] != inf:
                        ans = max(ans, d[hh][ww])

print(ans)