Problem p02803 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)

def main():
    H,W = map(int, input().split())

    S = ["" for _ in range(H)]
    for i in range(H):
        S[i] = input()

    INF = float("inf")
    N = H*W
    dp = [[INF for _ in range(N)] for _ in range(N)]

    for i in range(H):
        for j in range(W):
            
            if j != W-1 and S[i][j] == "." and S[i][j+1] == ".":
                dp[(W*i)+j][(W*i)+j+1] = 1
                dp[(W*i)+j+1][(W*i)+j] = 1

            if i != H-1 and S[i][j] == "." and S[i+1][j] == ".":

                dp[(W*i)+j][(W*(i+1))+j] = 1
                dp[(W*(i+1))+j][(W*i)+j] = 1
        
            dp[(W*i)+j][(W*i)+j] = 0


    N = H*W
    for k in range(N):
        for i in range(N):
            for j in range(N):
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])

    ans = 0
    for i in range(N):
        for j in range(N):
            if dp[i][j] != INF:
                ans = max(ans, dp[i][j])
    
    print(ans)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
H,W = map(int,input().split())
S = [input() for i in range(H)]

dxy = [(0,1),(1,0),(0,-1),(-1,0)]
from collections import deque
ans = 0
for y in range(H):
    for x in range(W):
        if S[y][x] == '#': continue
        dist = [[0]*W for i in range(H)]
        visited = [[0]*W for i in range(H)]
        visited[y][x] = 1
        q = deque([(x,y)])
        while q:
            x,y = q.popleft()
            for dx,dy in dxy:
                nx,ny = x+dx,y+dy
                if not 0 <= nx < W: continue
                if not 0 <= ny < H: continue
                if visited[ny][nx]: continue
                if S[ny][nx] == '#': continue
                visited[ny][nx] = 1
                dist[ny][nx] = dist[y][x] + 1
                q.append((nx,ny))
        
        for i in range(H):
            ans = max(ans, dist[i])

print(ans)

Buggy submission (verdict: Runtime Error):
from collections import deque



H,W = map(int, input().split())
S = [input() for _ in range(H)]

ans = 0
dyx = [(1,0), (0,1),(-1,0), (0,-1)]
for i in range(H):
    for j in range(W):
        if S[i][j] == "#" continue
        dist = [[0 for _ in range(W)] for _ in range(H)] 
        dist[i][j] = 0
        visited = [[False for _ in range(W)] for _ in range(H)]
        visited[i][j] = True
        tmp = 0
        q = deque()
        q.append((i,j))
        while q:
            y,x = q.popleft()
            for dy,dx in dyx:
                ny, nx = y+dy, x+dx
                if not 0 <= ny <= H-1 or not 0 <= nx <= W-1 or visited[ny][nx] == True or S[ny][nx] == "#":
                    continue
                q.append((ny, nx))
                dist[ny][nx] = dist[y][x] + 1
                visited[ny][nx] = True



        for i in range(H):
            ans = max(ans, max(dist[i]))

            

print(ans)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**6)

def main():
    H,W = map(int, input().split())

    S = ["" for _ in range(H)]
    for i in range(H):
        S[i] = input()

    INF = float("inf")
    dp = [[INF for _ in range(H*W)] for _ in range(H*W)]

    for i in range(H):
        for j in range(W):
            
            if j != W-1 and S[i][j] == "." and S[i][j+1] == ".":
                dp[(H*i)+j][(H*i)+j+1] = 1
                dp[(H*i)+j+1][(H*i)+j] = 1

            if i != H-1 and S[i][j] == "." and S[i+1][j] == ".":
                dp[(H*i)+j][(H*(i+1))+j] = 1
                dp[(H*(i+1))+j][(H*i)+j] = 1
        
            dp[(H*i)+j][(H*i)+j] = 0


    N = H*W
    for k in range(N):
        for i in range(N):
            for j in range(N):
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])

    ans = 0
    for i in range(N):
        for j in range(N):
            if dp[i][j] != INF:
                ans = max(ans, dp[i][j])
    
    print(ans)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
def main():
    H,W = map(int, input().split())

    S = ["" for _ in range(H)]
    for i in range(H):
        S[i] = input()

    INF = float("inf")
    dp = [[INF for _ in range(H*W)] for _ in range(H*W)]

    for i in range(H):
        for j in range(W):
            
            if j != W-1 and S[i][j] == "." and S[i][j+1] == ".":
                dp[(H*i)+j][(H*i)+j+1] = 1
                dp[(H*i)+j+1][(H*i)+j] = 1

            if i != H-1 and S[i][j] == "." and S[i+1][j] == ".":
                dp[(H*i)+j][(H*(i+1))+j] = 1
                dp[(H*(i+1))+j][(H*i)+j] = 1
        
            dp[(H*i)+j][(H*i)+j] = 0


    N = H*W
    for k in range(N):
        for i in range(N):
            for j in range(N):
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])

    ans = 0
    for i in range(N):
        for j in range(N):
            if dp[i][j] != INF:
                ans = max(ans, dp[i][j])
    
    print(ans)

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
H,W = map(int, input().split())

S = ["" for _ in range(H)]
for i in range(H):
    S[i] = input()

INF = float("inf")
dp = [[INF for _ in range(H*W)] for _ in range(H*W)]

for i in range(H):
    for j in range(W):
        
        if j != W-1 and S[i][j] == "." and S[i][j+1] == ".":
            dp[(H*i)+j][(H*i)+j+1] = 1
            dp[(H*i)+j+1][(H*i)+j] = 1

        if i != H-1 and S[i][j] == "." and S[i+1][j] == ".":
            dp[(H*i)+j][(H*(i+1))+j] = 1
            dp[(H*(i+1))+j][(H*i)+j] = 1
    
        dp[(H*i)+j][(H*i)+j] = 0


N = H*W
for k in range(N):
    for i in range(N):
        for j in range(N):
            dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])

ans = 0
for i in range(N):
    for j in range(N):
        if dp[i][j] != INF:
            ans = max(ans, dp[i][j])
 
print(ans)