Buggy python submission (verdict: Time Limit Exceeded):
from copy import deepcopy
from collections import deque
from itertools import product

H, W = map(int, input().split())
S = [list(input()) for i in range(H)]

S = [[1000000 if s == '.' else '#' for s in row] for row in S]
ans = 0
for i, j in product(range(H), range(W)):
    if S[i][j] == '#':
        continue
    d = deepcopy(S)
    d[i][j] = 0
    v = [[False] * W for i in range(H)]
    q = deque([(i, j)])
    while len(q) > 0:
        x, y = q.popleft()
        v[x][y] = True
        if 0 <= x - 1 and S[x - 1][y] != '#':
            d[x - 1][y] = min(d[x][y] + 1, d[x - 1][y])
            if not v[x - 1][y]:
                q.append((x - 1, y))
        if x + 1 < H and S[x + 1][y] != '#':
            d[x + 1][y] = min(d[x][y] + 1, d[x + 1][y])
            if not v[x + 1][y]:
                q.append((x + 1, y))
        if 0 <= y - 1 and S[x][y - 1] != '#':
            d[x][y - 1] = min(d[x][y] + 1, d[x][y - 1])
            if not v[x][y - 1]:
                q.append((x, y - 1))
        if y + 1 < W and S[x][y + 1] != '#':
            d[x][y + 1] = min(d[x][y] + 1, d[x][y + 1])
            if not v[x][y + 1]:
                q.append((x, y + 1))
    for x, y in product(range(H), range(W)):
        if v[x][y]:
            ans = max(d[x][y], ans)
print(ans)


Fixed submission:
from collections import deque
from itertools import product

INF = 1001001001
dr = [(1, 0), (-1, 0), (0, 1), (0, -1)]

H, W = map(int, input().split())
S = [list(input()) for i in range(H)]

ans = 0
for i, j in product(range(H), range(W)):
    if S[i][j] == '#':
        continue
    d = [[INF] * W for i in range(H)]
    d[i][j] = 0
    q = deque([(i, j)])
    while len(q) > 0:
        x, y = q.popleft()
        for dx, dy in dr:
            nx, ny = x + dx, y + dy
            if 0 <= nx < H and 0 <= ny < W and S[nx][ny] != '#' and d[nx][ny] == INF:
                d[nx][ny] = d[x][y] + 1
                ans = max(d[nx][ny], ans)
                q.append((nx, ny))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from copy import deepcopy
from collections import deque
from itertools import product

H, W = map(int, input().split())
S = [list(input()) for i in range(H)]

S = [[1000000 if s == '.' else '#' for s in row] for row in S]
ans = 0
for i, j in product(range(H), range(W)):
    if S[i][j] == '#':
        continue
    d = deepcopy(S)
    d[i][j] = 0
    v = [[False] * W for i in range(H)]
    q = deque([(i, j)])
    while len(q) > 0:
        x, y = q.popleft()
        v[x][y] = True
        if 0 <= x - 1 and S[x - 1][y] != '#':
            d[x - 1][y] = min(d[x][y] + 1, d[x - 1][y])
            if not v[x - 1][y]:
                q.append((x - 1, y))
        if x + 1 < H and S[x + 1][y] != '#':
            d[x + 1][y] = min(d[x][y] + 1, d[x + 1][y])
            if not v[x + 1][y]:
                q.append((x + 1, y))
        if 0 <= y - 1 and S[x][y - 1] != '#':
            d[x][y - 1] = min(d[x][y] + 1, d[x][y - 1])
            if not v[x][y - 1]:
                q.append((x, y - 1))
        if y + 1 < W and S[x][y + 1] != '#':
            d[x][y + 1] = min(d[x][y] + 1, d[x][y + 1])
            if not v[x][y + 1]:
                q.append((x, y + 1))
    for x, y in product(range(H), range(W)):
        if v[x][y]:
            ans = max(d[x][y], ans)
print(ans)


Fixed submission:
from collections import deque
from itertools import product

INF = 1001001001
dr = [(1, 0), (-1, 0), (0, 1), (0, -1)]

H, W = map(int, input().split())
S = [list(input()) for i in range(H)]

ans = 0
for i, j in product(range(H), range(W)):
    if S[i][j] == '#':
        continue
    d = [[INF] * W for i in range(H)]
    d[i][j] = 0
    q = deque([(i, j)])
    while len(q) > 0:
        x, y = q.popleft()
        for dx, dy in dr:
            nx, ny = x + dx, y + dy
            if 0 <= nx < H and 0 <= ny < W and S[nx][ny] != '#' and d[nx][ny] == INF:
                d[nx][ny] = d[x][y] + 1
                ans = max(d[nx][ny], ans)
                q.append((nx, ny))
print(ans)