Problem p03574 (python)

Fixed submission:
H,W=[int(i) for i in input().split(" ")]
S=[input() for i in range(H)]


for k,v in enumerate(S):
  for kk,vv in enumerate(list(v)):
    checkTarget = []
    if vv == "#":
      print("#", end="" )
    else:
      if H==1 and W==1:
        print("0" , end="")
        break
      if k == 0 and kk == 0:
        checkTarget = [                         
                                                   S[k][kk+1],
                                     S[k+1][kk], S[k+1][kk+1]]

      elif k == 0 and kk > 0 and kk < len(v)-1 and H==1:
        checkTarget = [ 
                        S[k][kk-1],             S[k][kk+1],
                        ]

      elif k == 0 and kk > 0 and kk < len(v)-1:
        checkTarget = [ 
                        S[k][kk-1],             S[k][kk+1],
                        S[k+1][kk-1], S[k+1][kk], S[k+1][kk+1]]
      elif k == 0 and kk == len(v)-1 and H==1:
        checkTarget = [ 
                        S[k][kk-1],             
                        ]

      elif k == 0 and kk == len(v)-1:
        checkTarget = [ 
                        S[k][kk-1],             
                        S[k+1][kk-1], S[k+1][kk]]
      elif k > 0 and k < len(S) -1 and kk==0 and W==1:
        checkTarget = [       S[k-1][kk],
                                         
                              S[k+1][kk]]

      elif k > 0 and k < len(S) -1 and kk==0:
        checkTarget = [       S[k-1][kk], S[k-1][kk+1],
                                         S[k][kk+1],
                              S[k+1][kk], S[k+1][kk+1]]

      elif k > 0 and k < len(S) -1 and kk==len(v)-1:
        checkTarget = [ S[k-1][kk-1],  S[k-1][kk],
                        S[k][kk-1],             
                        S[k+1][kk-1], S[k+1][kk]]

      elif k == len(S) -1 and kk == 0 and W==1:
        checkTarget = [   S[k-1][kk]
                                     ]
      elif k == len(S) -1 and kk == 0:
        checkTarget = [   S[k-1][kk], S[k-1][kk+1],
                                        S[k][kk+1]
                                                  ]
      elif k == len(S) -1 and kk> 0 and  kk < len(v)-1:
        checkTarget = [ S[k-1][kk-1],  S[k-1][kk], S[k-1][kk+1],
                        S[k][kk-1],                S[k][kk+1]
                                                                ]

      elif k == len(S) -1 and kk == len(v)-1:
        checkTarget = [ S[k-1][kk-1],  S[k-1][kk], 
                        S[k][kk-1]            
                                                               ]

      else:
        checkTarget = [ S[k-1][kk-1],  S[k-1][kk], S[k-1][kk+1],
                        S[k][kk-1],             S[k][kk+1],
                        S[k+1][kk-1], S[k+1][kk], S[k+1][kk+1]]

      print(len( [ i for i in checkTarget if i == "#"]  ), end="")
  print("", end="\n" if k != len(S)-1 else "" )

Buggy submission (verdict: Runtime Error):
H,W=[int(i) for i in input().split(" ")]
S=[input() for i in range(H)]

for k,v in enumerate(S):
  for kk,vv in enumerate(list(v)):
    checkTarget = []
    if vv == "#":
      print("#", end="" )
    else:
      if k == 0 and kk == 0:
        checkTarget = [                         
                                                   S[k][kk+1],
                                     S[k+1][kk], S[k+1][kk+1]]

      elif k == 0 and kk > 0 and kk < len(v)-1 and H==1:
        checkTarget = [ 
                        S[k][kk-1],             S[k][kk+1],
                        ]

      elif k == 0 and kk > 0 and kk < len(v)-1:
        checkTarget = [ 
                        S[k][kk-1],             S[k][kk+1],
                        S[k+1][kk-1], S[k+1][kk], S[k+1][kk+1]]
      elif k == 0 and kk == len(v)-1 and H==1:
        checkTarget = [ 
                        S[k][kk-1],             
                        ]

      elif k == 0 and kk == len(v)-1:
        checkTarget = [ 
                        S[k][kk-1],             
                        S[k+1][kk-1], S[k+1][kk]]
      elif k > 0 and k < len(S) -1 and kk==0 and W==1:
        checkTarget = [       S[k-1][kk],
                                         
                              S[k+1][kk]]

      elif k > 0 and k < len(S) -1 and kk==0:
        checkTarget = [       S[k-1][kk], S[k-1][kk+1],
                                         S[k][kk+1],
                              S[k+1][kk], S[k+1][kk+1]]

      elif k > 0 and k < len(S) -1 and kk==len(v)-1:
        checkTarget = [ S[k-1][kk-1],  S[k-1][kk],
                        S[k][kk-1],             
                        S[k+1][kk-1], S[k+1][kk]]

      elif k == len(S) -1 and kk == 0 and W==1:
        checkTarget = [   S[k-1][kk]
                                     ]
      elif k == len(S) -1 and kk == 0:
        checkTarget = [   S[k-1][kk], S[k-1][kk+1],
                                        S[k][kk+1]
                                                  ]
      elif k == len(S) -1 and kk> 0 and  kk < len(v)-1:
        checkTarget = [ S[k-1][kk-1],  S[k-1][kk], S[k-1][kk+1],
                        S[k][kk-1],                S[k][kk+1]
                                                                ]

      elif k == len(S) -1 and kk == len(v)-1:
        checkTarget = [ S[k-1][kk-1],  S[k-1][kk], 
                        S[k][kk-1]            
                                                               ]

      else:
        checkTarget = [ S[k-1][kk-1],  S[k-1][kk], S[k-1][kk+1],
                        S[k][kk-1],             S[k][kk+1],
                        S[k+1][kk-1], S[k+1][kk], S[k+1][kk+1]]

      print(len( [ i for i in checkTarget if i == "#"]  ), end="")
  print("", end="\n" if k != len(S)-1 else "" )

