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())
S = [list(input()) for i in range(H)]
S=[[0 if j == "." else "#" for j in i] for i in S]

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

                try:S[i-1][j-1]
                except IndexError:pass
                else:
                    if  i-1>=0 and j-1>0 and S[i-1][j-1]!='#':
                        S[i-1][j-1]+=1

                try:S[i-1][j]
                except IndexError:pass
                else:
                    if i-1>=0 and S[i-1][j]!='#':
                        S[i-1][j]+=1

                try:S[i-1][j+1]
                except IndexError:pass
                else:
                    if i-1>=0 and S[i-1][j+1]!='#':
                        S[i-1][j+1]+=1

                try:S[i][j-1]
                except IndexError:pass
                else:
                    if  j-1>=0 and S[i][j-1]!='#':
                        S[i][j-1]+=1
                        
                try:S[i][j+1]
                except IndexError:pass
                else:
                    if  S[i][j+1]!='#':
                        S[i][j+1]+=1

                try:S[i+1][j-1]
                except IndexError:pass
                else:
                    if j-1>=0 and S[i+1][j-1]!='#':
                        S[i+1][j-1]+=1

                try:S[i+1][j]
                except IndexError:pass
                else:
                    if S[i+1][j]!='#':
                        S[i+1][j]+=1

                try:S[i+1][j+1]
                except IndexError:pass
                else:
                    if S[i+1][j+1]!='#':
                        S[i+1][j+1]+=1
print(S)

Fixed, accepted submission:
H,W = map(int, input().split())
S = [list(input()) for i in range(H)]
S=[[0 if j == "." else "#" for j in i] for i in S]

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

                try:S[i-1][j-1]
                except IndexError:pass
                else:
                    if  i-1>=0 and j-1>=0 and S[i-1][j-1]!='#':
                        S[i-1][j-1]+=1

                try:S[i-1][j]
                except IndexError:pass
                else:
                    if i-1>=0 and S[i-1][j]!='#':
                        S[i-1][j]+=1

                try:S[i-1][j+1]
                except IndexError:pass
                else:
                    if i-1>=0 and S[i-1][j+1]!='#':
                        S[i-1][j+1]+=1

                try:S[i][j-1]
                except IndexError:pass
                else:
                    if  j-1>=0 and S[i][j-1]!='#':
                        S[i][j-1]+=1
                        
                try:S[i][j+1]
                except IndexError:pass
                else:
                    if  S[i][j+1]!='#':
                        S[i][j+1]+=1

                try:S[i+1][j-1]
                except IndexError:pass
                else:
                    if j-1>=0 and S[i+1][j-1]!='#':
                        S[i+1][j-1]+=1

                try:S[i+1][j]
                except IndexError:pass
                else:
                    if S[i+1][j]!='#':
                        S[i+1][j]+=1

                try:S[i+1][j+1]
                except IndexError:pass
                else:
                    if S[i+1][j+1]!='#':
                        S[i+1][j+1]+=1
for i in S:
    print(*i,sep="")