Problem p03574 (python)

Fixed submission:
h, w = map(int, input().split())
S = [list(input()) for i in range(h)]
for i in range(h):
    for j in range(w):
        if S[i][j] == '#':
            continue
        cnt = 0
        for dx in [-1, 0, 1]:
            for dy in [-1, 0, 1]:
                if not 0 <= i + dy < h or not 0 <= j + dx < w:
                    continue
                if S[i + dy][j + dx] == '#':
                    cnt += 1
        S[i][j] = str(cnt)
for s in S:
    print("".join(s))

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

h, m = map(int, input().split())
s = [''] * h
ans = [[0] * m for i in range(h)]
for i in range(h):
    s[i] = input()
for i in range(1, h - 1):
    for j in range(1, m - 1):
        cnt = 0
        if s[i][j] == '.':
            if s[i - 1][j - 1] == '#':
                cnt += 1
            if s[i - 1][j] == '#':
                cnt += 1
            if s[i - 1][j + 1] == '#':
                cnt += 1
            if s[i][j - 1] == '#':
                cnt += 1
            if s[i + 1][j + 1] == '#':
                cnt += 1
            if s[i][j + 1] == '#':
                cnt += 1
            if s[i + 1][j - 1] == '#':
                cnt += 1
            if s[i + 1][j] == '#':
                cnt += 1
        else:
            cnt = 10
        ans[i][j] += cnt

for i in range(1, m - 1):
    cnt_1, cnt_2 = 0, 0
    if s[0][i] == '.':
        if s[0][i - 1] == '#':
            cnt_1 += 1
        if s[1][i + 1] == '#':
            cnt_1 += 1
        if s[0][i + 1] == '#':
            cnt_1 += 1
        if s[1][i - 1] == '#':
            cnt_1 += 1
        if s[1][i] == '#':
            cnt_1 += 1
    else:
        cnt_1 = 10
    if s[h - 1][i] == '.':
        if s[h - 1][i - 1] == '#':
            cnt_2 += 1
        if s[h - 2][i + 1] == '#':
            cnt_2 += 1
        if s[h - 1][i + 1] == '#':
            cnt_2 += 1
        if s[h - 2][i - 1] == '#':
            cnt_2 += 1
        if s[h - 2][i] == '#':
            cnt_2 += 1
    else:
        cnt_2 = 10
    ans[0][i] = cnt_1
    ans[h - 1][i] = cnt_2
for i in range(1, h - 1):
    cnt_1, cnt_2 = 0, 0
    if s[i][0] == '.':
        if s[i - 1][0] == '#':
            cnt_1 += 1
        if s[i - 1][1] == '#':
            cnt_1 += 1
        if s[i + 1][1] == '#':
            cnt_1 += 1
        if s[i][1] == '#':
            cnt_1 += 1
        if s[i + 1][0] == '#':
            cnt_1 += 1
    else:
        cnt_1 = 10
    if s[i][h - 1] == '.':
        if s[i - 1][m - 1] == '#':
            cnt_2 += 1
        if s[i - 1][m - 2] == '#':
            cnt_2 += 1
        if s[i + 1][m - 2] == '#':
            cnt_2 += 1
        if s[i][m - 2] == '#':
            cnt_2 += 1
        if s[i + 1][m - 1] == '#':
            cnt_2 += 1
    else:
        cnt_2 = 10
    ans[i][0] = cnt_1
    ans[i][m - 1] = cnt_2
    cnt_1, cnt_2 = 0, 0
if s[0][0] == '.':
    if s[1][0] == '#':
        ans[0][0] += 1
    if s[0][1] == '#':
        ans[0][0] += 1
    if s[1][1] == '#':
        ans[0][0] += 1
else:
    ans[0][0] = 10
if s[0][m - 1] == '.':
    if s[1][m - 1] == '#':
        ans[0][m - 1] += 1
    if s[0][m - 2] == '#':
        ans[0][m - 1] += 1
    if s[1][m - 2] == '#':
        ans[0][m - 1] += 1
else:
    ans[0][m - 1] = 10
if s[h - 1][0] == '.':
    if s[h - 2][0] == '#':
        ans[h - 1][0] += 1
    if s[h - 1][1] == '#':
        ans[h - 1][0] += 1
    if s[h - 2][1] == '#':
        ans[h - 1][0] += 1
else:
    ans[h - 1][0] = 10
if s[h - 1][m - 1] == '.':
    if s[h - 2][m - 1] == '#':
        ans[h - 1][m - 1] += 1
    if s[h - 1][m - 2] == '#':
        ans[h - 1][m - 1] += 1
    if s[h - 2][m - 2] == '#':
        ans[h - 1][m - 1] += 1
else:
    ans[h - 1][m - 1] = 10
for i in range(h):
    for j in range(m):
        if ans[i][j] == 10:
            print('#', end='')
        else:
            print(ans[i][j], end='')
    print()