Problem p02803 (python)

Fixed submission:
# ABC 151 C Maze Master
from collections import deque

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

dy = [0, 1, 0, -1]
dx = [-1, 0, 1, 0]

ans = 0
for si in range(H):
    for sj in range(W):
        if graph[si][sj] == "#": continue
        que = deque()
        que.append([si, sj])

        dist = [[-1 for _ in range(W)] for _ in range(H)]
        dist[si][sj] = 0
        
        while que:
            y, x = que.popleft()

            for dir in range(4):
                ny = y + dy[dir]
                nx = x + dx[dir]
                if ny < 0 or ny >= H or nx < 0 or nx >= W: continue
                if graph[ny][nx] == "#": continue
                if dist[ny][nx] != -1: continue

                if graph[ny][nx] == ".":
                    dist[ny][nx] = dist[y][x] + 1
                    que.append([ny, nx])

        for i in range(H):
            for j in range(W):
                if dist[i][j] == -1: continue
                ans = max(ans, dist[i][j])

print(ans)

Buggy submission (verdict: Runtime Error):
# ABC 151 C Maze Master
from collections import deque

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

dy = [0, 1, 0, -1]
dx = [-1, 0, 1, 0]

ans = 0
for si in range(H):
    for sj in range(W):
        if graph[si][sj] == "#": continue
        que = deque()
        que.append([si, sj])

        dist = [[-1 for _ in range(W)] for _ in range(H)]
        dist[si][sj] = 0
        
        while que:
            y, x = que.popleft()

            for dir in range(4):
                ny = y + dy[dir]
                nx = x + dx[dir]
                if ny < 0 or ny >= H or nx < 0 or nx >= W: continue
                if graph[ny][nx] == "#":continue

                if graph[ny][nx] == "." and dist[ny][nx] == -1:
                    dist[ny][nx] = dist[y][x] + 1
                    que.append([ny, nx])

    for i in range(H):
        for j in range(W):
            if dist[i][j] == -1: continue
            ans = max(ans, dist[i][j])

print(ans)

Buggy submission (verdict: Runtime Error):
# ABC 151 C Maze Master
from collections import deque

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

dy = [0, 1, 0, -1]
dx = [-1, 0, 1, 0]

ans = 0
for si in range(H):
    for sj in range(W):
        if graph[si][sj] == "#": continue
        que = deque()
        que.append([si, sj])

        dist = [[-1 for _ in range(W)] for _ in range(H)]
        dist[si][sj] = 0
        
        while que:
            y, x = que.popleft()

            for dir in range(4):
                ny = y + dy[dir]
                nx = x + dx[dir]
                if ny < 0 or ny >= H or nx < 0 or nx >= W: continue
                if graph[ny][nx] == "#":continue

                if graph[ny][nx] == ".":
                    dist[ny][nx] = dist[y][x] + 1
                    que.append([ny, nx])

    for i in range(H):
        for j in range(W):
            if dist[i][j] == -1: continue
            ans = max(ans, dist[i][j])

print(ans)

Buggy submission (verdict: Runtime Error):
# ABC 151 C Maze Master
import sys
from collections import deque

input = sys.stdin.readline
H, W = map(int, input().split())          
graph = [list(input()) for _ in range(H)]

dy = [0, 1, 0, -1]
dx = [-1, 0, 1, 0]

ans = 0
for si in range(H):
    for sj in range(W):
        if graph[si][sj] == "#": continue
        que = deque()
        que.append([si, sj])

        dist = [[-1 for _ in range(W)] for _ in range(H)]
        dist[si][sj] = 0
        
        while que:
            y, x = que.popleft()

            for dir in range(4):
                ny = y + dy[dir]
                nx = x + dx[dir]
                if ny < 0 or ny >= H or nx < 0 or nx >= W: continue
                if graph[ny][nx] == "#":continue

                if dist[ny][nx] != -1: continue

                if graph[ny][nx] == ".":
                    dist[ny][nx] = dist[y][x] + 1
                    que.append([ny, nx])

    for i in range(H):
        for j in range(W):
            if dist[i][j] == -1: continue
            ans = max(ans, dist[i][j])

print(ans)

Buggy submission (verdict: Runtime Error):
from collections import deque

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

dy = [0, 1, 0, -1]
dx = [-1, 0, 1, 0]

ans = 0
for si in range(H):
    for sj in range(W):
        if graph[si][sj] == "#": continue
        que = deque()
        que.append([si, sj])

        dist = [[-1 for _ in range(W)] for _ in range(H)]
        dist[si][sj] = 0
        
        while que:
            y, x = que.popleft()

            for dir in range(4):
                ny = y + dy[dir]
                nx = x + dx[dir]
                if ny < 0 or ny >= H or nx < 0 or nx >= W: continue
                elif graph[ny][nx] == "#":continue

                elif dist[ny][nx] != -1: continue

                elif graph[ny][nx] == ".":
                    dist[ny][nx] = dist[y][x] + 1
                    que.append([ny, nx])

    for i in range(H):
        for j in range(W):
            if dist[i][j] == -1: continue
            ans = max(ans, dist[i][j])

print(ans)