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())
S = [list(map(str,input().split())) for _ in range(H)]
dhw = [[0,1],[0,-1],[1,0],[-1,0]]

ans = 0
for h in range(H):
  for w in range(W):
    d = deque([h,w])
    maze = [[-1] * W for _ in range(H)]
    if S[h][w] == '#':
      continue
    maze[h][w] = 0
    while d:
      ht , hw = d.popleft()
      for dh, dw in dhw:
        nh = ht + dh
        nw = wt + dw
        if 0 <= nh < H and 0 <= nw < W:
          if maze[nh][nw] == -1 and S[nh][nw] == '.':
            d.append([nh,nw])
            maze[nh][nw] = maze[ht][wt] + 1
    ans = max(ans,max(maze))
print(ans)

from intertools import chain
from collections import deque
H, W = map(int,input().split())
S = [list(input()) for _ in range(H)]
dhw = [[0,1],[0,-1],[1,0],[-1,0]]

ans = 0
for h in range(H):
  for w in range(W):
    d = deque([h,w])
    maze = [[-1] * W for _ in range(H)]
    if S[h][w] == '#':
      continue
    maze[h][w] = 0
    while d:
      ht , wt = d.popleft()
      for dh, dw in dhw:
        nh = ht + dh
        nw = wt + dw
        if not(0 <= nh < H and 0 <= nw < W):
          continue
        S[nh][nw] == '#':
          continue
        if maze[nh][nw] == -1:
          d.append([nh,nw])
          maze[nh][nw] = maze[ht][wt] + 1
    ans = max(ans,max[max(l) for l in maze])

print(ans)


from collections import deque
H, W = map(int,input().split())
S = [list(input()) for _ in range(H)]
dhw = [[0,1],[0,-1],[1,0],[-1,0]]

ans = 0
for h in range(H):
  for w in range(W):
    d = deque([h,w])
    maze = [[-1] * W for _ in range(H)]
    if S[h][w] == '#':
      continue
    maze[h][w] = 0
    while d:
      ht , wt = d.popleft()
      for dh, dw in dhw:
        nh = ht + dh
        nw = wt + dw
        if not(0 <= nh < H and 0 <= nw < W):
          continue
        S[nh][nw] == '#':
          continue
        if maze[nh][nw] == -1:
          d.append([nh,nw])
          maze[nh][nw] = maze[ht][wt] + 1
    ans = max(ans,max[max(l) for l in maze])

print(ans)


from intertools import chain
from collections import deque
H, W = map(int,input().split())
S = [list(input()) for _ in range(H)]
dhw = [[0,1],[0,-1],[1,0],[-1,0]]

ans = 0
for h in range(H):
  for w in range(W):
    d = deque([h,w])
    maze = [[-1] * W for _ in range(H)]
    if S[h][w] == '#':
      continue
    maze[h][w] = 0
    while d:
      ht , wt = d.popleft()
      for dh, dw in dhw:
        nh = ht + dh
        nw = wt + dw
        if 0 <= nh < H and 0 <= nw < W:
          if maze[nh][nw] == -1 and S[nh][nw] == '.':
            d.append([nh,nw])
            maze[nh][nw] = maze[ht][wt] + 1
    ans = max(ans,max(chain(*maze))
print(ans)


from collections import deque
H, W = map(int,input().split())
S = [list(map(str,input().split())) for _ in range(H)]
dhw = [[0,1],[0,-1],[1,0],[-1,0]]

ans = 0
for h in range(H):
  for w in range(W):
    d = deque([h,w])
    maze = [[-1] * W for _ in range(H)]
    if S[h][w] == '#':
      continue
    maze[h][w] = 0
    while d:
      ht , hw = d.popleft()
      for dh, dw in dhw:
        nh = ht + dh
        nw = wt + dw
        if 0 <= nh < H and 0 <= nw < W:
          if maze[nh][nw] == -1 and S[nh][nw] == '.':
            d.append([nh,nw])
            maze[nh][nw] = maze[ht][wt] + 1
    ans = max(ans,max(maze))
print(ans)


from collections import deque

def main():
    H, W = map(int, input().split())
    maze = [list(input()) for _ in range(H)]
    directions = [[0, 1], [0, -1], [1, 0], [-1, 0]]

    ans = 0
    for h in range(H):
        for w in range(W):
            if maze[h][w] == '#':
                continue

            goal_dists = [[-1] * W for _ in range(H)]
            goal_dists[h][w] = 0
            d = deque([[h, w]])

            while d:
                ht, wt = d.popleft()
                for dh, dw in directions:
                    if not (0 <= ht+dh < H and 0 <= wt+dw < W):
                        continue
                    if maze[ht+dh][wt+dw] == '#':
                        continue
                    if goal_dists[ht+dh][wt+dw] == -1:
                        goal_dists[ht+dh][wt+dw] = goal_dists[ht][wt] + 1
                        d.append([ht+dh, wt+dw])
            
            ans = max(ans, max([max(l) for l in goal_dists]))

    print(ans)

if __name__ == "__main__":
    main()


Verdicts: Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Accepted