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

h,w = map(int, input().split())
PATH = "."
WALL = "#"
z=0

maze=list()
for a in range(h):
 mazew=list()
 mazeh = input()
 for b in range(w):
  mazew.append(mazeh[b])
 maze.append(mazew)
 print(maze)
for c in range(h):
 for d in range(w):
   
   
  distance_visited = list()
  for l in range(h):
   distance_visited.append([-1] * w)
   
  queue = deque([[c, d]])
  distance_visited[c][d] = 0
  while True:
   x,y = queue.popleft()
      
   for i,j in ([1, 0], [-1, 0], [0, 1], [0, -1]):
    new_x, new_y = x + i, y + j
       
    if new_x>=0 and new_x<=h-1 and new_y>=0 and new_y<=w-1:
     if maze[new_x][new_y] != WALL and distance_visited[new_x][new_y] == -1:
               
      distance_visited[new_x][new_y] = distance_visited[x][y] + 1
      queue.append([new_x, new_y])
      if distance_visited[new_x][new_y]>z:
       z=distance_visited[new_x][new_y]
   if queue==deque([]):
    break
print(z)

from collections import deque

h,w = map(int, input().split())
PATH = "."
WALL = "#"
z=0

maze=list()
for a in range(h):
 mazew=list()
 mazeh = input()
 for b in range(w):
  mazew.append(mazeh[b])
 maze.append(mazew)
 print(maze)

for c in range(h):
 for d in range(w):
  for e in range(h):
   for f in range(w):
   
   
    if maze[e][f] == WALL:
     continue
    else:
     distance_visited = list()
     for l in range(h):
      distance_visited.append([-1] * w)
    
     queue = deque([[c, d]])
     distance_visited[c][d] = 0
     goal_x, goal_y=e,f
     while True:
      x,y = queue.popleft()
      
      if x == e and y==f:
       goal_x, goal_y = x, y
       break
        
      for i,j in ([1, 0], [-1, 0], [0, 1], [0, -1]):
       new_x, new_y = x + i, y + j
        
       if new_x>=0 and new_x<=h-1 and new_y>=0 and new_y<=w-1:
        if maze[new_x][new_y] != WALL and distance_visited[new_x][new_y] == -1:
                
         distance_visited[new_x][new_y] = distance_visited[x][y] + 1
         queue.append([new_x, new_y])
      if z<distance_visited[goal_x][goal_y]:
       z=distance_visited[goal_x][goal_y]
print(z)

from collections import deque

h,w = map(int, input().split())
PATH = "."
WALL = "#"
z=0

maze=list()
for a in range(h):
 mazew=list()
 mazeh = input()
 for b in range(w):
  mazew.append(mazeh[b])
 maze.append(mazew)
for c in range(h):
 for d in range(w):
   
   
  if maze[c][d]==WALL:
   continue
  else:
   distance_visited = list()
   for l in range(h):
    distance_visited.append([-1] * w)
   
   queue = deque([[c, d]])
   distance_visited[c][d] = 0
   while True:
    x,y = queue.popleft()
      
    for i,j in ([1, 0], [-1, 0], [0, 1], [0, -1]):
     new_x, new_y = x + i, y + j
       
     if new_x>=0 and new_x<=h-1 and new_y>=0 and new_y<=w-1:
      if maze[new_x][new_y] != WALL and distance_visited[new_x][new_y] == -1:
               
       distance_visited[new_x][new_y] = distance_visited[x][y] + 1
       queue.append([new_x, new_y])
       if distance_visited[new_x][new_y]>z:
        z=distance_visited[new_x][new_y]
    if queue==deque([]):
     break
print(z)

Verdicts: Wrong Answer

Runtime Error

Accepted