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
import sys
buf = sys.stdin.buffer

h, w = map(int, buf.readline().split())
map = ""
for _ in range(h):
    map += input() + "#"
#print(map)

n = h * (w + 1)
periods = [[] for _ in range(n)]
dir = [-w - 1, 1, -1, w + 1]
for i in range(n):
    if map[i] == "#":
        continue
    for d in dir:
        next_i = i + d
        if next_i < 0 or next_i >= n or map[next_i] == "#":
            continue
        periods[i].append(next_i)
#print(periods)

q = deque()
def bfs(pos):
    for next_i in periods[pos]:
        if con_cnt[next_i] == -1:
            con_cnt[next_i] = con_cnt[pos] + 1
            q.append(next_i)
        
        if q:
            bfs(q.popleft())

start_points = [i for i, j in enumerate(periods) if len(j) > 0]
ans = 0
for start_point in start_points:
    con_cnt = [-1] * n
    con_cnt[start_point] = 0
    bfs(start_point)
    ans = max(ans, max(con_cnt))
print(ans)

from collections import deque
import sys
buf = sys.stdin.buffer

h, w = map(int, buf.readline().split())
map = ""
for _ in range(h):
    map += input() + "#"
#print(map)

n = h * (w + 1)
periods = [[] for _ in range(n)]
dir = [-w - 1, 1, -1, w + 1]
for i in range(n):
    if map[i] == "#":
        continue
    for d in dir:
        next_i = i + d
        if next_i < 0 or next_i >= n or map[next_i] == "#":
            continue
        periods[i].append(next_i)
#print(periods)

q = deque()
def bfs(pos):
    for next_i in periods[pos]:
        if con_cnt[next_i] == -1:
            con_cnt[next_i] = con_cnt[pos] + 1
            q.append(next_i)
        
        if len(q) > 0:
            bfs(q.popleft())

start_points = [i for i, j in enumerate(periods) if len(j) > 0]
ans = 0
for start_point in start_points:
    con_cnt = [-1] * n
    con_cnt[start_point] = 0
    bfs(start_point)
    ans = max(ans, max(con_cnt))
print(ans)

from collections import deque
import sys
buf = sys.stdin.buffer

h, w = map(int, buf.readline().split())
map = ""
for _ in range(h):
    map += input() + "#"
#print(map)

n = h * (w + 1)
periods = [[] for _ in range(n)]
dir = [-w - 1, 1, -1, w + 1]
for i in range(n):
    if map[i] == "#":
        continue
    for d in dir:
        next_i = i + d
        if next_i < 0 or next_i >= n or map[next_i] == "#":
            continue
        periods[i].append(next_i)
#print(periods)

q = deque()
def bfs(now_i):
    for next_i in periods[now_i]:
        if con_cnt[next_i] == -1:
            con_cnt[next_i] = con_cnt[now_i] + 1
            q.append(next_i)
    
    if q:
        bfs(q.popleft())

start_points = [i for i, j in enumerate(periods) if len(j) > 0]
ans = 0
for start_point in start_points:
    con_cnt = [-1] * n
    con_cnt[start_point] = 0
    bfs(start_point)
    #print(con_cnt)
    ans = max(ans, max(con_cnt))

print(ans)


Verdicts: Wrong Answer

Wrong Answer

Accepted