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 sys
from collections import deque

H,W=map(int,input().split())
maze=[input() for i in range(H)]

def bfs(sy,sx):
    queue = deque([[sy,sx]])
    log = [[0 for i in range(W)] for j in range(H)]
    while queue:
        p = queue.popleft()

        for i,j in [1,0],[-1,0],[0,1],[0,-1]:
            next_p = [p[0]+i,p[1]+j]
            
            if 0 <= next_p[0] < H and 0 <= next_p[1] < W:
                if next_p[0] == sy and next_p[1] == sx:
                    continue

                if maze[next_p[0]][next_p[1]] == '.'\
                    and log[next_p[0]][next_p[1]] == 0:
                    log[next_p[0]][next_p[1]] = log[p[0]][p[1]] + 1
                    queue.append([next_p[0],next_p[1]])
                    print(log)
    
    return log

for i in range(H):
    for j in range(W):
        if maze[i][j] == '.':
            tmp_i = i
            tmp_j = j
            break


tmplog = bfs(tmp_i,tmp_j)
max_v = 0

for i in range(H):
    for j in range(W):
        if tmplog[i][j] > max_v:
            max_v = tmplog[i][j]
            s_y = i
            s_x = j

finlog = bfs(s_y,s_x)

for i in range(H):
    for j in range(W):
        if finlog[i][j] > max_v:
            max_v = finlog[i][j]

print(max_v)

import sys
from collections import deque

H,W=map(int,input().split())
maze=[input() for i in range(H)]

def bfs(sy,sx):
    queue = deque([[sy,sx]])
    log = [[0 for i in range(W)] for j in range(H)]
    while queue:
        p = queue.popleft()

        for i,j in [1,0],[-1,0],[0,1],[0,-1]:
            next_p = [p[0]+i,p[1]+j]
            
            if 0 <= next_p[0] < H and 0 <= next_p[1] < W:
                if next_p[0] == sy and next_p[1] == sx:
                    continue

                if maze[next_p[0]][next_p[1]] == '.'\
                    and log[next_p[0]][next_p[1]] == 0:
                    log[next_p[0]][next_p[1]] = log[p[0]][p[1]] + 1
                    queue.append([next_p[0],next_p[1]])
    
    return log

for i in range(H):
    for j in range(W):
        if maze[i][j] == '.':
            tmp_i = i
            tmp_j = j
            break

tmplog = bfs(tmp_i,tmp_j)
max_v = 0

for i in range(H):
    for j in range(W):
        if tmplog[i][j] > max_v:
            max_v = tmplog[i][j]
            tmp_i = i
            tmp_j = j

tmplog = bfs(tmp_i,tmp_j)
max_v = 0

for i in range(H):
    for j in range(W):
        if tmplog[i][j] > max_v:
            max_v = tmplog[i][j]
            s_y = i
            s_x = j

finlog = bfs(s_y,s_x)

for i in range(H):
    for j in range(W):
        if finlog[i][j] > max_v:
            max_v = finlog[i][j]

print(max_v)

Verdicts: Wrong Answer

Accepted