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

grid = []

for i in range(h):
    s = list(input().rstrip())
    grid.append(s)
dxdy = [[0, 1], [1,0], [-1, 0], [0, -1]]
visited = [[0]*w for _ in range(h)]
dist = [[0]*w for _ in range(h)]

for i in range(h):
    for j in range(w):
        if grid[i][j]==".":
            y = i
            x = j
            break



dist[y][x] = 0
max_dist = [0, []]
def max1(a,b):
    if a[0]>b[0]:
        return a
    else:
        return b
def dfs(x, y):
    visited[y][x]=1
    for dydx in dxdy:
        if (x+dydx[0]<w and y+dydx[1]<h) and (x+dydx[0]>-1 and y+dydx[1]>-1) and not visited[y+dydx[1]][x+dydx[0]]:
            if grid[y+dydx[1]][x+dydx[0]]==".":
                dist[y+dydx[1]][x+dydx[0]] = dist[y][x]+1
                dfs(x+dydx[0], y+dydx[1])
dfs(x, y)

for i in range(h):
    for j in range(w):
        max_dist = max1(max_dist, [dist[i][j], i,j])

visited = [[0]*w for _ in range(h)]
dist = [[0]*w for _ in range(h)]
dfs(max_dist[2],max_dist[1])


for i in range(h):
    for j in range(w):
        max_dist = max1(max_dist, [dist[i][j], i,j])
print(max_dist[0])


from collections import deque

h,w = map(int, input().split())
grid = []
for i in range(h):
    s = list(input().rstrip())
    grid.append(s)
dydx = [[0, 1], [1,0], [-1, 0], [0, -1]]

def find_longest_path(xy):
    visited = [[0]*w for _ in range(h)]
    dist = [[0]*w for _ in range(h)]
    que = deque()
    que.append(xy)
    while que:
        v = que.popleft()
        x,y = v[0],v[1]
        visited[y][x]=1
        for i in dydx:
            dx = i[0]
            dy = i[1]
            if ((x+dx<w and y+dy<h) and ((x+dx)>-1)and(y+dy)>-1) and not visited[y+dy][x+dx]:
                if(grid[y+dy][x+dx]=="."):
                    que.appendleft([x+dx, y+dy])
                    dist[y+dy][x+dx]=dist[y][x]+1
                    u,k = x+dx, y+dy
                    r = dist[y+dy][x+dx]



    return r, [u,k]

def main():
    for i in range(h):
        for j in range(w):
            if grid[i][j]==".":
                x,y= j,i
    r,v=find_longest_path([x,y])
    r,res = find_longest_path(v)
    print(r)

if __name__ == '__main__':
    main()


from collections import deque

h,w = map(int, input().split())
grid = []
for i in range(h):
    s = list(input().rstrip())
    grid.append(s)
dydx = [[0, 1], [1,0], [-1, 0], [0, -1]]

def find_longest_path(xy):
    visited = [[0]*w for _ in range(h)]
    dist = [[0]*w for _ in range(h)]
    visited[xy[1]][xy[0]]=1
    que = deque()
    que.append(xy)
    while que:
        v = que.popleft()
        x,y = v[0],v[1]
        for i in dydx:
            dx = i[0]
            dy = i[1]
            if ((x+dx<w and y+dy<h) and ((x+dx)>-1)and(y+dy)>-1) and not visited[y+dy][x+dx]:
                if(grid[y+dy][x+dx]=="."):
                    que.append([x+dx, y+dy])
                    visited[y+dy][x+dx]=1
                    dist[y+dy][x+dx]=dist[y][x]+1
                    u,k = x+dx, y+dy
                    r = dist[y+dy][x+dx]

    return r

def main():
    k=0
    for i in range(h):
        for j in range(w):
            if grid[i][j]==".":
                k = max(k, find_longest_path([j,i]))
    print(k)

if __name__ == '__main__':
    main()


Verdicts: Wrong Answer

Wrong Answer

Accepted