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 numpy as np
 
 
H,W= [int(x) for x in input().split()]
n=[]
T=np.zeros((H,W),dtype=int)

for i in range(H):
  n=input()
  for j in range(W):
    if n[j]=="#": T[i][j]=1
S= np.zeros_like(T, dtype=int)
d=0
c=deque()

for i in range(H):
    for j in range(W):
         
        if T[i][j] ==1:continue

        c=deque()    
        s=0
        c+=[i,j]
        S[i][j]=1
        t=len(c)
        while t>0:
                s+=1
                t=len(c)
                try:
                    m = c.popleft()
                    n = c.popleft()
                except IndexError:
                    break
                #S[m][n]=s
                if m != H-1:
                    if T[m+1][n]==0 and S[m+1][n]==0:
                        c+=[m+1,n]
                        S[m+1][n] = S[m][n]+1
                if m != 0:
                    if T[m-1][n]==0 and S[m-1][n]==0:
                        c+=[m-1,n] 
                        S[m-1][n] = S[m][n]+1
                if n != W-1:
                    if T[m][n+1]==0 and S[m][n+1]==0:
                        c+=[m,n+1]
                        S[m][n+1] = S[m][n]+1
                if n != 0:
                    if T[m][n-1]==0 and S[m][n-1]==0:
                        c+=[m,n-1]
                        S[m][n-1] = S[m][n]+1
                        #c += (chk_route([c.popleft(),c.popleft()]))
                
        print(S)
        d = max(d,np.max(S))
        S= np.zeros_like(T, dtype=int)
        
print(d) 

from collections import deque
import numpy as np


H,W= [int(x) for x in input().split()]
n=[]
T=np.zeros((H,W),dtype=int)

for i in range(H):
  n=input()
  for j in range(W):
    if n[j]=="#": T[i][j]=1
S= np.zeros_like(T, dtype=int)
d=0
c=deque()

for i in range(H):
    for j in range(W):

        if T[i][j] == 1:continue

        c.append((i,j))
        S[i][j]=1

        while True:
                #print(c)
                try:
                    m,n = c.popleft()
                except IndexError:
                    #print("break")
                    break

                if m != H-1:
                    if T[m+1][n]==0 and S[m+1][n]==0:
                        c.append((m+1,n))
                        S[m+1][n] = S[m][n]+1
                if m != 0:
                    if T[m-1][n]==0 and S[m-1][n]==0:
                        c.append((m-1,n))
                        S[m-1][n] = S[m][n]+1
                if n != W-1:
                    if T[m][n+1]==0 and S[m][n+1]==0:
                        c.append((m,n+1))
                        S[m][n+1] = S[m][n]+1
                if n != 0:
                    if T[m][n-1]==0 and S[m][n-1]==0:
                        c.append((m,n-1))
                        S[m][n-1] = S[m][n]+1

        print(S)
        d = max(d,np.max(S))
        S = np.zeros_like(T, dtype=int)

print(d-1)


from collections import deque
import numpy as np


H,W= [int(x) for x in input().split()]
n=[]
T=np.zeros((H,W),dtype=int)

for i in range(H):
  n=input()
  for j in range(W):
    if n[j]=="#": T[i][j]=1
B=T.copy()
d=0
c=deque()
for i in range(H):
    for j in range(W):

        if T[i][j] ==1:continue
        s=0
        c.append((i,j))

        t=len(c)
        while t>0:
                s+=1
                t=len(c)
                for k in range(t):
                        m,n = c.popleft()
                        T[m][n]=s
                        if m != H-1:
                                if T[m+1][n]==0:c.append((m+1,n))
                        if m != 0:
                                if T[m-1][n]==0:c.append((m-1,n))
                        if n != W-1:
                                if T[m][n+1]==0:c.append((m,n+1))
                        if n != 0:
                                if T[m][n-1]==0:c.append((m,n-1))
                        #c += (chk_route([c.popleft(),c.popleft()]))

        print(T)
        d = max(d,s)
        T=B.copy()

print(d-2)


from collections import deque
import numpy as np
 
 
H,W= [int(x) for x in input().split()]
n=[]
T=np.zeros((H,W),dtype=int)

for i in range(H):
  n=input()
  for j in range(W):
    if n[j]=="#": T[i][j]=1
