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 sys
import copy
sys.setrecursionlimit(10**7)

h,w = map(int,input().split())
s = [list(input()) for i in range(h)]
count = 0

def dfs(x,y,gx,gy,c):
    global count
    global ans
    if not(0 <= x < h) or not(0 <= y < w) or c[x][y] == "#":
        return
    else:
        count += 1
    if x == gx and y == gy:
        ans.append(count)
        #print(count)
        return count
    c[x][y] = "#"
    dfs(x+1,y, gx, gy,c)
    dfs(x-1,y, gx, gy,c)
    dfs(x,y+1, gx, gy,c)
    dfs(x,y-1, gx, gy,c)

sx, sy = 0, 0
for i in range(h):
    for j in range(w):
        if s[i][j] == ".":
            sx, sy = i, j
            break
        
ans = []

for i in range(h):
    for j in range(w):
        if s[i][j] == ".":
            gx, gy = i, j
            c = copy.deepcopy(s)
            (dfs(sx, sy, gx, gy, c))
            count = 0

print(max(ans)-1)

h,w = map(int,input().split())
C = [list(input()) for i in range(h)]

queue = []
visited = [] 
for i in range(h):
    T = [0]*w
    visited.append(T) 
    
for i in range(h):
    for j in range(w):
        if C[i][j] == '.':
            queue.append([i,j])
            visited[i][j] = 1

            dy_dx = [[1,0],[0,1],[-1,0],[0,-1]]

            count = 0
            p = 0
            while len(queue) > 0:
              now = queue.pop(0)
              for i in range(4):
                y = now[0]+dy_dx[i][0]
                x = now[1]+dy_dx[i][1]
                if 0 <= y < h and 0 <= x < w:
                  if C[y][x] != '#' and visited[y][x] == 0:
                    visited[y][x] = visited[now[0]][now[1]]+1
                    queue.append([y,x])
                    if p == 0:
                      count += 1
                      p += 1
              p = 0
        for i in range(h):
          for j in range(w):
            ans = max(ans,visited[i][j])
        queue = []
        visited = [] 
        ans = 0
        for i in range(h):
            T = [0]*w
            visited.append(T) 
            
       
            
        
print(ans-1)

h,w = map(int,input().split())
C = [list(input()) for i in range(h)]

queue = []
visited = [] 
ans = []
for i in range(h):
    T = [0]*w
    visited.append(T) 
    
for i in range(h):
    for j in range(w):
        if C[i][j] == '.':
            queue.append([i,j])
            visited[i][j] = 1

            dy_dx = [[1,0],[0,1],[-1,0],[0,-1]]

            count = 0
            p = 0
            while len(queue) > 0:
              now = queue.pop(0)
              for i in range(4):
                y = now[0]+dy_dx[i][0]
                x = now[1]+dy_dx[i][1]
                if 0 <= y < h and 0 <= x < w:
                  if C[y][x] != '#' and visited[y][x] == 0:
                    visited[y][x] = 1
                    queue.append([y,x])
                    if p == 0:
                      count += 1
                      p += 1
              p = 0
        ans.append(count)
        queue = []
        visited = []  
        for i in range(h):
            T = [0]*w
            visited.append(T) 
        
print(max(ans))

h,w = map(int,input().split())
C = [list(input()) for i in range(h)]

queue = []
visited = [] 
ans = 0

visited = [[0 for i in range(w)] for i in range(h)]

for i in range(h):
    for j in range(w):
        if C[i][j] == '.':
            queue.append([i,j])
            visited[i][j] = 1

            dy_dx = [[1,0],[0,1],[-1,0],[0,-1]]

            while len(queue) > 0:
                now = queue.pop(0)
                for k in range(4):
                    y = now[0]+dy_dx[k][0]
                    x = now[1]+dy_dx[k][1]
                    if 0 <= y < h and 0 <= x < w:
                        if C[y][x] != '#' and visited[y][x] == 0:
                            visited[y][x] = visited[now[0]][now[1]]+1
                            queue.append([y,x])
                    
        for l in range(h):
            for m in range(w):
                ans = max(ans,visited[l][m])
            
        queue = []
        visited = [[0 for i in range(w)] for i in range(h)]
                        
        
print(ans-1)

Verdicts: Wrong Answer

Runtime Error

Runtime Error

Accepted