Problem p03574 (python)

Fixed submission:
def check_nmine(h,w,matrix):
    count=0
    #h+1 -1 （いっぱい書く）
    if h-1 >= 0 and w-1 >= 0:
        if(matrix[h-1][w-1] == '#'):
            count += 1
    if h-1 >= 0 and w >= 0:
        if(matrix[h-1][w] == '#'):
            count += 1
    if h-1 >= 0 and w+1 < W:
        if(matrix[h-1][w+1] == '#'):
            count += 1
            
    if h >= 0 and w-1 >= 0:
        if(matrix[h][w-1] == '#'):
            count += 1
    if h >= 0 and w+1 < W:
        if(matrix[h][w+1] == '#'):
            count += 1
            
    if h+1 < H and w-1 >= 0:
        if(matrix[h+1][w-1] == '#'):
            count += 1
    if h+1 < H and w >= 0:
        if(matrix[h+1][w] == '#'):
            count += 1
    if h+1 < H and w+1 < W:
        if(matrix[h+1][w+1] == '#'):
            count += 1
            
    return count


H,W = map(int,input().split())



matrix = []
for i in range(H):
    matrix.append(list(input()))

for h in range(H):
    for w in range(W):
        if matrix[h][w] != '#':
            matrix[h][w] = str(check_nmine(h,w,matrix))
            
for i in matrix:
    print("".join(i))

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

matrix = []
for i in range(H):
    matrix.append(list(input()))

for h in range(H):
    for w in range(W):
        if matrix[h][w] != '#':
            matrix[h][w] = str(check_nmine(h,w,matrix))
            
for i in matrix:
    print("".join(i))
    
def check_nmine(h,w,matrix):
    count=0
    #h+1 -1 （いっぱい書く）
    if h-1 >= 0 and w-1 >= 0:
        if(matrix[h-1][w-1] == '#'):
            count += 1
    if h-1 >= 0 and w >= 0:
        if(matrix[h-1][w] == '#'):
            count += 1
    if h-1 >= 0 and w+1 < W:
        if(matrix[h-1][w+1] == '#'):
            count += 1
            
    if h >= 0 and w-1 >= 0:
        if(matrix[h][w-1] == '#'):
            count += 1
    if h >= 0 and w+1 < W:
        if(matrix[h][w+1] == '#'):
            count += 1
            
    if h+1 < H and w-1 >= 0:
        if(matrix[h+1][w-1] == '#'):
            count += 1
    if h+1 < H and w >= 0:
        if(matrix[h+1][w] == '#'):
            count += 1
    if h+1 < H and w+1 < W:
        if(matrix[h+1][w+1] == '#'):
            count += 1
            
    return count

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

# make iremono
matrix = []

for i in range(H):
    matrix.append(list(input()))

#rows = sample_input.split("\n")
#matrix = [list(r) for r in rows]

ans = matrix.copy()

for h in range(H):
    for w in range(W):
        if matrix[h][w] != '#':
            ans[h][w] = str(check_nmine(h,w,matrix))
            
for i in ans:
    print("".join(i))
    
def check_nmine(h,w,matrix):
    count=0
    #h+1 -1 （いっぱい書く）
    if h-1 >= 0 and w-1 >= 0:
        if(matrix[h-1][w-1] == '#'):
            count += 1
    if h-1 >= 0 and w >= 0:
        if(matrix[h-1][w] == '#'):
            count += 1
    if h-1 >= 0 and w+1 < W:
        if(matrix[h-1][w+1] == '#'):
            count += 1
            
    if h >= 0 and w-1 >= 0:
        if(matrix[h][w-1] == '#'):
            count += 1
    if h >= 0 and w+1 < W:
        if(matrix[h][w+1] == '#'):
            count += 1
            
    if h+1 < H and w-1 >= 0:
        if(matrix[h+1][w-1] == '#'):
            count += 1
    if h+1 < H and w >= 0:
        if(matrix[h+1][w] == '#'):
            count += 1
    if h+1 < H and w+1 < W:
        if(matrix[h+1][w+1] == '#'):
            count += 1
            
    return count