Problem p03574 (python)

Fixed submission:
import sys
def main():
    in1 = input()
    nums, length = in1.split(' ')
    nums= int(nums)
    length = int(length)
    bombs = []

    for i in range(nums):

        bombs.append(input())
    checked = [['']*length for i in range(nums)]
    for i in range(nums):
        for j in range(length):
            if bombs[i][j] == '#':
                checked[i][j] = '#'
            else:
                bombnum = 0
                if i != 0:
                    if j != 0:
                        if bombs[i-1][j-1] == '#':
                            bombnum += 1

                    if j != length -1:
                        if bombs[i-1][j+1] == '#':
                            bombnum += 1

                    if bombs[i-1][j] == '#':
                        bombnum += 1


                if j != 0:
                    if bombs[i][j-1] == '#':
                        bombnum += 1

                if j != length-1:
                    if bombs[i][j+1] == '#':
                        bombnum += 1


                if i != nums-1:
                    if j != 0:
                        if bombs[i+1][j-1] == '#':
                            bombnum += 1

                    if j != length -1:
                        if bombs[i+1][j+1] == '#':
                            bombnum += 1

                    if bombs[i+1][j] == '#':
                        bombnum += 1

                checked[i][j] = bombnum
    for i in checked:
        for j in i:
            print(j, end = '')
        print('')
if __name__ == '__main__':
    main()

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

ans = board
dist = [(1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1), (0, 1)]

for i in range(H):
    for j in range(W):
        if board[i][j] == '#':
            continue
        count = 0
        for x, y in dist:
            now_x = i + x
            now_y = j + y
            if now_x >= W or now_x < 0 or now_y >= H or now_y < 0:
                continue
            if board[now_x][now_y] == '#':
                count += 1
        ans[i][j] = str(count)

for line in ans:
    print(''.join(line))

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

ans = board
dist = [(1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1), (0, 1)]

for i in range(H):
    for j in range(W):
        if board[i][j] == '#':
            continue
        count = 0
        for x, y in dist:
            now_x = i + x
            now_y = j + y
            if now_x > W or now_x < 0 or now_y > H or now_y < 0:
                continue
            if board[now_x][now_y] == '#':
                count += 1
        ans[i][j] = str(count)

for line in ans:
    print(''.join(line))

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

ans = board
dist = [(1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1), (0, 1)]
count = 0

for i in range(H):
    for j in range(W):
        if board[i][j] == '#':
            continue
        for x, y in dist:
            now_x = i + x
            now_y = j + y
            if now_x > W or now_x < 0 or now_y > H or now_y < 0:
                continue
            if board[now_x][now_y] == '#':
                count += 1
        ans[i][j] = str(count)

for line in ans:
    print(''.join(line))