Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H, W = map(int, input().split())
field = ''
for i in range(H):
    field += input()
field += '#' * W

def bfs(s):
    noreach = [1] * (H * W)
    for _ in range(W):
        noreach.append(0)
    q = deque()
    q.append((s, 0))
    while q:
        p, c = q.popleft()
        noreach[p] = 0
        if (field[p - W] == '.') and noreach[p - W]:
            q.append((p - W, c + 1))
        if (field[p + W] == '.') and noreach[p + W]:
            q.append((p + W, c + 1))
        if (field[p - 1] == '.') and noreach[p - 1] and p % W != 0:
            q.append((p - 1, c + 1))
        if (field[p + 1] == '.') and noreach[p + 1] and p % W != W - 1:
            q.append((p + 1, c + 1))
    return c

ans = -1
for s in range(H * W):
    if field[s] == '.':
        ans = max(ans, bfs(s))
print(ans)


Fixed submission:
from collections import deque
H, W = map(int, input().split())
field = ''
for i in range(H):
    field += input()

def bfs(start):
    dist = [0] * (H * W)
    q = deque([start])
    while q:
        p = q.popleft()
        c = dist[p] + 1
        if p - W != start and p - W >= 0 and field[p - W] == '.' and dist[p - W] == 0:
            dist[p - W] = c
            q.append(p - W)
        if p + W != start and p + W < H * W and field[p + W] == '.' and dist[p + W] == 0:
            dist[p + W] = c
            q.append(p + W)
        if p - 1 != start and p - 1 >= 0 and field[p - 1] == '.' and dist[p - 1] == 0 and p % W != 0:
            dist[p - 1] = c
            q.append(p - 1)
        if p + 1 != start and p + 1 < H * W and field[p + 1] == '.' and dist[p + 1] == 0 and p % W != W - 1:
            dist[p + 1] = c
            q.append(p + 1)
    return dist[p]

ans = 0
for s in range(H * W):
    if field[s] == '.':
        ans = max(ans, bfs(s))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H, W = map(int, input().split())
field = ''
for i in range(H):
    field += input()
field += '#' * W

def bfs(s):
    noreach = [1] * (H * W)
    for _ in range(W):
        noreach.append(0)
    q = deque()
    q.append((s, 0))
    while q:
        p, c = q.popleft()
        noreach[p] = 0
        if (field[p - W] == '.') and noreach[p - W]:
            q.append((p - W, c + 1))
        if (field[p + W] == '.') and noreach[p + W]:
            q.append((p + W, c + 1))
        if (field[p - 1] == '.') and noreach[p - 1] and p % W != 0:
            q.append((p - 1, c + 1))
        if (field[p + 1] == '.') and noreach[p + 1] and p % W != W - 1:
            q.append((p + 1, c + 1))
    return c

ans = -1
for s in range(H * W):
    if field[s] == '.':
        ans = max(ans, bfs(s))
print(ans)


Fixed submission:
from collections import deque
H, W = map(int, input().split())
field = ''
for i in range(H):
    field += input()

def bfs(start):
    dist = [0] * (H * W)
    q = deque([start])
    while q:
        p = q.popleft()
        c = dist[p] + 1
        if p - W != start and p - W >= 0 and field[p - W] == '.' and dist[p - W] == 0:
            dist[p - W] = c
            q.append(p - W)
        if p + W != start and p + W < H * W and field[p + W] == '.' and dist[p + W] == 0:
            dist[p + W] = c
            q.append(p + W)
        if p - 1 != start and p - 1 >= 0 and field[p - 1] == '.' and dist[p - 1] == 0 and p % W != 0:
            dist[p - 1] = c
            q.append(p - 1)
        if p + 1 != start and p + 1 < H * W and field[p + 1] == '.' and dist[p + 1] == 0 and p % W != W - 1:
            dist[p + 1] = c
            q.append(p + 1)
    return dist[p]

ans = 0
for s in range(H * W):
    if field[s] == '.':
        ans = max(ans, bfs(s))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H, W = map(int, input().split())
field = [['#'] * (W + 2)]
for i in range(H):
    field.append(list('#' + input() + '#'))
field.append(['#'] * (W + 2))

def num_to_coo(p):
    h, w = divmod(p, W)
    h += 1
    w += 1
    return h, w

