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):
#!/usr/bin/env python3
#abc151_d
import sys
from collections import deque
mv = ((1,0),(-1,0),(0,1),(0,-1))
h,w = map(int,input().split())
board = [["#"]*(w+2) for _ in range(h+2)]
for i in range(h):
    s = input()
    for j in range(w):
        board[i+1][j+1] = str(s[j])
cur_max = 0 
#print(board[0][0] == ".")
for i in range(h+2):
    for j in range(w+2):
        #print(i,j,board[i][j])
        tmp =  board[i][j]
        if tmp == ".":
            visit = deque()
            start_pos = (i,j)
            visit.append(start_pos)
            cnt = [[-1]*(w+2) for _ in range(h+2)] 
            cnt[i][j] = 0 
            while visit:
                point = visit.popleft()
                for i in mv:
                    pos = point + i
                    if str(board[pos[0]][pos[1]]) == ".":
                        if cnt[pos[0]][pos[1]] == -1: #unvisited
                            cnt[pos[0]][pos[1]] = cnt[point[0]][point[1]] + 1
                            if cnt[pos[0]][pos[1]] > cur_max:
                                cur_max = cnt[pos[0]][pos[1]]
print(cur_max)                    



#!/usr/bin/env python3
import sys
from collections import deque
mv = ((1,0),(-1,0),(0,1),(0,-1))
h,w = map(int,input().split())
board = [["#"]*(w+2) for _ in range(h+2)]
for i in range(h):
    s = input()
    for j in range(w):
        board[i+1][j+1] = str(s[j])
cur_max = 0 
#print(board[0][0] == ".")
for i in range(h+2):
    for j in range(w+2):
        #print(i,j,board[i][j])
        if str(board[i][j]) == "#":
            visit = deque()
            start_pos = (i,j)
            visit.append(start_pos)
            cnt = [[-1]*(w+2) for _ in range(h+2)] 
            cnt[i][j] = 0 
            while visit:
                point = visit.popleft()
                for i in mv:
                    pos = point + i
                    if str(board[pos[0]][pos[1]]) == ".":
                        if cnt[pos[0]][pos[1]] == -1: #unvisited
                            cnt[pos[0]][pos[1]] = cnt[point[0]][point[1]] + 1
                            if cnt[pos[0]][pos[1]] > cur_max:
                                cur_max = cnt[pos[0]][pos[1]]
print(cur_max)                    



#!/usr/bin/env python3
#abc151_d
import sys
from collections import deque
mv = ((1,0),(-1,0),(0,1),(0,-1))
h,w = map(int,input().split())
board = [["#"]*(w+2) for _ in range(h+2)]
for i in range(h):
    s = input()
    for j in range(w):
        board[i+1][j+1] = str(s[j])
cur_max = 0 
#print(board[0][0] == ".")

    #print(board[i])
for i in range(h+2):
    for j in range(w+2):
        #print(i,j,board[i][j])
        tmp =  board[i][j]
        if tmp == ".":
            #print("yes",i,j)
            visit = deque()
            start_pos = (i,j)
            visit.append(start_pos)
            cnt = [[-1]*(w+2) for _ in range(h+2)] 
            cnt[i][j] = 0 
            while visit:
                point = visit.popleft()
                #print(point[0], point[1])
                for k in mv:
                    pos = (point[0]+k[0],point[1]+k[1])
                    #print(pos)
                    if str(board[pos[0]][pos[1]]) == ".":
                        if cnt[pos[0]][pos[1]] == -1: #unvisited
                            #print(i,j,"yes")
                            cnt[pos[0]][pos[1]] = cnt[point[0]][point[1]] + 1
                            visit.append((pos[0],pos[1]))
                            if cnt[pos[0]][pos[1]] > cur_max:
                                cur_max = cnt[pos[0]][pos[1]]
print(cur_max)                    


Verdicts: Runtime Error

Runtime Error

Accepted