Buggy python submission (verdict: Time Limit Exceeded):
import collections

H, W = map(int, input().split(" "))
mp = []
for _ in range(H):
    mp.append(input().strip())


def bfs(sh, sw, mp):
    que = collections.deque()
    que.append((sh, sw, 0))
    dist = [[float('inf') for _ in range(W)] for _ in range(H)]

    while len(que) > 0:
        h, w, d = que.popleft()
        dist[h][w] = d
        for (dh, dw) in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
            if h + dh < 0 or w + dw < 0 or h + dh >= H or w + dw >= W:
                continue
            if mp[h + dh][w + dw] == '#' or dist[h + dh][w + dw] != float('inf'):
                continue
            que.append((h + dh, w + dw, d + 1))
    return dist


for h in range(H):
    for w in range(W):
        if mp[h][w] == '.':
            tmp_dist = bfs(h, w, mp)
            sh = h; sw = w;
            break

for h in range(H):
    for w in range(W):
        if mp[h][w] == '.' and tmp_dist[h][w] >= tmp_dist[sh][sw]:
            sh = h
            sw = w

dist = bfs(sh, sw, mp)
ans = 1
for h in range(H):
    for w in range(W):
        if mp[h][w] == '.':
            ans = max(ans, dist[h][w])
print(ans)

Fixed submission:
import collections

H, W = map(int, input().split(" "))
mp = []
for _ in range(H):
    mp.append(input().strip())


def bfs(sh, sw, mp):
    que = collections.deque()
    que.append((sh, sw))
    dist = [[-1 for _ in range(W)] for _ in range(H)]
    dist[sh][sw] = 0

    while que:
        h, w = que.popleft()
        for (dh, dw) in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
            if h + dh < 0 or w + dw < 0 or h + dh >= H or w + dw >= W:
                continue
            if mp[h + dh][w + dw] == '.' and dist[h + dh][w + dw] == -1:
                dist[h + dh][w + dw] = dist[h][w] + 1
                que.append((h + dh, w + dw))

    ans = 0
    for row in dist:
        ans = max(ans, max(row))
    return ans


ans = 0
for h in range(H):
    for w in range(W):
        if mp[h][w] == '.':
            ans = max(ans, bfs(h, w, mp))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import collections

H, W = map(int, input().split(" "))
mp = []
for _ in range(H):
    mp.append(input().strip())


def bfs(sh, sw, mp):
    que = collections.deque()
    que.append((sh, sw, 0))
    dist = [[float('inf') for _ in range(W)] for _ in range(H)]

    while len(que) > 0:
        h, w, d = que.popleft()
        dist[h][w] = d
        for (dh, dw) in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
            if h + dh < 0 or w + dw < 0 or h + dh >= H or w + dw >= W:
                continue
            if mp[h + dh][w + dw] == '#' or dist[h + dh][w + dw] != float('inf'):
                continue
            que.append((h + dh, w + dw, d + 1))
    return dist


for h in range(H):
    for w in range(W):
        if mp[h][w] == '.':
            tmp_dist = bfs(h, w, mp)
            sh = h; sw = w;
            break

for h in range(H):
    for w in range(W):
        if mp[h][w] == '.' and tmp_dist[h][w] >= tmp_dist[sh][sw]:
            sh = h
            sw = w

dist = bfs(sh, sw, mp)
ans = 1
for h in range(H):
    for w in range(W):
        if mp[h][w] == '.':
            ans = max(ans, dist[h][w])
print(ans)

Fixed submission:
import collections

H, W = map(int, input().split(" "))
mp = []
for _ in range(H):
    mp.append(input().strip())


def bfs(sh, sw, mp):
    que = collections.deque()
    que.append((sh, sw))
    dist = [[-1 for _ in range(W)] for _ in range(H)]
    dist[sh][sw] = 0

    while que:
        h, w = que.popleft()
        for (dh, dw) in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
            if h + dh < 0 or w + dw < 0 or h + dh >= H or w + dw >= W:
                continue
            if mp[h + dh][w + dw] == '.' and dist[h + dh][w + dw] == -1:
                dist[h + dh][w + dw] = dist[h][w] + 1
                que.append((h + dh, w + dw))

    ans = 0
    for row in dist:
        ans = max(ans, max(row))
    return ans


ans = 0
for h in range(H):
    for w in range(W):
        if mp[h][w] == '.':
            ans = max(ans, bfs(h, w, mp))
print(ans)

Buggy python submission (verdict: Runtime Error):
import collections

H, W = map(int, input().split(" "))
mp = []
for _ in range(H):
    mp.append(input().strip())


def bfs(sh, sw, mp):
    que = collections.deque()
    que.append((sh, sw, 0))
    dist = [[float('inf') for _ in range(W)] for _ in range(W)]

    while len(que) > 0:
        h, w, d = que.popleft()
        if dist[h][w] != float('inf'):
            continue
        dist[h][w] = d
        for (dh, dw) in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
            if h + dh < 0 or w + dw < 0 or h + dh >= H or w + dw >= W:
                continue
            if mp[h + dh][w + dw] == '#':
                continue
            que.append((h + dh, w + dw, d + 1))
    return dist


for h in range(H):
    for w in range(W):
        if mp[h][w] == '.':
            tmp_dist = bfs(h, w, mp)
            break

sh, sw = (h, w)
for h in range(H):
    for w in range(W):
        if mp[h][w] == '.' and tmp_dist[sh][sw] >= tmp_dist[h][w]:
            sh = h; sw = w
dist = bfs(sh, sw, mp)

ans = 0
for h in range(H):
    for w in range(W):
        if mp[h][w] == '.':
            ans = max(ans, dist[h][w])
print(ans)

Fixed submission:
import collections

H, W = map(int, input().split(" "))
mp = []
for _ in range(H):
    mp.append(input().strip())


def bfs(sh, sw, mp):
    que = collections.deque()
    que.append((sh, sw))
    dist = [[-1 for _ in range(W)] for _ in range(H)]
    dist[sh][sw] = 0

    while que:
        h, w = que.popleft()
        for (dh, dw) in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
            if h + dh < 0 or w + dw < 0 or h + dh >= H or w + dw >= W:
                continue
            if mp[h + dh][w + dw] == '.' and dist[h + dh][w + dw] == -1:
                dist[h + dh][w + dw] = dist[h][w] + 1
                que.append((h + dh, w + dw))

    ans = 0
    for row in dist:
        ans = max(ans, max(row))
    return ans


ans = 0
for h in range(H):
    for w in range(W):
        if mp[h][w] == '.':
            ans = max(ans, bfs(h, w, mp))
print(ans)