Buggy python submission (verdict: Runtime Error):
h, w = list(map(int, input().split()))
s = [input() for i in range(h)]
ans = [[0] * w for i in range(h)]
for i in range(h):
    for j in range(w):
        count = 0
        # print(i, j)
        if s[i][j] == '#':
            ans[i][j] = '#'
            continue
        if i == 0:
            if j == 0:
                if s[i][1] == '#':
                    count += 1
                if s[i+1][0] == '#':
                    count += 1
                if s[i+1][1] == '#':
                    count += 1
            elif j == w-1:
                if s[i][j-1] == '#':
                    count += 1
                if s[i+1][j] == '#':
                    count += 1
                if s[i+1][j-1] == '#':
                    count += 1
            else:
                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
        elif i == h-1:
            if j == 0:
                if s[i][1] == '#':
                    count += 1
                if s[i-1][0] == '#':
                    count += 1
                if s[i-1][1] == '#':
                    count += 1
            elif j == w-1:
                if s[i][j-1] == '#':
                    count += 1
                if s[i-1][j] == '#':
                    count += 1
                if s[i-1][j-1] == '#':
                    count += 1
            else:
                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
        else:
            if j == 0:
                if s[i][1] == '#':
                    count += 1
                if s[i-1][0] == '#':
                    count += 1
                if s[i-1][1] == '#':
                    count += 1
                if s[i+1][0] == '#':
                    count += 1
                if s[i+1][1] == '#':
                    count += 1
            elif j == w-1:
                if s[i][j-1] == '#':
                    count += 1
                if s[i-1][j] == '#':
                    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
            else:
                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
                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)
for i in range(h):
    print(''.join(ans[i]))

Fixed submission:
h, w = list(map(int, input().split()))
s = [[c for c in input()] for i in range(h)]
ans = [[0] * w for i in range(h)]

dx = [1, 0, -1, 0, 1, -1, -1, 1]
dy = [0, 1, 0, -1, 1, 1, -1, -1]

for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            continue
        num = 0
        for d in range(8):
            ni = i + dy[d]
            nj = j + dx[d]
            if ni < 0 or h <= ni:
                continue
            if nj < 0 or w <= nj:
                continue
            if s[ni][nj] == '#':
                num += 1
        s[i][j] = num
for l in s:
    print(''.join([str(c) for c in l]))