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 queue


class Solution:

    def solve(self, H: int, W: int, S) -> int:
        INF = H*W+1
        dist = {}
        for i in range(H):
            for j in range(W):
                dist[(i, j)] = INF

        def bfs(__start):

            
            
            #     for j in range(W):
            
            #     print(s)
            # print("-----")

            goals = []
            goals.append(__start)
            dist[__start] = 0
            __nexts = queue.Queue()

            __nexts.put(__start)

            while not __nexts.empty():
                __next = __nexts.get()

                __is_node = True

                for delta in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
                    h = __next[0] + delta[0]
                    w = __next[1] + delta[1]
                    if h < 0 or h > H - 1:
                        continue

                    if w < 0 or w > W - 1:
                        continue

                    if S[h][w] != "#" and dist[(h, w)] == INF:
                        __nexts.put((h, w))
                        dist[(h, w)] = dist[__next] + 1
                        __is_node = False

                if __is_node:
                    goals.append(__next)

            return goals

        ans = 0
        for i in range(H):
            for j in range(W):

                # skip if (i,j) is not on the edge
                if 0 < i and i < H - 1 and 0 < j and j < W - 1:
                    continue

                # skip if (i,J) is a wall
                if S[i][j] == "#":
                    continue

                # skip if (i,j) is already visited
                if dist[(i, j)] < INF:
                    continue

                goals = bfs((i, j))

                dists = [dist[goal] for goal in goals]
                dists.sort()

                max_dist = 0
                if len(dists) > 1:
                    max_dist = dists[-1] + dists[-2]

                if ans < max_dist:
                    ans = max_dist

        return ans


if __name__ == '__main__':

    # standard input
    H, W = map(int, input().split())
    S = []
    for h in range(H):
        S.append(input())

    # solve
    solution = Solution()
    print(solution.solve(H, W, S))


import queue


class Solution:

    def solve(self, H: int, W: int, S) -> int:

        def onEdge(pos):
            i = pos[0]
            j = pos[1]
            if 0 < i and i < H - 1 and 0 < j and j < W - 1:
                return False
            else:
                return True

        def bfs(start):
            INF = H*W+1
            dist = {}
            for i in range(H):
                for j in range(W):
                    dist[(i, j)] = INF

            max_dist = 0

            dist[start] = 0
            nexts = queue.Queue()
            nexts.put(start)

            while not nexts.empty():
                __next = nexts.get()

                # if onEdge(__next):
                max_dist = max(dist[__next], max_dist)

                for delta in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
                    h = __next[0] + delta[0]
                    w = __next[1] + delta[1]

                    if h < 0 or h > H - 1 or w < 0 or w > W - 1:
                        continue

                    if S[h][w] != "#":
                        if dist[(h, w)] == INF:
                            nexts.put((h, w))
                            dist[(h, w)] = dist[__next] + 1

            return max_dist

        ans = 0
        for i in range(H):
            for j in range(W):

                # skip if (i,j) is not on the edge
                # if not onEdge((i, j)):
                #     continue

                # skip if (i,J) is a wall
                if S[i][j] == "#":
                    continue

                ans = max(bfs((i, j)), ans)

        return ans


if __name__ == '__main__':

    # standard input
    H, W = map(int, input().split())
    S = []
    for h in range(H):
        S.append(input())

    # solve
    solution = Solution()
    print(solution.solve(H, W, S))


Verdicts: Wrong Answer

Accepted