Buggy python submission (verdict: Runtime Error):
H, W = map(int, input().split())
S = []
for _ in range(H):
    S.append(list(input()))
# print(S)
for i in range(H):
    for j in range(W):
        if S[i][j] == '.':
            bomb_num = 0
            if i == 0:
                if j == 0:
                    for m in range(2):
                        for k in range(2):
                            if m == 0 and k == 0:
                                continue
                            if S[i+m][j+k] == '#':
                                bomb_num += 1
                        S[i][j] = str(bomb_num)
                elif j == W-1:
                    for m in range(2):
                        for k in range(2):
                            if m == 0 and k == 1:
                                continue
                            if S[i+m][j+k-1] == '#':
                                bomb_num += 1
                        S[i][j] = str(bomb_num)
                else:
                    for m in range(2):
                        for k in range(3):
                            if m == 0 and k == 1:
                                continue
                            if S[i+m][j+k-1] == '#':
                                bomb_num += 1
                        S[i][j] = str(bomb_num)
            elif i == H-1:
                if j == 0:
                    for m in range(2):
                        for k in range(2):
                            if m == 1 and k == 0:
                                continue
                            if S[i+m-1][j+k] == '#':
                                bomb_num += 1
                        S[i][j] = str(bomb_num)
                elif j == W-1:
                    for m in range(2):
                        for k in range(2):
                            if m == 1 and k == 1:
                                continue
                            if S[i+m-1][j+k-1] == '#':
                                bomb_num += 1
                        S[i][j] = str(bomb_num)
                else:
                    for m in range(2):
                        for k in range(3):
                            if m == 1 and k == 1:
                                continue
                            if S[i+m-1][j+k-1] == '#':
                                bomb_num += 1
                        S[i][j] = str(bomb_num)
            elif j == 0 and (1 <= i and i <= H-2):
                for m in range(3):
                    for k in range(2):
                        if m == 1 and k == 0:
                            continue
                        if S[i+m-1][j+k] == '#':
                            bomb_num += 1
                    S[i][j] = str(bomb_num)
            elif j == W-1 and (1 <= i and i <= H-2):
                for m in range(3):
                    for k in range(2):
                        if m == 1 and k == 1:
                            continue
                        if S[i+m-1][j+k-1] == '#':
                            bomb_num += 1
                    S[i][j] = str(bomb_num)
            else:
                for m in range(3):
                    for k in range(3):
                        if m == 1 and k == 1:
                            continue
                        if S[i+m-1][j+k-1] == '#':
                            bomb_num += 1
                S[i][j] = str(bomb_num)

for i in range(H):
    answer = ''.join(S[i])
    print(answer)

Fixed submission:
H, W = map(int, input().split())
filed = []
for i in range(H):
    filed.append(list(input()))
dy = [1, 0, -1, 0, 1, -1, 1, -1]
dx = [0, 1, 0, -1, 1, 1, -1, -1]

for y in range(H):
    for x in range(W):
        count = 0
        if filed[y][x] == '#':
            continue
        for k in range(8):
            ny = y + dy[k]
            nx = x + dx[k]
            if ny < 0 or H <= ny or nx < 0 or W <= nx:
                continue
            if filed[ny][nx] == '#':
                count += 1
        filed[y][x] = str(count)

for y in range(H):
    print("".join(filed[y]))