Buggy submission (verdict: Runtime Error):
H,W=[int(i) for i in input().split(" ")]
S=[input() for i in range(H)]

for k,v in enumerate(S):
  for kk,vv in enumerate(list(v)):
    checkTarget = []
    if vv == "#":
      print("#", end="" )
    else:
      if k == 0 and kk == 0:
        checkTarget = [                         
                                                   S[k][kk+1],
                                     S[k+1][kk], S[k+1][kk+1]]

      elif k == 0 and kk > 0 and kk < len(v)-1:
        checkTarget = [ 
                        S[k][kk-1],             S[k][kk+1],
                        S[k+1][kk-1], S[k+1][kk], S[k+1][kk+1]]
      elif k == 0 and kk == len(v)-1:
        checkTarget = [ 
                        S[k][kk-1],             
                        S[k+1][kk-1], S[k+1][kk]]
      elif k > 0 and k < len(S) -1 and kk==0 and W==1:
        checkTarget = [       S[k-1][kk],
                                         
                              S[k+1][kk]]

      elif k > 0 and k < len(S) -1 and kk==0:
        checkTarget = [       S[k-1][kk], S[k-1][kk+1],
                                         S[k][kk+1],
                              S[k+1][kk], S[k+1][kk+1]]

      elif k > 0 and k < len(S) -1 and kk==len(v)-1:
        checkTarget = [ S[k-1][kk-1],  S[k-1][kk],
                        S[k][kk-1],             
                        S[k+1][kk-1], S[k+1][kk]]

      elif k == len(S) -1 and kk == 0 and W==1:
        checkTarget = [   S[k-1][kk]
                                     ]
      elif k == len(S) -1 and kk == 0:
        checkTarget = [   S[k-1][kk], S[k-1][kk+1],
                                        S[k][kk+1]
                                                  ]
      elif k == len(S) -1 and kk> 0 and  kk < len(v)-1:
        checkTarget = [ S[k-1][kk-1],  S[k-1][kk], S[k-1][kk+1],
                        S[k][kk-1],                S[k][kk+1]
                                                                ]

      elif k == len(S) -1 and kk == len(v)-1:
        checkTarget = [ S[k-1][kk-1],  S[k-1][kk], 
                        S[k][kk-1]            
                                                               ]

      else:
        checkTarget = [ S[k-1][kk-1],  S[k-1][kk], S[k-1][kk+1],
                        S[k][kk-1],             S[k][kk+1],
                        S[k+1][kk-1], S[k+1][kk], S[k+1][kk+1]]

      print(len( [ i for i in checkTarget if i == "#"]  ), end="")
  print("", end="\n" if k != len(S)-1 else "" )

Buggy submission (verdict: Runtime Error):
H,W=[int(i) for i in input().split(" ")]
S=[input() for i in range(H)]

for k,v in enumerate(S):
  for kk,vv in enumerate(list(v)):
    checkTarget = []
    if vv == "#":
      print("#", end="" )
    else:
      if k == 0 and kk == 0:
        checkTarget = [                         
                                                   S[k][kk+1],
                                     S[k+1][kk], S[k+1][kk+1]]

      elif k == 0 and kk > 0 and kk < len(v)-1:
        checkTarget = [ 
                        S[k][kk-1],             S[k][kk+1],
                        S[k+1][kk-1], S[k+1][kk], S[k+1][kk+1]]
      elif k == 0 and kk == len(v)-1:
        checkTarget = [ 
                        S[k][kk-1],             
                        S[k+1][kk-1], S[k+1][kk]]
      elif k > 0 and k < len(S) -1 and kk==0:
        checkTarget = [       S[k-1][kk], S[k-1][kk+1],
                                         S[k][kk+1],
                              S[k+1][kk], S[k+1][kk+1]]

      elif k > 0 and k < len(S) -1 and kk==len(v)-1:
        checkTarget = [ S[k-1][kk-1],  S[k-1][kk],
                        S[k][kk-1],             
                        S[k+1][kk-1], S[k+1][kk]]

      elif k == len(S) -1 and kk == 0:
        checkTarget = [   S[k-1][kk], S[k-1][kk+1],
                                        S[k][kk+1]
                                                  ]
      elif k == len(S) -1 and kk> 0 and  kk < len(v)-1:
        checkTarget = [ S[k-1][kk-1],  S[k-1][kk], S[k-1][kk+1],
                        S[k][kk-1],                S[k][kk+1]
                                                                ]

      elif k == len(S) -1 and kk == len(v)-1:
        checkTarget = [ S[k-1][kk-1],  S[k-1][kk], 
                        S[k][kk-1]            
                                                               ]

      else:
        checkTarget = [ S[k-1][kk-1],  S[k-1][kk], S[k-1][kk+1],
                        S[k][kk-1],             S[k][kk+1],
                        S[k+1][kk-1], S[k+1][kk], S[k+1][kk+1]]

      print(len( [ i for i in checkTarget if i == "#"]  ), end="")
  print("", end="\n" if k != len(S)-1 else "" )