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):
# -*- coding: utf-8 -*- #

h, w = map(int, input().split())
s_list = [list(input()) for _ in range(h)]
ans_list = [[0] * w for _ in range(h)]
print(s_list)
print(ans_list)

for i in range(h):
    for j in range(w):
        if s_list[i][j] == "#":
            ans_list[i][j] = "#"
        else:
            if (i!=0) and (j!=0):
                if (s_list[i-1][j-1] == "#"):
                    ans_list[i][j] += 1
            if (i!=0):
                if (s_list[i-1][j] == "#"):
                    ans_list[i][j] += 1
            if (i!=0) and (j!=w-1):
                if (s_list[i-1][j+1] == "#"):
                    ans_list[i][j] += 1
            if (j!=0):
                if (s_list[i][j-1] == "#"):
                    ans_list[i][j] += 1
            if (j!=w-1):
                if (s_list[i][j+1] == "#"):
                    ans_list[i][j] += 1
            if (i!=h-1) and (j!=0):
                if (s_list[i+1][j-1] == "#"):
                    ans_list[i][j] += 1
            if (i!=h-1):
                if (s_list[i+1][j] == "#"):
                    ans_list[i][j] += 1
            if (i!=h-1) and (j!=w-1):
                if (s_list[i+1][j+1] == "#"):
                    ans_list[i][j] += 1

for i in ans_list:
    print(''.join(map(str, i)))

Fixed, accepted submission:
# -*- coding: utf-8 -*- #

h, w = map(int, input().split())
s_list = [list(input()) for _ in range(h)]
ans_list = [[0] * w for _ in range(h)]

for i in range(h):
    for j in range(w):
        if s_list[i][j] == "#":
            ans_list[i][j] = "#"
        else:
            if (i!=0) and (j!=0):
                if (s_list[i-1][j-1] == "#"):
                    ans_list[i][j] += 1
            if (i!=0):
                if (s_list[i-1][j] == "#"):
                    ans_list[i][j] += 1
            if (i!=0) and (j!=w-1):
                if (s_list[i-1][j+1] == "#"):
                    ans_list[i][j] += 1
            if (j!=0):
                if (s_list[i][j-1] == "#"):
                    ans_list[i][j] += 1
            if (j!=w-1):
                if (s_list[i][j+1] == "#"):
                    ans_list[i][j] += 1
            if (i!=h-1) and (j!=0):
                if (s_list[i+1][j-1] == "#"):
                    ans_list[i][j] += 1
            if (i!=h-1):
                if (s_list[i+1][j] == "#"):
                    ans_list[i][j] += 1
            if (i!=h-1) and (j!=w-1):
                if (s_list[i+1][j+1] == "#"):
                    ans_list[i][j] += 1

for i in ans_list:
    print(''.join(map(str, i)))