Problem p03574 (python)

Fixed submission:
a = input().split()
H = int(a[0])
W = int(a[1])

ans = []
for i in range(H+2):
 ans.append([])
 for j in range(W+2):
   ans[i].append(0)

S = []
S.append([])
for j in range(W+2):
   S[0].append('.')
for i in range(H):
  s = input()
  s = list(s)
  s.insert(0,'.')
  s.append('.')
  S.append(s)
S.append([])
for j in range(W+2):
   S[-1].append('.')

for i in range(H+2):
  if((i != 0)and(i != H+1)):
   for j in range(W+2):
    if((j != 0)and(j != W+1)):
      if(S[i][j] == '#'):
        ans[i][j] = '#'
      else:
       if(S[i-1][j] == '#'):#上
        ans[i][j] += 1
       if(S[i+1][j] == '#'):#下
        ans[i][j] += 1
       if(S[i][j+1] == '#'):#右
        ans[i][j] += 1
       if(S[i][j-1] == '#'):#左
        ans[i][j] += 1 
       if(S[i-1][j+1] == '#'):#右上
        ans[i][j] += 1
       if(S[i-1][j-1] == '#'):#左上
        ans[i][j] += 1
       if(S[i+1][j+1] == '#'):#右下
        ans[i][j] += 1
       if(S[i+1][j-1] == '#'):#左下
        ans[i][j] += 1
        
for i in range(H+2):
  if((i != 0)and(i != H+1)):
   ans[i].pop(0)
   ans[i].pop(-1)
   for j in range(W):
    ans[i][j] = str(ans[i][j])
   print(''.join(ans[i]))

Buggy submission (verdict: Runtime Error):
a = input().split()
H = int(a[0])
W = int(a[1])

S = []
ans_list = [[0]*W]*H
print(ans_list)
print(type(1))
print(type(ans_list[0][0]))
for i in range(H):
  s = input().split()
  s = list(s[0])
  S.append(s)
  
def counter1(i,j):#上
  if(S[i-1][j]=='#'):
     ans_list[i][j] += 1
def counter2(i,j):#下
  if(S[i+1][j]=='#'):
     ans_list[i][j] += 1 
def counter3(i,j):#右
  if(S[i][j+1]=='#'):
     ans_list[i][j] += 1   
def counter4(i,j):#左
  if(S[i][j-1]=='#'):
     ans_list[i][j] += 1  
def counter5(i,j):#右上
  if(S[i-1][j+1]=='#'):
     ans_list[i][j] += 1
def counter6(i,j):#右下
  if(S[i+1][j+1]=='#'):
     ans_list[i][j] += 1
def counter7(i,j):#左上
  if(S[i-1][j-1]=='#'):
     ans_list[i][j] += 1
def counter8(i,j):#左下
  if(S[i+1][j-1]=='#'):
     ans_list[i][j] += 1

for i in range(H):
  for j in range(W):
    if(S[i][j] == '#'):
      ans_list[i][j] = '#'
    elif(S[i][j] == '.'):
      if(i == 0):###1行目
        if(j == 0):
         counter2(i,j)#下
         counter3(i,j)#右
         counter6(i,j)#右下
        elif((j != 0)and(j != W-1)):
         counter2(i,j)#下
         counter3(i,j)#右
         counter4(i,j)#左
         counter6(i,j)#右下
         counter8(i,j)#左下
        elif(j == W-1):
         counter2(i,j)#下
         counter4(i,j)#左
         counter8(i,j)#左下
      elif((i != 0)and(i != H-1)):###真ん中の行
        if(j == 0):
         counter1(i,j)#上
         counter2(i,j)#下
         counter3(i,j)#右
         counter5(i,j)#右上
         counter6(i,j)#右下
        elif((j != 0)and(j != W-1)):
         counter1(i,j)#上
         counter2(i,j)#下
         counter3(i,j)#右
         counter4(i,j)#左
         counter5(i,j)#右上
         counter6(i,j)#右下
         counter7(i,j)#左上
         counter8(i,j)#左下
        elif(j == W-1):
         counter1(i,j)#上
         counter2(i,j)#下
         counter4(i,j)#左
         counter7(i,j)#左上
         counter8(i,j)#左下   
      elif(i == H-1):###最後の行
        if(j == 0):
         counter1(i,j)#上
         counter3(i,j)#右
         counter5(i,j)#右上
        elif((j != 0)and(j != W-1)):
         counter1(i,j)#上
         counter3(i,j)#右
         counter4(i,j)#左
         counter5(i,j)#右上
         counter7(i,j)#左上
        elif(j == W-1):
         counter1(i,j)#上
         counter4(i,j)#左
         counter7(i,j)#左上
  

