Problem:
You are given an H × W grid.   The squares in the grid are described by H strings, S_1,...,S_H.   The j-th character in the string S_i corresponds to the square at the i-th row from the top and j-th column from the left (1 \leq i \leq H,1 \leq j \leq W).   `.` stands for an empty square, and `#` stands for a square containing a bomb.  Dolphin is interested in how many bomb squares are horizontally, vertically or diagonally adjacent to each empty square.   (Below, we will simply say "adjacent" for this meaning. For each square, there are at most eight adjacent squares.)   He decides to replace each `.` in our H strings with a digit that represents the number of bomb squares adjacent to the corresponding empty square.  Print the strings after the process.

Incorrect submission (verdict: Wrong Answer):
h, w = map(int, input().split())
mass = []
for i in range(h):
    x = input()
    mass.append([])
    for j in range(w):
        mass[i].append(x[j])

init = []
for i in range(h):
    # x = input()
    init.append([])
    for j in range(w):
        init[i].append(0)

for i in range(h):
    for j in range(w):
        if mass[i][j] != "#":
            # up
            if i + 1 < h and mass[i + 1][j] == "#":
                init[i][j] += 1
                # down
            if i - 1 >= 0 and mass[i - 1][j] == "#":
                init[i][j] += 1
                # right
            if j + 1 < w and mass[i][j + 1] == "#":
                init[i][j] += 1
                # left
            if j - 1 >= 0 and mass[i][j - 1] == "#":
                init[i][j] += 1
            if j + 1 < w and i + 1 < h and mass[i + 1][j + 1] == "#":
                init[i][j] += 1

            if i - 1 >= 0 and j - 1 >= 0 and mass[i - 1][j - 1] == "#":
                init[i][j] += 1

            if i + 1 < h and j - 1 >= 0 and mass[i + 1][j - 1] == "#":
                init[i][j] += 1

            if i - 1 >= 0 and j + 1 < w and mass[i - 1][j + 1] == "#":
                init[i][j] += 1
        else:
            init[i][j] = "#"
for i in range(h):
    for j in range(w):
        print(init[i][j])


Fixed, accepted submission:
h, w = map(int, input().split())
mass = []
for i in range(h):
    x = input()
    mass.append([])
    for j in range(w):
        mass[i].append(x[j])

init = []
for i in range(h):
    # x = input()
    init.append([])
    for j in range(w):
        init[i].append(0)

for i in range(h):
    for j in range(w):
        if mass[i][j] != "#":
            # up
            if i + 1 < h and mass[i + 1][j] == "#":
                init[i][j] += 1
                # down
            if i - 1 >= 0 and mass[i - 1][j] == "#":
                init[i][j] += 1
                # right
            if j + 1 < w and mass[i][j + 1] == "#":
                init[i][j] += 1
                # left
            if j - 1 >= 0 and mass[i][j - 1] == "#":
                init[i][j] += 1
            if j + 1 < w and i + 1 < h and mass[i + 1][j + 1] == "#":
                init[i][j] += 1

            if i - 1 >= 0 and j - 1 >= 0 and mass[i - 1][j - 1] == "#":
                init[i][j] += 1

            if i + 1 < h and j - 1 >= 0 and mass[i + 1][j - 1] == "#":
                init[i][j] += 1

            if i - 1 >= 0 and j + 1 < w and mass[i - 1][j + 1] == "#":
                init[i][j] += 1
        else:
            init[i][j] = "#"
res = []
for i in range(h):
    for j in range(w):
        res.append(str(init[i][j]))
    print(''.join(res))
    res = []