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):
import copy

def agent(maze,ori):
    global H
    global W

    hi=ori[0]
    wi=ori[1]
    root=[]
    if hi > 0:
        root.append([hi-1,wi])
    if hi < H-1:
        root.append([hi+1,wi])
    if wi > 0:
        root.append([hi,wi-1])
    if wi < W-1:
        root.append([hi,wi+1])
    
    for h,w in root:
        if maze[h][w]==".":
            maze[h][w]=maze[hi][wi]+1
            agent(maze,[h,w])
        elif maze[h][w]=="#":
            pass
        else:
            if maze[h][w] > maze[hi][wi]+1:
                maze[h][w]=maze[hi][wi]+1
                agent(maze,[h,w])


H,W=map(int,input().split())

maze=[]
for h in range(H):
    maze_w=list(input())
    lis=[]
    for e in maze_w:
        if e =="#":
            lis.append(-1)
        else:
            lis.append(e)

    maze.append(lis)

maze_ori=copy.deepcopy(maze)
maxd=0

for h in range(H):
    for w  in range(W):
        maze=copy.deepcopy(maze_ori)
        if maze[h][w]==".":
            maze[h][w]=0
            agent(maze,[h,w])
            maxd=max(maxd,max(max(maze)))
            print(maze)
        else:
            pass


print(maxd)


import copy
H,W=map(int,input().split())

def BFS(h,w,maze):


maze=[]
maze.append("#"*W)
for h in range(H):
    maze_e=input()
    line=[]

    maze.append("#"+line+"#")

maze.append("#"*W)


maze_ori=copy.deepcopy(maze)

maxd=0

for h in range(1,H+1):
    for w in range(1,W+1):
        maze=copy.deepcopy(maze_ori)
        if maze[h][w]="#":
            continue
        else:
            next_flag=True


        direction=[[-1,0],[0][-1],[1,0],[0][1]])
        distance=0
        maze[h][w]=distance

        next_list=[h,w]
        while next_flag is True:
            p_list=next_list[]
            next_list=[]
            distance+=1
            next_flag=False
            for p in p_list:
                for d in direction:
                    i=p[0]+d[0]
                    j=p[1]+d[1]

                    if maze[i,j]==".":
                        maze[i,j]=distance
                        next_list.append([i,j])
                        next_flag=True
            
        maxd=max(maxd,distance-1)

print(maxd)

import copy
H,W=map(int,input().split())

maze=[]
maze.append(["#"]*(W+2))
for h in range(H):
    line=list(input())

    maze.append(["#"]+line+["#"])

maze.append(["#"]*(W+2))


maze_ori=copy.deepcopy(maze)

maxd=0

for h in range(1,H+1):
    for w in range(1,W+1):
        maze=copy.deepcopy(maze_ori)
        if maze[h][w]=="#":
            continue
        else:
            next_flag=True


        direction=[[-1,0],[0,-1],[1,0],[0,1]]
        distance=0
        maze[h][w]=distance

        next_list=[]
        next_list.append([h,w])
        while next_flag is True:
            p_list=next_list
            next_list=[]
            distance+=1
            next_flag=False
            for p in p_list:
                for d in direction:
                    i=p[0]+d[0]
                    j=p[1]+d[1]

                    if maze[i][j]==".":
                        maze[i][j]=distance
                        next_list.append([i,j])
                        next_flag=True
            
        maxd=max(maxd,distance-1)

print(maxd)


Verdicts: Wrong Answer

Runtime Error

Accepted