S= np.zeros_like(T, dtype=int)
d=0
c=deque()

for i in range(H):
    for j in range(W):
         
        if T[i][j] ==1:continue

        c+=[i,j]
        S[i][j]=1
        t=len(c)
        while t>0:
                t=len(c)
                #print(c)
                try:
                    m = c.popleft()
                    n = c.popleft()
                except IndexError:
                    break
                if m != H-1:
                    if T[m+1][n]==0 and S[m+1][n]==0:
                        c+=[m+1,n]
                        S[m+1][n] = S[m][n]+1
                if m != 0:
                    if T[m-1][n]==0 and S[m-1][n]==0:
                        c+=[m-1,n] 
                        S[m-1][n] = S[m][n]+1
                if n != W-1:
                    if T[m][n+1]==0 and S[m][n+1]==0:
                        c+=[m,n+1]
                        S[m][n+1] = S[m][n]+1
                if n != 0:
                    if T[m][n-1]==0 and S[m][n-1]==0:
                        c+=[m,n-1]
                        S[m][n-1] = S[m][n]+1
                
        print(S)
        d = max(d,np.max(S))
        S = np.zeros_like(T, dtype=int)
        
print(d-1) 

from collections import deque
import numpy as np
 
 
H,W= [int(x) for x in input().split()]
n=[]
T=np.zeros((H,W),dtype=int)

for i in range(H):
  n=input()
  for j in range(W):
    if n[j]=="#": T[i][j]=1
S= np.zeros_like(T, dtype=int)
d=0
c=deque()

for i in range(H):
    for j in range(W):
         
        if T[i][j] ==1:continue

        c=deque()    
        c+=[i,j]
        S[i][j]=1
        t=len(c)
        while t>0:
                t=len(c)
                try:
                    m = c.popleft()
                    n = c.popleft()
                except IndexError:
                    break
                if m != H-1:
                    if T[m+1][n]==0 and S[m+1][n]==0:
                        c+=[m+1,n]
                        S[m+1][n] = S[m][n]+1
                if m != 0:
                    if T[m-1][n]==0 and S[m-1][n]==0:
                        c+=[m-1,n] 
                        S[m-1][n] = S[m][n]+1
                if n != W-1:
                    if T[m][n+1]==0 and S[m][n+1]==0:
                        c+=[m,n+1]
                        S[m][n+1] = S[m][n]+1
                if n != 0:
                    if T[m][n-1]==0 and S[m][n-1]==0:
                        c+=[m,n-1]
                        S[m][n-1] = S[m][n]+1
                
        print(S)
        d = max(d,np.max(S))
        S= np.zeros_like(T, dtype=int)
        
print(d-1) 

H,W= [int(x) for x in input().split()]
n=[]
T=np.zeros((H,W),dtype=int)

for i in range(H):
  n=input()
  for j in range(W):
    if n[j]=="#": T[i][j]=1
B=T.copy()    
d=0
for i in range(H):
    for j in range(W):
         
        if T[i][j] ==1:continue

        c=deque()    
        s=0
        c+=[i,j]
        
        t=len(c)//2
        while t>0:
                s+=1
                t=len(c)//2
                for k in range(t):
                        m = c.popleft()
                        n = c.popleft()
                        T[m][n]=s
                        if m != H-1:
                                if T[m+1][n]==0:
                                        c.append(m+1)
                                        c.append(n)
                        if m != 0:
                                if T[m-1][n]==0:
                                        c.append(m-1)
                                        c.append(n)
 
                        if n != W-1:
                                if T[m][n+1]==0:
                                        c.append(m)
                                        c.append(n+1)
                                        
                        if n != 0:
                                if T[m][n-1]==0:
                                        c.append(m)
                                        c.append(n-1)

        #print(T)
        d = max(d,s)
        T=B.copy()
        
print(d-2)

from collections import deque
import numpy as np
 
 
H,W= [int(x) for x in input().split()]
n=[]
T=np.zeros((H,W),dtype=int)
T=[]
S=np.zeros_like(T,dtype=int) 
for i in range(H):
  T[i]=[x for x in input().split()]
