Buggy python submission (verdict: Runtime Error):
H, W = map(int, input().split())
S = [input() for _ in range(H)]
S_ans = [[0 for i in range(W)] for j in range(H)]

for j in range(H):
    for i in range(W):
        if S[j][i] == '#':
            S_ans[j][i] = '#'

            # 左端のケース
            if j == 0:
                # 上下端のケース
                if i == 0 and i == H -1:
                    if j + 2 <= H:
                        if S[j + 1][i]:
                            S_ans[j + 1][i] += 1
                # 上端のケース
                elif i == 0:
                    if j + 2 <= H:
                        if S[j + 1][i] == '.':
                            S_ans[j + 1][i] += 1
                    if i + 2 <= W:
                        if S[j][i + 1] == '.':
                            S_ans[j][i + 1] += 1
                    if j + 2 <= H and i + 2 <= W:
                        if S[j + 1][i + 1] == '.':
                            S_ans[j + 1][i + 1] += 1
                # 下端のケース
                elif i == W - 1:
                    if j + 2 <= H:
                        if S[j + 1][i] == '.':
                            S_ans[j + 1][i] += 1
                    if i >= 1:
                        if S[j][i - 1] == '.':
                            S_ans[j][i - 1] += 1
                    if j + 2 <= H and i >= 1:
                        if S[j + 1][i - 1] == '.':
                            S_ans[j + 1][i - 1] += 1
                # 上下の端ではないケース
                else:
                    if j + 2 <= H:
                        if S[j + 1][i] == '.':
                            S_ans[j + 1][i] += 1
                    if i + 2 <= W:
                        if S[j][i + 1] == '.':
                            S_ans[j][i + 1] += 1
                    if j + 2 <= H and i + 2 <= W:
                        if S[j + 1][i + 1] == '.':
                            S_ans[j + 1][i + 1] += 1
                    if i >= 1:
                        if S[j][i - 1] == '.':
                            S_ans[j][i - 1] += 1
                    if j + 2 <= H and i >= 1:
                        if S[j + 1][i - 1] == '.':
                            S_ans[j + 1][i - 1] += 1
            # 右端のケース
            elif j == H - 1:
                # 上下端のケース
                if i == 0 and i == W -1:
                    if j >= 1:
                        if S[j - 1][i]:
                            S_ans[j - 1][i] += 1
                # 上端のケース
                elif i == 0:
                    if j - 1 >= 0:
                        if S[j - 1][i] == '.':
                            S_ans[j - 1][i] += 1
                    if i + 2 <= W:
                        if S[j][i + 1] == '.':
                            S_ans[j][i + 1] += 1
                    if j - 1 >= 0 and i + 2 <= W:
                        if S[j - 1][i + 1] == '.':
                            S_ans[j - 1][i + 1] += 1
                # 下端のケース
                elif i == W - 1:
                    if j - 1 >= 0:
                        if S[j - 1][i] == '.':
                            S_ans[j - 1][i] += 1
                    if i >= 1:
                        if S[j][i - 1] == '.':
                            S_ans[j][i - 1] += 1
                    if j - 1 > 0 and i >= 1:
                        if S[j - 1][i - 1] == '.':
                            S_ans[j - 1][i - 1] += 1
                # 上下の端ではないケース
                else:
                    if j - 1 >= 0:
                        if S[j - 1][i] == '.':
                            S_ans[j - 1][i] += 1
                    if i + 2 <= W:
                        if S[j][i + 1] == '.':
                            S_ans[j][i + 1] += 1
                    if j - 1 >= 0 and i + 2 <= W:
                        if S[j - 1][i + 1] == '.':
                            S_ans[j - 1][i + 1] += 1
                    if i >= 1:
                        if S[j][i - 1] == '.':
                            S_ans[j][i - 1] += 1
                    if j - 1 > 0 and i >= 1:
                        if S[j - 1][i - 1] == '.':
                            S_ans[j - 1][i - 1] += 1
            # 左右の端ではないケース
            else:
                # 上下端のケース
                if i == 0 and i == W - 1:
                    if j + 2 <= H:
                        if S[j + 1][i]:
                            S_ans[j + 1][i] += 1
                    if j >= 1:
                        if S[j - 1][i]:
                            S_ans[j - 1][i] += 1
                # 上橋のケース
                elif i == 0:
                    if j - 1 >= 0:
                        if S[j - 1][i] == '.':
                            S_ans[j - 1][i] += 1
                    if j + 2 <= H:
                        if S[j + 1][i] == '.':
                            S_ans[j + 1][i] += 1
                    if i + 2 <= W:
                        if S[j][i + 1] == '.':
                            S_ans[j][i + 1] += 1
                        if S[j + 1][i + 1] == '.':
                            S_ans[j + 1][i + 1] += 1
                        if S[j - 1][i + 1] == '.':
                            S_ans[j - 1][i + 1] += 1
                # 下端のケース
                elif i == W - 1:
                    if j - 1 >= 0:
                        if S[j - 1][i] == '.':
                            S_ans[j - 1][i] += 1
                    if j + 2 <= H:
                        if S[j + 1][i] == '.':
                            S_ans[j + 1][i] += 1
                    if i >= 1:
                        if S[j][i - 1] == '.':
                            S_ans[j][i - 1] += 1
                        if S[j + 1][i - 1] == '.':
                            S_ans[j + 1][i - 1] += 1
                        if S[j - 1][i - 1] == '.':
                            S_ans[j - 1][i - 1] += 1
                # 上下の端ではないケース
                else:
                    if j - 1 >= 0:
                        if S[j - 1][i] == '.':
                            S_ans[j - 1][i] += 1
                    if j + 2 <= H:
                        if S[j + 1][i] == '.':
                            S_ans[j + 1][i] += 1
                    if i + 2 <= W:
                        if S[j][i + 1] == '.':
                            S_ans[j][i + 1] += 1
                        if S[j + 1][i + 1] == '.':
                            S_ans[j + 1][i + 1] += 1
                        if S[j - 1][i + 1] == '.':
                            S_ans[j - 1][i + 1] += 1
                    if i >= 1:
                        if S[j][i - 1] == '.':
                            S_ans[j][i - 1] += 1
                        if S[j + 1][i - 1] == '.':
                            S_ans[j + 1][i - 1] += 1
                        if S[j - 1][i - 1] == '.':
                            S_ans[j - 1][i - 1] += 1

for i in range(H):
    print(''.join([str(s) for s in S_ans[i]]))


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

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

for i in range(H):
    for j in range(W):
        if S[i][j] != '#':
            count = 0
            for k in range(8):
                x = i + dx[k]
                y = j + dy[k]

                if (0 <= x <= H - 1) and (0 <= y <= W -1):
                    if S[x][y] == "#":
                        count += 1
            S[i][j] = str(count)

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