Problem: Takahashi has a maze, which is a grid of H \times W squares with H horizontal rows and W vertical columns.  The square at the i-th row from the top and the j-th column is a "wall" square if S_{ij} is `#`, and a "road" square if S_{ij} is `.`.  From a road square, you can move to a horizontally or vertically adjacent road square.  You cannot move out of the maze, move to a wall square, or move diagonally.  Takahashi will choose a starting square and a goal square, which can be any road squares, and give the maze to Aoki.  Aoki will then travel from the starting square to the goal square, in the minimum number of moves required.  In this situation, find the maximum possible number of moves Aoki has to make.

Submission attempts (in order):
from collections import deque

def bfs(grid, start):
    H, W = len(maze), len(maze[0])
    distance = [[-1] * W for _ in range(H)]
    queue = deque([start])
    distance[start[0]][start[1]] = 0
    while queue:
        x, y = queue.popleft()
 
        for i, j in ((1, 0), (-1, 0), (0, 1), (0, -1)):
            new_y, new_x = y + j, x + i
            if 0 <= new_y < W and 0 <= new_x < H:
                if grid[new_x][new_y] == '.' and distance[new_x][new_y] == -1:
                    distance[new_x][new_y] = distance[x][y] + 1
                    queue.append((new_x, new_y))
    return max(map(max, distance))

h, w = map(int, input().split())
maze = [input() for _ in range(h)]


res = -1
for i in range(h):
    for j in range(w):
        if(maze[i][j] == '#'): continue
        tmpmaze = copy.deepcopy(maze)
        steps = bfs(tmpmaze, [i, j])
        if(steps > res):
            res = steps
print(res)

from collections import deque

def bfs(grid, start):
    H, W = len(grid), len(grid[0])
    distance = [[-1] * W for _ in range(H)]
    queue = deque([start])
    distance[start[0]][start[1]] = 0
    while queue:
        x, y = queue.popleft()
 
        for i, j in ((1, 0), (-1, 0), (0, 1), (0, -1)):
            new_y, new_x = y + j, x + i
            if 0 <= new_y < W and 0 <= new_x < H:
                if grid[new_x][new_y] == '.' and distance[new_x][new_y] == -1:
                    distance[new_x][new_y] = distance[x][y] + 1
                    queue.append((new_x, new_y))
    return max(map(max, distance))

h, w = map(int, input().split())
maze = [input() for _ in range(h)]


res = -1
for i in range(h):
    for j in range(w):
        if(maze[i][j] == '#'): continue
        tmpmaze = copy.deepcopy(maze)
        steps = bfs(tmpmaze, [i, j])
        if(steps > res):
            res = steps
print(res)

from collections import deque
h, w = map(int, input().split())
s = [input() for _ in range(h)]

INF = 1001001001
di = [-1, 0, 1, 0]
dj = [0, -1, 0, 1]

def update(i, j, x):
    global dist, s, q
    if(dist[i][j] != INF): return
    dist[i][j] = x
    q.append([i, j])

res = 0
for si in range(h):
    for sj in range(w):
        if(s[si][sj] == '#'): continue
        
        dist = [[INF for _ in range(w)] for _ in range(h)]
        q = deque()
        update(si, sj, 0)
        
        while(q):
            i, j = q.popleft()
            
            for idir, jdir in zip(di, dj):
                ni = i + idir
                nj = j + jdir
                
                if(ni<0 or ni>=h or nj<0 or nj>=w): continue
                if(s[ni][nj] == '#'): continue
                
                update(ni, nj, dist[i][j]+1)
        
        for i in range(h):
            for j in range(w):
                if(dist[i][j]==INF): continue
                res = max(res, dist[i][j])

print(res)

Verdicts: Runtime Error

Runtime Error

Accepted