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

graph = [[float('inf')]*H*W for _ in range(H*W)]
nv = [(-1, 0), (1, 0), (0, -1), (0, 1)]

for h in range(H):
    for w in range(W):
        if S[h][w]=='.':
            for nx, ny in nv:
                if H <= h+nx or h+nx < 0 or W <= w+ny or w+ny < 0:
                    continue
                if S[h+nx][w+ny]=='.':
                    graph[W*h+w][W*(h+nx)+(w+ny)] = 1

for k in range(H*W):
    for i in range(H*W):
        for j in range(H*W):
            graph[i][i]=0
            graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j])
ans = 0
for i in range(H*W-1):
    for j in range(i+1, H*W):
        if graph[i][j] != float('inf'):
            ans = max(ans, graph[i][j])
print(ans)

Fixed submission:
from collections import deque
H, W = map(int, input().split())
area = ['#']*(W+2)
for i in range(H):
    area += ['#']+list(input())+['#']
area += ['#']*(W+2)
H+=2
W+=2
move = [-1, 1, W, -W]
ans = 0
for i in range(H):
    for j in range(W):
        st = W*i+j
        if area[st]=='#': continue
        dq = deque([])
        dist = [-1]*(H*W)
        dq.append(st)
        dist[st] = 0
        while dq:
            now = dq.popleft()
            for dx in move:
                nxt = now+dx
                if dist[nxt]>=0 or area[nxt]=='#': continue
                dist[nxt] = dist[now]+1
                ans = max(ans, dist[nxt])
                dq.append(nxt)
print(ans)

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

graph = [[float('inf')]*H*W for _ in range(H*W)]
for i in range(H*W):
    graph[i][i]=0
nv = [(-1, 0), (1, 0), (0, -1), (0, 1)]

for h in range(H):
    for w in range(W):
        if S[h][w]=='.':
            for nx, ny in nv:
                if H <= h+nx or h+nx < 0 or W <= w+ny or w+ny < 0:
                    continue
                if S[h+nx][w+ny]=='.':
                    graph[W*h+w][W*(h+nx)+(w+ny)] = 1
                    graph[W*(h+nx)+(w+ny)][W*h+w] = 1

for k in range(H*W):
    for i in range(H*W):
        for j in range(H*W):
            graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j])
ans = 0
for i in range(H*W-1):
    for j in range(i+1, H*W):
        if graph[i][j] != float('inf'):
            ans = max(ans, graph[i][j])
print(ans)

Fixed submission:
from collections import deque
H, W = map(int, input().split())
area = ['#']*(W+2)
for i in range(H):
    area += ['#']+list(input())+['#']
area += ['#']*(W+2)
H+=2
W+=2
move = [-1, 1, W, -W]
ans = 0
for i in range(H):
    for j in range(W):
        st = W*i+j
        if area[st]=='#': continue
        dq = deque([])
        dist = [-1]*(H*W)
        dq.append(st)
        dist[st] = 0
        while dq:
            now = dq.popleft()
            for dx in move:
                nxt = now+dx
                if dist[nxt]>=0 or area[nxt]=='#': continue
                dist[nxt] = dist[now]+1
                ans = max(ans, dist[nxt])
                dq.append(nxt)
print(ans)

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

graph = [[float('inf')]*H*W for _ in range(H*W)]
for i in range(H*W):
    graph[i][i]=0
nv = [(-1, 0), (1, 0), (0, -1), (0, 1)]

for h in range(H):
    for w in range(W):
        if S[h][w]=='.':
            for nx, ny in nv:
                if H <= h+nx or h+nx < 0 or W <= w+ny or w+ny < 0:
                    continue
                if S[h+nx][w+ny]=='.':
                    graph[W*h+w][W*(h+nx)+(w+ny)] = 1
                    graph[W*(h+nx)+(w+ny)][W*h+w] = 1

for k in range(H*W):
    for i in range(H*W):
        for j in range(H*W):
            graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j])
ans = 0
for i in range(H*W-1):
    for j in range(i+1, H*W):
        if graph[i][j] != float('inf'):
            ans = max(ans, graph[i][j])
print(ans)

Fixed submission:
from collections import deque
H, W = map(int, input().split())
area = ['#']*(W+2)
for i in range(H):
    area += ['#']+list(input())+['#']
area += ['#']*(W+2)
H+=2
W+=2
move = [-1, 1, W, -W]
ans = 0
for i in range(H):
    for j in range(W):
        st = W*i+j
        if area[st]=='#': continue
        dq = deque([])
        dist = [-1]*(H*W)
        dq.append(st)
        dist[st] = 0
        while dq:
            now = dq.popleft()
            for dx in move:
                nxt = now+dx
                if dist[nxt]>=0 or area[nxt]=='#': continue
                dist[nxt] = dist[now]+1
                ans = max(ans, dist[nxt])
                dq.append(nxt)
print(ans)