Buggy python submission (verdict: Runtime Error):
h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
ans = [[0] * w for _ in range(h)]

for i in range(h):
    for j in range(w):
        count = 0
        if s[i][j] == '.':
            if i == 0:
                if s[i+1][j] == '#':
                    count += 1
                if j == 0:
                    if s[i][j+1] == '#':
                        count += 1
                    if s[i+1][j+1] == '#':
                        count += 1
                elif j == w-1:
                    if s[i][j-1] == '#':
                        count += 1
                    if s[i+1][j-1] == '#':
                        count += 1
                else:
                    if s[i][j+1] == '#':
                        count += 1
                    if s[i+1][j+1] == '#':
                        count += 1
                    if s[i][j-1] == '#':
                        count += 1
                    if s[i+1][j-1] == '#':
                        count += 1
            elif i == h-1:
                if s[i-1][j] == '#':
                    count += 1
                if j == 0:
                    if s[i][j+1] == '#':
                        count += 1
                    if s[i-1][j+1] == '#':
                        count += 1
                elif j == w-1:
                    if s[i][j-1] == '#':
                        count += 1
                    if s[i-1][j-1] == '#':
                        count += 1
                else:
                    if s[i][j+1] == '#':
                        count += 1
                    if s[i-1][j+1] == '#':
                        count += 1
                    if s[i][j-1] == '#':
                        count += 1
                    if s[i-1][j-1] == '#':
                        count += 1
            else:
                if j == 0:
                    if s[i-1][j] == '#':
                        count += 1
                    if s[i-1][j+1] == '#':
                        count += 1
                    if s[i][j+1] == '#':
                        count += 1
                    if s[i+1][j+1] == '#':
                        count += 1
                    if s[i+1][j] == '#':
                        count += 1
                elif j == w-1:
                    if s[i - 1][j] == '#':
                        count += 1
                    if s[i - 1][j - 1] == '#':
                        count += 1
                    if s[i][j - 1] == '#':
                        count += 1
                    if s[i + 1][j - 1] == '#':
                        count += 1
                    if s[i + 1][j] == '#':
                        count += 1
                else:
                    if s[i-1][j-1] == '#':
                        count += 1
                    if s[i-1][j] == '#':
                        count += 1
                    if s[i-1][j+1] == '#':
                        count += 1
                    if s[i][j-1] == '#':
                        count += 1
                    if s[i][j+1] == '#':
                        count += 1
                    if s[i+1][j+1] == '#':
                        count += 1
                    if s[i+1][j] == '#':
                        count += 1
                    if s[i+1][j-1] == '#':
                        count += 1
            ans[i][j] = str(count)
        else:
            ans[i][j] = '#'

for a in ans:
    print(''.join(map(str, a)))

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

a = [[0 for i in range(W)] for j in range(H)]
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            a[i][j] = "#"
            for p in [-1, 0, 1]:
                for q in [-1, 0, 1]:
                    if i + p >= 0 and j + q >= 0 and i + p < H and j + q < W and S[i + p][j + q] != "#":
                        a[i + p][j + q] += 1

for i in range(H):
    print(''.join(map(str, a[i])))