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

def bfs(sy,sx):
    m_dis=0
    move=[[1,0],[0,1],[-1,0],[0,-1]]
    visited=[[-1]*yoko for i in range(tate)]
    queue=deque()
    queue.append([sy,sx]) 
    visited[sy][sx]=0 
    while queue:
        y,x=queue.popleft()
        for dy,dx in move:
            my,mx=y+dy,x+dx
            if not(0<=my<tate) or not(0<=mx<yoko):
                continue
            if maze[my][mx]=="." and visited[my][mx]==-1:
                queue.append([my,mx])
                visited[my][mx]=visited[y][x]+1 
                m_dis+=1
    return m_dis

tate,yoko=map(int,input().split())
maze=[]
for i in range(tate):
    line=list(input())
    maze.append(line)
ans=0
for y in range(tate):
    for x in range(yoko):
        ans=max(ans,bfs(y,x))
print(ans)

from collections import deque

def bfs(sy,sx):
    m_dis=0
    mv_ok=False
    move=[[1,0],[0,1],[-1,0],[0,-1]]
    visited=[[-1]*yoko for i in range(tate)]
    queue=deque()
    queue.append([sy,sx]) 
    visited[sy][sx]=0 
    while queue:
        mv_ok=False
        y,x=queue.popleft()
        for dy,dx in move:
            my,mx=y+dy,x+dx
            if not(0<=my<tate) or not(0<=mx<yoko):
                continue
            if maze[my][mx]=="." and visited[my][mx]==-1:
                queue.append([my,mx])
                visited[my][mx]=visited[y][x]+1 
                mv_ok=True
        if mv_ok:
            m_dis+=1
    return m_dis

tate,yoko=map(int,input().split())
maze=[]
for i in range(tate):
    line=list(input())
    maze.append(line)
ans=0
for y in range(tate):
    for x in range(yoko):
        ans=max(ans,bfs(y,x))
print(ans)

from collections import deque

def bfs(sy,sx):
    m_dis=0
    
    move=[[1,0],[0,1],[-1,0],[0,-1]]
    visited=[[-1]*yoko for i in range(tate)]
    queue=deque()
    queue.append([sy,sx]) 
    visited[sy][sx]=0 
    while queue:
        y,x=queue.popleft()
        for dy,dx in move:
            my,mx=y+dy,x+dx
            if not(0<=my<tate) or not(0<=mx<yoko):
                continue
            if maze[my][mx]=="." and visited[my][mx]==-1:
                queue.append([my,mx])
                visited[my][mx]=visited[y][x]+1 
                m_dis=visited[my][mx]
        
    return m_dis

tate,yoko=map(int,input().split())
maze=[]
for i in range(tate):
    line=list(input())
    maze.append(line)
ans=0
for y in range(tate):
    for x in range(yoko):
        if maze[y][x]==".":
            ans=max(ans,bfs(y,x))
print(ans)

Verdicts: Wrong Answer

Wrong Answer

Accepted