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 itertools
from collections import deque
import math
H, W = map(int,input().split())
#S=list(map(list,input().split()[6:]))
S = []
for i in range(H):
    a = list(map(str,input().split()))
    S.append(a)


start_or_goal = []
for i in range(H):
    a = S[i][0]
    b = S[i][W-1]
    c = []
    d = []
    if a == ".":
        c.append(i)
        c.append(0)
        if c not in start_or_goal:
            start_or_goal.append(c)
    if b == ".":
        d.append(i)
        d.append(W-1)
        if d not in start_or_goal:
            start_or_goal.append(d)
for i in range(W):
    a = S[0][i]
    b = S[H-1][i]
    c = []
    d = []
    if a == ".":
        c.append(0)
        c.append(i)
        if c not in start_or_goal:
            start_or_goal.append(c)
    if b == ".":
        d.append(H-1)
        d.append(i)
        if d not in start_or_goal:
            start_or_goal.append(d)

max_root = []
def bfs(h,w,sy,sx,gy,gx,maze):

    
    for y in range(h):
        for x in range(w):
            if maze[y][x]=='.':
                maze[y][x]=math.inf


    maze[sy-1][sx-1]=0
    que=deque([[sy-1,sx-1]])
    while que:
        y,x=que.popleft()
        for i,j in [(1,0),(0,1),(-1,0),(0,-1)]:
            nexty,nextx=y+i,x+j
            dist=maze[nexty][nextx]
            if dist!='#':
                if dist>maze[y][x]+1:
                    maze[nexty][nextx]=maze[y][x]+1
                    que.append([nexty,nextx])
    return maze[gy-1][gx-1]

s_to_g = list(itertools.combinations(start_or_goal,2))
for i in range(len(s_to_g)):
    sy = s_to_g[i][0][0]+1
    sx = s_to_g[i][0][1]+1
    gy = s_to_g[i][1][0]+1
    gx = s_to_g[i][1][1]+1
    ans = bfs(H,W,sy,sx,gy,gx,S)
    max_root.append(ans)
ans = max(max_root)
print(ans)

from collections import deque

def bfs(maze,dist,sy,sx):
    
    queue = deque([[sy,sx]])
    
    dist[sy][sx] = 0
    
    md = 0
    while queue:
        
        y,x = queue.popleft()
        for j, k in ([1,0],[-1,0],[0,1],[0,-1]):
            new_y, new_x = y+j, x+k
            
            if 0 <= new_y < H and 0 <= new_x < W:
                
                
                if maze[new_y][new_x] == "." and dist[new_y][new_x] == -1:
                    dist[new_y][new_x] = dist[y][x] + 1
                    queue.append([new_y, new_x])
                    md = max(dist[new_y][new_x],md)
    return md
H, W = map(int,input().split())
maze = [input() for i in range(H)]

ans = 0
for i in range(H):
    for j in range(W):
        dist = [[-1]*W for k in range(H)]
        
        if maze[i][j] != "#":
            a = bfs(maze,dist,i,j)
            ans = max(a,ans)
print(ans)

Verdicts: Runtime Error

Accepted