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):
h, w = list(map(int, input().split()))

s = []
for i in range(h):
    line = input()
    s.append(line)

ans = []
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            continue
        d = [[1<<10 for x in range(w)] for y in range(h)]

        stack = []
        stack.append((i, j))
        d[i][j] = 0
        ma = 0
        while stack:
            y, x = stack.pop()
            if y > 0 and s[y-1][x] == '.' and d[y-1][x] > d[y][x]+1:
                d[y-1][x] = d[y][x] + 1
                stack.append((y-1, x))
            if x < w-1 and s[y][x+1] == '.' and d[y][x+1] > d[y][x]+1:
                d[y][x+1] = d[y][x] + 1
                stack.append((y, x+1))
            if y < h-1 and s[y+1][x] == '.' and d[y+1][x] > d[y][x]+1:
                d[y+1][x] = d[y][x] + 1
                stack.append((y+1, x))
            if x > 0 and s[y][x-1] == '.' and d[y][x-1] > d[y][x]+1:
                d[y][x-1] = d[y][x] + 1
                stack.append((y, x-1))
            if d[y][x] > ma:
                ma = d[y][x]
        ans.append(ma)
ans.sort()
print(ans[-1])


    h, w = list(map(int, input().split()))
     
    s = []
    for i in range(h):
        line = input()
        s.append(line)
     
    ans = []
    for i in range(h):
        for j in range(w):
            if s[i][j] == '#':
                continue
            d = [[1<<10 for x in range(w)] for y in range(h)]
     
            stack = []
            stack.append((i, j))
            d[i][j] = 0
            while stack:
                y, x = stack.pop()
                if y > 0 and s[y-1][x] == '.' and d[y-1][x] > d[y][x]+1:
                    d[y-1][x] = d[y][x] + 1
                    stack.append((y-1, x))
                if x < w-1 and s[y][x+1] == '.' and d[y][x+1] > d[y][x]+1:
                    d[y][x+1] = d[y][x] + 1
                    stack.append((y, x+1))
                if y < h-1 and s[y+1][x] == '.' and d[y+1][x] > d[y][x]+1:
                    d[y+1][x] = d[y][x] + 1
                    stack.append((y+1, x))
                if x > 0 and s[y][x-1] == '.' and d[y][x-1] > d[y][x]+1:
                    d[y][x-1] = d[y][x] + 1
                    stack.append((y, x-1))
            ma = 0
            for y in range(h):
                for x in range(w):
                    if d[y][x] > ma:
                        ma = d[y][x]

            ans.append(ma)
    ans.sort()
    print(ans[-1])


h, w = list(map(int, input().split()))

s = []
for i in range(h):
    line = input()
    s.append(line)

ans = []
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            continue
        start = (i, j)
        d = [[1<<10 for x in range(w)] for y in range(h)]

        stack = []
        stack.append((start[0], start[1]))
        d[start[0]][start[1]] = 0
        ma = 0
        while stack:
            y, x = stack.pop()
            if y > 0 and s[y-1][x] == '.' and d[y-1][x] > d[y][x]+1:
                d[y-1][x] = d[y][x] + 1
                stack.append((y-1, x))
            if x < w-1 and s[y][x+1] == '.' and d[y][x+1] > d[y][x]+1:
                d[y][x+1] = d[y][x] + 1
                stack.append((y, x+1))
            if y < h-1 and s[y+1][x] == '.' and d[y+1][x] > d[y][x]+1:
                d[y+1][x] = d[y][x] + 1
                stack.append((y+1, x))
            if x > 0 and s[y][x-1] == '.' and d[y][x-1] > d[y][x]+1:
                d[y][x-1] = d[y][x] + 1
                stack.append((y, x-1))
            if d[y][x] > ma:
                ma = d[y][x]
        ans.append(ma)
ans.sort()
print(ans[-1])


import collections
h, w = list(map(int, input().split()))

s = []
for i in range(h):
    line = input()
    s.append(line)

ans = 0
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            continue
        d = [[False for x in range(w)] for y in range(h)]

        stack = collections.deque()
        stack.append((i, j, 0))
        d[i][j] = True
        while stack:
            y, x, score = stack.popleft()
            if y > 0 and s[y-1][x] == '.' and not d[y-1][x]:
                stack.append((y-1, x, score+1))
                d[y-1][x] = True
            if x < w-1 and s[y][x+1] == '.' and not d[y][x+1]:
                stack.append((y, x+1, score+1))
                d[y][x+1] = True
            if y < h-1 and s[y+1][x] == '.' and not d[y+1][x]:
                stack.append((y+1, x, score+1))
                d[y+1][x] = True
            if x > 0 and s[y][x-1] == '.' and not d[y][x-1]:
                stack.append((y, x-1, score+1))
                d[y][x-1] = True
            if score > ans:
                ans = score
print(ans)


Verdicts: Wrong Answer

Runtime Error

Wrong Answer

Accepted