print(ans_list)

Buggy submission (verdict: Runtime Error):
a=input().split()
H=int(a[0])
W=int(a[1])
A=[]
B=[]

for i in range(H):
  a=input()
  a=list(a)
  if(i!=H-1):
   a.pop()   
  A.append(a)

for i in range(H):
  for j in range(W):
    if(A[i][j]=="."):
     A[i][j]=0

for i in range(H):
  if((i!=0)and(i!=H-1)):
   for j in range(W):
     if((j!=0)and(j!=W-1)):
        if( (A[i-1][j]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i+1][j]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1
        if( (A[i][j-1]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1
        if( (A[i][j+1]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1

for i in range(H):
   for j in range(W): 
      if( (i==0)and(j!=0)and(j!=W-1) ):
        if( (A[i][j-1]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i][j+1]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1
      if( (i==H-1)and(j!=0)and(j!=W-1) ):
        if( (A[i][j-1]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i][j+1]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
      if( (j==0)and(i!=0)and(i!=H-1) ):
        if( (A[i-1][j]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i+1][j]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1
      if( (j==W-1)and(i!=0)and(i!=H-1) ):
        if( (A[i-1][j]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i+1][j]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1
          
      if( (i==0)and(j==0) ):
        if( (A[i+1][j]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i][j+1]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1
      if( (i==0)and(j==W-1) ):
        if( (A[i+1][j]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i][j-1]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1
      if( (i==H-1)and(j==0) ):
        if( (A[i-1][j]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i][j+1]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1
      if( (i==H-1)and(j==W-1) ):
        if( (A[i-1][j]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i][j-1]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1

for i in range(H):
 for j in range(W): 
  A[i][j]=str(A[i][j])

X=""
for i in range(H):
 for j in A[i]:
  X += j
 print(X)
 X=""

Buggy submission (verdict: Runtime Error):
a=input().split()
H=int(a[0])
W=int(a[1])
A=[]
B=[]

for i in range(H):
  a=input()
  a=list(a)
  if(i!=H-1):
   a.pop()   
  A.append(a)

for i in range(H):
  for j in range(W):
    if(A[i][j]=="."):
     A[i][j]=0

for i in range(H):
  if((i!=0)and(i!=H-1)):
   for j in range(W):
     if((j!=0)and(j!=W-1)):
        if( (A[i-1][j]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i+1][j]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1
        if( (A[i][j-1]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1
        if( (A[i][j+1]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1

for i in range(H):
   for j in range(W): 
      if( (i==0)and(j!=0)and(j!=W-1) ):
        if( (A[i][j-1]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i][j+1]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1
      if( (i==H-1)and(j!=0)and(j!=W-1) ):
        if( (A[i][j-1]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i][j+1]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
      if( (j==0)and(i!=0)and(i!=H-1) ):
        if( (A[i-1][j]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i+1][j]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1
      if( (j==W-1)and(i!=0)and(i!=H-1) ):
        if( (A[i-1][j]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i+1][j]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1
          
      if( (i==0)and(j==0) ):
        if( (A[i+1][j]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i][j+1]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1
      if( (i==0)and(j==W-1) ):
        if( (A[i+1][j]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i][j-1]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1
      if( (i==H-1)and(j==0) ):
        if( (A[i-1][j]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i][j+1]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1
      if( (i==H-1)and(j==W-1) ):
        if( (A[i-1][j]=="#")and(A[i][j]!="#") ):
          A[i][j] +=1
        if( (A[i][j-1]=="#")and(A[i][j]!="#") ): 
          A[i][j] +=1

print(A)