Problem p03053 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**7)
from collections import deque
from heapq import heappush, heappop
def solve():
    H,W = map(int, input().split())
    A = [ list(input()) for _ in range(H)]
    HMANY = [ [10**9] * W for _ in range(H)]

    
    
    # q = deque()
    for h in range(H):
        for w in range(W):
            if A[h][w] == "#" :
                HMANY[h][w] = 0
    for w in range(W):
        for h in range(H-1):
            if HMANY[h][w]+1 < HMANY[h+1][w]:
                HMANY[h+1][w] = HMANY[h][w] + 1
        for h in range(H-1, 0, -1):
            if HMANY[h][w]+1 < HMANY[h-1][w]:
                HMANY[h-1][w] = HMANY[h][w] + 1
    
    for h in range(H):
        for w in range(W-1):
            if HMANY[h][w]+1 < HMANY[h][w+1]:
                HMANY[h][w+1] = HMANY[h][w] + 1
        for w in range(W-1, 0, -1):
            if HMANY[h][w]+1 < HMANY[h][w-1]:
                HMANY[h][w-1] = HMANY[h][w] + 1        
    #             q.append((h,w))
    #             A[h][w] = "."
    # ans = 0
    # while q:
    #     nextQ = deque()
    #     for h,w in q:
    #         A[h][w] = "#"
    #         if h+1 <H and A[h+1][w] == ".":
    #             nextQ.append( (h+1,w))
    #         if w+1 <W and A[h][w+1] == ".":
    #             nextQ.append((h,w+1))
    #         if 0<=h-1 and A[h-1][w] == ".":
    #             nextQ.append((h-1,w))
    #         if 0<=w-1 and A[h][w-1] == ".":
    #             nextQ.append((h,w-1))        
    #     ans += 1
    #     q = nextQ
        # num,h,w = q.popleft()        
        # if HMANY[h][w] > num and A[h][w] == ".":
        #     HMANY[h][w] = num
        #     A[h][w] = "#"
        #     if h+1 <H and A[h+1][w] == ".":
        #         q.append( (num+1,h+1,w))
        #     if w+1 <W and A[h][w+1] == ".":
        #         q.append((num+1,h,w+1))
        #     if 0<=h-1 and A[h-1][w] == ".":
        #         q.append((num+1,h-1,w))
        #     if 0<=w-1 and A[h][w-1] == ".":
        #         q.append((num+1,h,w-1))

    ans = 0
    for h in range(H):
        ans = max(ans, max(HMANY[h]))
    # print(ans-1)
    print(ans)

if __name__ == "__main__":
    solve()

Buggy submission (verdict: Runtime Error):
H,W = map(int, input().split())
A = [input() for _ in range(H)]
HMANY = [ [10**9] * W for _ in range(H)]


def dfs(h,w,A,HMANY, num,H,W):
    if not ( 0<= h < H and 0 <= w < W) : 
        return

    if HMANY[h][w] > num:
        HMANY[h][w] = num  

        if 0 <= h-1 :
            dfs(h-1,w,A,HMANY,num+1 ,H,W)
        if h+1 < H:
            dfs(h+1,w,A,HMANY,num+1 ,H,W)
        if w+1 < W:
            dfs(h,w+1,A,HMANY,num+1 ,H,W)
        if 0<=w-1:
            dfs(h,w-1,A,HMANY,num+1 ,H,W)

for h in range(H):
    for w in range(W):
        if A[h][w] == "#":
            dfs(h,w, A,HMANY, 0, H,W)

ans = 0
for h in range(H):
    ans = max(ans, max(HMANY[h]))
    # ans = max(ans, HMANY[h][w])
print(ans)

Buggy submission (verdict: Runtime Error):
H,W = map(int, input().split())
A = [input() for _ in range(H)]
HMANY = [ [10**9] * W for _ in range(H)]


def dfs(h,w,A,HMANY, num):
    if not ( 0<= h < H and 0 <= w < W) : 
        return
    if HMANY[h][w] > num:
        HMANY[h][w] = num        
        if 0 <= h-1 :
            dfs(h-1,w,A,HMANY,num+1 )
        if h+1 < H:
            dfs(h+1,w,A,HMANY,num+1 )
        if w+1 < W:
            dfs(h,w+1,A,HMANY,num+1 )
        if 0<=w-1:
            dfs(h,w-1,A,HMANY,num+1 )

for h in range(H):
    for w in range(W):
        # if HMANY[h][w] == 10**9:
            if A[h][w] == "#":
                dfs(h,w, A,HMANY, 0)

ans = 0
for h in range(H):
    for w in range(W):
        ans = max(ans, HMANY[h][w])
print(ans)

Buggy submission (verdict: Runtime Error):
H,W = map(int, input().split())
A = [input() for _ in range(H)]
HMANY = [ [10**9] * W for _ in range(H)]


def dfs(h,w,A,HMANY, num):
    if HMANY[h][w] > num:
        HMANY[h][w] = num        
        if 0 <= h - 1 :
            dfs(h-1,w,A,HMANY,num+1 )
        if h+1 < H:
            dfs(h+1,w,A,HMANY,num+1 )
        if w+1 < W:
            dfs(h,w+1,A,HMANY,num+1 )
        if 0<=w-1:
            dfs(h,w-1,A,HMANY,num+1 )

for h in range(H):
    for w in range(W):
        # if HMANY[h][w] == 10**9:
            if A[h][w] == "#":
                dfs(h,w, A,HMANY, 0)

ans = 0
for h in range(H):
    for w in range(W):
        ans = max(ans, HMANY[h][w])
print(ans)

Buggy submission (verdict: Runtime Error):
H,W = map(int, input().split())
A = [input() for _ in range(H)]
HMANY = [ [10**9] * W for _ in range(H)]


def dfs(h,w,A,HMANY, num):
    if HMANY[h][w] > num:
        HMANY[h][w] = num        
        if 0 <= h - 1 :
            dfs(h-1,w,A,HMANY,num+1 )
        if h+1 < H:
            dfs(h+1,w,A,HMANY,num+1 )
        if w+1 < W:
            dfs(h,w+1,A,HMANY,num+1 )
        if 0<=w-1:
            dfs(h,w-1,A,HMANY,num+1 )

for h in range(H):
    for w in range(W):
        # if HMANY[h][w] == 10**9:
            if A[h][w] == "#":
                dfs(h,w, A,HMANY, 0)

ans = 0
for h in range(H):
    for w in range(W):
        ans = max(ans, HMANY[h][w])
print(ans)