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
import numpy as np


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


def bfs(initial):
    
    dist = [[-1] * W for _ in range(H)]
    dist[initial[0]][initial[1]] = 0
    frontier = deque()
    frontier.append((initial, None))
    # tuple
    explored = {initial}
    while len(frontier):
        locations = []
        current_node = frontier.popleft()
        current_state = current_node[0]
        x, y = current_state
        if (x + 1 < H) and (maze[x + 1][y] != '#'):
            locations.append((x + 1, y))
            dist[x + 1][y] = dist[x][y] + 1
        
        if (x - 1 >= 0) and (maze[x - 1][y] != '#'):
            locations.append((x - 1, y))
            dist[x - 1][y] = dist[x][y] + 1
            
        if (y + 1 < W) and (maze[x][y + 1] != '#'):
            locations.append((x, y + 1))
            dist[x][y + 1] = dist[x][y] + 1
        
        if (y - 1 >= 0) and (maze[x][y - 1] != '#'):
            locations.append((x, y - 1))
            dist[x][y-1] = dist[x][y] + 1
        for child in locations:
            if str(child) in [str(i) for i in explored]:
                continue
            explored.add(child)
            frontier.append((child, current_node))
    return np.max(dist)


ans = 0
for x in range(H):
    for y in range(W):
        if maze[x][y] == '.':
            solution = bfs((x, y))
            if ans < solution:
                ans = solution

print(ans)

from collections import deque

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


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


ans = 0
for i in range(H):
    for j in range(W):
        if maze[i][j] == '#':
            continue
        solution = bfs((i, j))
        if ans < solution:
            ans = solution

print(ans)


Verdicts: Wrong Answer

Accepted