def bfs(s):
    noreach = [1] * (H * W)
    for _ in range(W):
        noreach.append(0)
    q = deque()
    q.append((s, 0))
    while q:
        p, c = q.popleft()
        noreach[p] = 0
        h, w = num_to_coo(p)
        if (field[h - 1][w] == '.') and noreach[p - W]:
            q.append((p - W, c + 1))
        if (field[h + 1][w] == '.') and noreach[p + W]:
            q.append((p + W, c + 1))
        if (field[h][w - 1] == '.') and noreach[p - 1]:
            q.append((p - 1, c + 1))
        if (field[h][w + 1] == '.') and noreach[p + 1]:
            q.append((p + 1, c + 1))
    return c

ans = -1
for s in range(H * W):
    h, w = num_to_coo(s)
    if field[h][w] == '.':
        ans = max(ans, bfs(s))
print(ans)


Fixed submission:
from collections import deque
H, W = map(int, input().split())
field = ''
for i in range(H):
    field += input()

def bfs(start):
    dist = [0] * (H * W)
    q = deque([start])
    while q:
        p = q.popleft()
        c = dist[p] + 1
        if p - W != start and p - W >= 0 and field[p - W] == '.' and dist[p - W] == 0:
            dist[p - W] = c
            q.append(p - W)
        if p + W != start and p + W < H * W and field[p + W] == '.' and dist[p + W] == 0:
            dist[p + W] = c
            q.append(p + W)
        if p - 1 != start and p - 1 >= 0 and field[p - 1] == '.' and dist[p - 1] == 0 and p % W != 0:
            dist[p - 1] = c
            q.append(p - 1)
        if p + 1 != start and p + 1 < H * W and field[p + 1] == '.' and dist[p + 1] == 0 and p % W != W - 1:
            dist[p + 1] = c
            q.append(p + 1)
    return dist[p]

ans = 0
for s in range(H * W):
    if field[s] == '.':
        ans = max(ans, bfs(s))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
H, W = map(int, input().split())
field = [['#'] * (W + 2)]
for i in range(H):
    field.append(list('#' + input() + '#'))
field.append(['#'] * (W + 2))

def num_to_coo(p):
    h, w = divmod(p, W)
    h += 1
    w += 1
    return h, w

def bfs(s):
    cost = [-1] * (H * W)
    q = deque()
    q.append((s, 0))
    while q:
        p, c = q.popleft()
        cost[p] = c
        h, w = num_to_coo(p)
        if (field[h - 1][w] == '.') and p - W >= 0 and (cost[p - W] == -1):
            q.append((p - W, c + 1))
        if (field[h + 1][w] == '.') and p + W < H * W and (cost[p + W] == -1):
            q.append((p + W, c + 1))
        if (field[h][w - 1] == '.') and p - 1 >= 0 and (cost[p - 1] == -1):
            q.append((p - 1, c + 1))
        if (field[h][w + 1] == '.') and p + 1 < H * W and (cost[p + 1] == -1):
            q.append((p + 1, c + 1))
    return c

ans = -1
for s in range(H * W):
    h, w = num_to_coo(s)
    if field[h][w] == '.':
        ans = max(ans, bfs(s))
print(ans)


Fixed submission:
from collections import deque
H, W = map(int, input().split())
field = ''
for i in range(H):
    field += input()

def bfs(start):
    dist = [0] * (H * W)
    q = deque([start])
    while q:
        p = q.popleft()
        c = dist[p] + 1
        if p - W != start and p - W >= 0 and field[p - W] == '.' and dist[p - W] == 0:
            dist[p - W] = c
            q.append(p - W)
        if p + W != start and p + W < H * W and field[p + W] == '.' and dist[p + W] == 0:
            dist[p + W] = c
            q.append(p + W)
        if p - 1 != start and p - 1 >= 0 and field[p - 1] == '.' and dist[p - 1] == 0 and p % W != 0:
            dist[p - 1] = c
            q.append(p - 1)
        if p + 1 != start and p + 1 < H * W and field[p + 1] == '.' and dist[p + 1] == 0 and p % W != W - 1:
            dist[p + 1] = c
            q.append(p + 1)
    return dist[p]

ans = 0
for s in range(H * W):
    if field[s] == '.':
        ans = max(ans, bfs(s))
print(ans)