Problem p03574 (python)

Fixed submission:
H,W=map(int,input().split())
S=[[] for i in range(H+2)]
S[0]=list("_"*(W+2))
S[H+1]=list("_"*(W+2))
for i in range(H):
  S[i+1]=list("_"+str(input())+"_")
for i in range(1,H+1):
  for j in range(1,W+1):
    if S[i][j]!="#":
      bomb=0
      for h in [-1,0,1]:
        for w in [-1,0,1]:    
          if S[i+h][j+w]=="#": bomb+=1
      S[i][j]=bomb
        
for i in range(H+2):
  print(''.join(map(str, S[i])).replace("_",""))

Buggy submission (verdict: Runtime Error):
H,W=map(int,input().split())
S=[[] for i in range(H)]
for i in range(H):
  S[i]=list(str(input()))
ans=[[] for i in range(H)]
for i in range(H):
  for j in range(W):
    if i==0:
      if j==0:
        bomb=0
        if S[i+1][j]=="#": bomb+=1
        if S[i][j+1]=="#": bomb+=1
        if S[i+1][j+1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["0"]
      elif j==W-1:
        bomb=0
        if S[i+1][j]=="#": bomb+=1
        if S[i][j-1]=="#": bomb+=1
        if S[i+1][j-1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["0"]
      else:
        bomb=0
        if S[i+1][j]=="#": bomb+=1
        if S[i][j-1]=="#": bomb+=1
        if S[i+1][j-1]=="#": bomb+=1
        if S[i][j+1]=="#": bomb+=1
        if S[i+1][j+1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["0"]
    elif i==H-1:
      if j==0:
        bomb=0
        if S[i-1][j]=="#": bomb+=1
        if S[i][j+1]=="#": bomb+=1
        if S[i-1][j+1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["0"]
      elif j==W-1:
        bomb=0
        if S[i-1][j]=="#": bomb+=1
        if S[i][j-1]=="#": bomb+=1
        if S[i-1][j-1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["0"]
      else:
        bomb=0
        if S[i-1][j]=="#": bomb+=1
        if S[i][j-1]=="#": bomb+=1
        if S[i-1][j-1]=="#": bomb+=1
        if S[i][j+1]=="#": bomb+=1
        if S[i-1][j+1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["0"]
    else:
      if j==0:
        bomb=0
        if S[i-1][j]=="#": bomb+=1
        if S[i+1][j]=="#": bomb+=1
        if S[i][j+1]=="#": bomb+=1
        if S[i-1][j+1]=="#": bomb+=1
        if S[i+1][j+1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["0"]
      elif j==W-1:
        bomb=0
        if S[i-1][j]=="#": bomb+=1
        if S[i+1][j]=="#": bomb+=1
        if S[i][j-1]=="#": bomb+=1
        if S[i-1][j-1]=="#": bomb+=1
        if S[i+1][j-1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["0"]
      else:
        bomb=0
        if S[i-1][j]=="#": bomb+=1
        if S[i+1][j]=="#": bomb+=1
        if S[i][j-1]=="#": bomb+=1
        if S[i-1][j-1]=="#": bomb+=1
        if S[i+1][j-1]=="#": bomb+=1
        if S[i][j+1]=="#": bomb+=1
        if S[i-1][j+1]=="#": bomb+=1
        if S[i+1][j+1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["0"]
          
for i in range(H):
  print(''.join(map(str, ans[i])))

Buggy submission (verdict: Runtime Error):
H,W=map(int,input().split())
S=[[] for i in range(H)]
for i in range(H):
  S[i]=list(str(input()))
ans=[[] for i in range(H)]
for i in range(H):
  for j in range(W):
    if i==0:
      if j==0:
        bomb=0
        if S[i+1][j]=="#": bomb+=1
        if S[i][j+1]=="#": bomb+=1
        if S[i+1][j+1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["."]
      elif j==W-1:
        bomb=0
        if S[i+1][j]=="#": bomb+=1
        if S[i][j-1]=="#": bomb+=1
        if S[i+1][j-1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["."]
      else:
        bomb=0
        if S[i+1][j]=="#": bomb+=1
        if S[i][j-1]=="#": bomb+=1
        if S[i+1][j-1]=="#": bomb+=1
        if S[i][j+1]=="#": bomb+=1
        if S[i+1][j+1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["."]
    elif i==H-1:
      if j==0:
        bomb=0
        if S[i-1][j]=="#": bomb+=1
        if S[i][j+1]=="#": bomb+=1
        if S[i-1][j+1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["."]
      elif j==W-1:
        bomb=0
        if S[i-1][j]=="#": bomb+=1
        if S[i][j-1]=="#": bomb+=1
        if S[i-1][j-1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["."]
      else:
        bomb=0
        if S[i-1][j]=="#": bomb+=1
        if S[i][j-1]=="#": bomb+=1
        if S[i-1][j-1]=="#": bomb+=1
        if S[i][j+1]=="#": bomb+=1
        if S[i-1][j+1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["."]
    else:
      if j==0:
        bomb=0
        if S[i-1][j]=="#": bomb+=1
        if S[i+1][j]=="#": bomb+=1
        if S[i][j+1]=="#": bomb+=1
        if S[i-1][j+1]=="#": bomb+=1
        if S[i+1][j+1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["."]
      elif j==W-1:
        bomb=0
        if S[i-1][j]=="#": bomb+=1
        if S[i+1][j]=="#": bomb+=1
        if S[i][j-1]=="#": bomb+=1
        if S[i-1][j-1]=="#": bomb+=1
        if S[i+1][j-1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["."]
      else:
        bomb=0
        if S[i-1][j]=="#": bomb+=1
        if S[i+1][j]=="#": bomb+=1
        if S[i][j-1]=="#": bomb+=1
        if S[i-1][j-1]=="#": bomb+=1
        if S[i+1][j-1]=="#": bomb+=1
        if S[i][j+1]=="#": bomb+=1
        if S[i-1][j+1]=="#": bomb+=1
        if S[i+1][j+1]=="#": bomb+=1
        if S[i][j]=="#": ans[i]+=["#"]
        elif bomb: ans[i]+=[bomb]
        else: ans[i]+=["."]
          
for i in range(H):
  print(''.join(map(str, ans[i])))