#B=T.copy()    
d=0
c=deque()  
for i in range(H):
    for j in range(W):
         
        if T[i][j] =="#":continue
 
          
        c.append((i,j))
        S[i][j] = 1
        #c.append(j)
        
        #t=len(c)
        while True:

                try:
                  m,n = c.popleft()
                except IndexError: break
                
                
                for kk in [(m+1,n),(m-1,n),(m,n+1),(m,n-1)]:
                  if not (0<=kk[0]<H and 0<= kk[1] <W): continue
                  if T[kk]=="#":continue
                  if S[kk] == 0:
                    c.append(kk)
                    S[kk] = S[m][n]+1        
                        
 
        #print(T)
        d = max(d,np.max(S))
        S=np.zeros_like(T,dtype=int)
        
print(d-1)


from collections import deque
import numpy as np
 
 
H,W= [int(x) for x in input().split()]
n=[]
T=np.zeros((H,W),dtype=int)

for i in range(H):
  n=input()
  for j in range(W):
    if n[j]=="#": T[i][j]=1
B=T.copy()    
d=0
c=deque() 
for i in range(H):
    for j in range(W):
         
        if T[i][j] ==1:continue   
        s=0
        c.append((i,j))
        
        t=len(c)//2
        while t>0:
                s+=1
                t=len(c)//2
                for k in range(t):
                        m,n = c.popleft()
                        T[m][n]=s
                        if m != H-1:
                                if T[m+1][n]==0:c.append((m+1,n))
                        if m != 0:
                                if T[m-1][n]==0:c.append((m-1,n)) 
                        if n != W-1:
                                if T[m][n+1]==0:c.append((m,n+1))
                        if n != 0:
                                if T[m][n-1]==0:c.append((m,n-1))
                        #c += (chk_route([c.popleft(),c.popleft()]))
                
        #print(T)
        d = max(d,s)
        T=B.copy()
        
print(d-2)


from collections import deque
import numpy as np


H,W= [int(x) for x in input().split()]
n=[]
T=np.zeros((H,W),dtype=int)

for i in range(H):
  n=input()
  for j in range(W):
    if n[j]=="#": T[i][j]=1
S= np.zeros_like(T, dtype=int)
d=0
c=deque()

for i in range(H):
    for j in range(W):

        if T[i][j] ==1:continue

        c+=[i,j]
        S[i][j]=1
        
        while True:
            
                #print(c)
                try:
                    m = c.popleft()
                    n = c.popleft()
                except IndexError:
                    break
                if m != H-1:
                    if T[m+1][n]==0 and S[m+1][n]==0:
                        c+=[m+1,n]
                        S[m+1][n] = S[m][n]+1
                if m != 0:
                    if T[m-1][n]==0 and S[m-1][n]==0:
                        c+=[m-1,n]
                        S[m-1][n] = S[m][n]+1
                if n != W-1:
                    if T[m][n+1]==0 and S[m][n+1]==0:
                        c+=[m,n+1]
                        S[m][n+1] = S[m][n]+1
                if n != 0:
                    if T[m][n-1]==0 and S[m][n-1]==0:
                        c+=[m,n-1]
                        S[m][n-1] = S[m][n]+1

        print(S)
        d = max(d,np.max(S))
        S = np.zeros_like(T, dtype=int)

print(d-1)


from collections import deque
import numpy as np
 
 
H,W= [int(x) for x in input().split()]
n=[]
T=np.zeros((H,W),dtype='int')
#T=[]
S=np.zeros_like(T,dtype='int') 
for i in range(H):
  n=input()
  for j in range(W):
    if n[j]=="#": T[i][j]=1
#B=T.copy()    
d=0
c=deque()  
for i in range(H):
    for j in range(W):
         
        if T[i][j] ==1:continue
 
          
        c.append((i,j))
        S[i][j] = 1
        #c.append(j)
        
        #t=len(c)
        while True:

                try:
                  m,n = c.popleft()
                except IndexError: break
                
                
                for kk in [(m+1,n),(m-1,n),(m,n+1),(m,n-1)]:
                  if not (0<=kk[0]<H and 0<= kk[1] <W): continue
                  if T[kk]==1:continue
                  if S[kk] == 0:
                    c.append(kk)
                    S[kk] = S[m][n]+1        
                        
 
        #print(T)
        d = max(d,np.max(S))
        S=np.zeros_like(T,dtype='int')
        
print(d-1)


Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Runtime Error

Runtime Error

Wrong Answer

Wrong Answer

Accepted