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):
def nyu():
    H,W = map(int,input().split())
    S = [list(input()) for i in range(H)] 
    return S,H,W

def check(S,H,W):

    for h in range(H):
        for w in range(W):
            if S[h][w] != "#":
                if h !=0 :
                    if S[h-1][w] == "#":
                        S[h][w] +=1
                if h !=H-1 :
                    if S[h+1][w] == "#":
                        S[h][w] +=1
                if w !=0 :
                    if S[h][w-1] == "#":
                        S[h][w] +=1
                if w !=W-1 :
                    if S[h][w+1] == "#":
                        S[h][w] +=1

                if h !=H-1  and w !=W-1:                    
                    if S[h+1][w+1] == "#":
                        S[h][w] +=1
                if h != 0  and w !=W-1:                    
                    if S[h-1][w+1] == "#":
                        S[h][w] +=1

                if h !=H-1  and w !=0:                    
                    if S[h+1][w-1] == "#":
                        S[h][w] +=1

                if h != 0  and w !=0:                    
                    if S[h-1][w-1] == "#":
                        S[h][w] +=1

    for a in S:
        print(*a)

def convert(S,H,W):
    
    for h in range(H):
        for w in range(W):
            if S[h][w]==".":
                S[h][w] = 0
    return S




S,H,W = nyu()
S =convert(S,H,W)
check(S,H,W)


Fixed, accepted submission:
def nyu():
    H,W = map(int,input().split())
    S = [list(input()) for i in range(H)] 
    return S,H,W

def check(S,H,W):

    for h in range(H):
        for w in range(W):
            if S[h][w] != "#":
                if h !=0 :
                    if S[h-1][w] == "#":
                        S[h][w] +=1
                if h !=H-1 :
                    if S[h+1][w] == "#":
                        S[h][w] +=1
                if w !=0 :
                    if S[h][w-1] == "#":
                        S[h][w] +=1
                if w !=W-1 :
                    if S[h][w+1] == "#":
                        S[h][w] +=1

                if h !=H-1  and w !=W-1:                    
                    if S[h+1][w+1] == "#":
                        S[h][w] +=1
                if h != 0  and w !=W-1:                    
                    if S[h-1][w+1] == "#":
                        S[h][w] +=1

                if h !=H-1  and w !=0:                    
                    if S[h+1][w-1] == "#":
                        S[h][w] +=1

                if h != 0  and w !=0:                    
                    if S[h-1][w-1] == "#":
                        S[h][w] +=1

    for a in S:
       print(*a,sep='')
 

def convert(S,H,W):
    
    for h in range(H):
        for w in range(W):
            if S[h][w]==".":
                S[h][w] = 0
    return S

S,H,W = nyu()
S =convert(S,H,W)
check(S,H,W)