Problem p02803 (python)

Fixed submission:
h,w = map(int,input().split())
sl = []
for _ in range(h):
   s = list(input())
   sl.append(s)

G = [[] for i in range(h*w)]
for i in range(h):
    for j in range(w):
        if sl[i][j] == "#":
            continue
        a = i*w + j
        if j != w - 1:
            if sl[i][j+1] != "#":
                b = i*w + j + 1
                G[a].append(b)
        if j != 0:
            if sl[i][j-1] != "#":
                b = i*w + j - 1
                G[a].append(b)
        if i != 0:
            if sl[i-1][j] != "#":
                b = i*w + j - w
                G[a].append(b)
        if i != h - 1:
            if sl[i+1][j] != "#":
                b = i*w + j + w
                G[a].append(b)

# G[v]: 頂点vに隣接する頂点list
# N: 頂点数

ans = 0
from collections import deque
for i in range(h*w):
    if sl[i//w][i%w] == "#":
        continue
    dist = [-1]*(w*h)
    que = deque([i])
    dist[i] = 0
    while que:
        v = que.popleft()
        d = dist[v]
        for p in G[v]:
            if dist[p] > -1:
                continue
            dist[p] = d + 1
            que.append(p)
    ans = max(ans, max(dist))
    
print(ans)

Buggy submission (verdict: Runtime Error):
h,w = map(int,input().split())
sl = []
for _ in range(h):
   s = list(input())
   sl.append(s)

G = [[] for i in range(h*w)]
for i in range(h):
    for j in range(w):
        if sl[i][j] == "#":
            continue
        a = i*h + j
        if j != w - 1:
            if sl[i][j+1] != "#":
                b = i*h + j + 1
                G[a].append(b)
        if j != 0:
            if sl[i][j-1] != "#":
                b = i*h + j - 1
                G[a].append(b)
        if i != 0:
            if sl[i-1][j] != "#":
                b = i*h + j - h
                G[a].append(b)
        if i != h - 1:
            if sl[i+1][j] != "#":
                b = i*h + j + h
                G[a].append(b)

# G[v]: 頂点vに隣接する頂点list
# N: 頂点数

ans = 0
from collections import deque
for i in range(h*w):
    if sl[i//w][i%w] == "#":
        continue
    dist = [-1]*(w*h)
    que = deque([i])
    dist[i] = 0
    while que:
        v = que.popleft()
        d = dist[v]
        for p in G[v]:
            if dist[p] > -1:
                continue
            dist[p] = d + 1
            que.append(p)
    ans = max(ans, max(dist))
    
print(ans)

Buggy submission (verdict: Runtime Error):
h,w = map(int,input().split())
sl = []
for _ in range(h):
   s = list(input())
   sl.append(s)

G = [[] for i in range(h*w)]
for i in range(h):
    for j in range(w):
        if sl[i][j] == "#":
            continue
        a = i*h + j
        if j != w - 1:
            if sl[i][j+1] != "#":
                b = i*h + j + 1
                G[a].append(b)
        if j != 0:
            if sl[i][j-1] != "#":
                b = i*h + j - 1
                G[a].append(b)
        if i != 0:
            if sl[i-1][j] != "#":
                b = i*h + j - h
                G[a].append(b)
        if i != h - 1:
            if sl[i+1][j] != "#":
                b = i*h + j + h
                G[a].append(b)

# G[v]: 頂点vに隣接する頂点list
# N: 頂点数

ans = 0
from collections import deque
for i in range(h*w):
    dist = [-1]*(w*h)
    que = deque([i])
    dist[i] = 0
    while que:
        v = que.popleft()
        d = dist[v]
        for p in G[v]:
            if dist[p] > -1:
                continue
            dist[p] = d + 1
            que.append(p)
    ans = max(ans, max(dist))
    


print(ans)