Problem p03574 (python)

Fixed submission:
h,w = list(map(int,input().split()))
s=[i for i in range(h)]
for i in range(h):
  s[i] = input()
count = [[0 for i in range(w)]for j in range(h)]
if h==1and w==1:
  if s=="#":
    print("#")
    exit()
  else:
    print(0)
    exit()
  
elif h==1:
  for i in range(w):
    if s[0][i]=="#":
      if i==0:
        count[0][i+1]+=1
      elif i==w-1:
        count[0][i-1]+=1
      else:
        count[0][i-1]+=1
        count[0][i+1]+=1
  ans=""
  for i in range(w):
    if s[0][i]!="#":
      ans+=str(count[0][i])
    else:
      ans+="#"
  print(ans)
  exit()
elif w==1:
  for i in range(h):
    if s[i][0]=="#":
      if i==0:
        count[i+1][0]+=1
      elif i==h-1:
        count[i-1][0]+=1
      else:
        count[i-1][0]+=1
        count[i+1][0]+=1
  
  for i in range(h):
    ans=""
    if s[i][0]!="#":
      ans+=str(count[i][0])
    else:
      ans+="#"
    print(ans)
  exit()


for i in range(h):
  for j in range(w):
    if s[i][j]=="#":
      if i==0:
        if j==0:
          count[i+1][j]+=1
          count[i+1][j+1]+=1
          count[i][j+1]+=1
        elif j==w-1:
          count[i][j-1]+=1
          count[i+1][j-1]+=1
          count[i+1][j]+=1
        else:
          count[i][j-1]+=1
          count[i][j+1]+=1
          count[i+1][j-1]+=1
          count[i+1][j]  +=1
          count[i+1][j+1]+=1
      elif i==h-1:
        if j==0:
          count[i][j+1]+=1
          count[i-1][j]+=1
          count[i-1][j+1]+=1
        elif j==w-1:
          count[i][j-1]+=1
          count[i-1][j-1]+=1
          count[i-1][j]+=1
        else:
          count[i][j-1]+=1
          count[i][j+1]+=1
          count[i-1][j-1]+=1
          count[i-1][j]+=1
          count[i-1][j+1]+=1
      else:
        if j==0:
          count[i-1][j]+=1
          count[i-1][j+1]+=1
          count[i][j+1]+=1
          count[i+1][j]+=1
          count[i+1][j+1]+=1
        elif j==w-1:
          count[i-1][j-1]+=1
          count[i-1][j]+=1
          count[i][j-1]+=1
          count[i+1][j-1]+=1
          count[i+1][j]+=1
        else:
          count[i-1][j-1]+=1
          count[i-1][j]+=1
          count[i-1][j+1]+=1
          count[i][j-1]+=1
          count[i][j+1]+=1
          count[i+1][j-1]+=1
          count[i+1][j]+=1
          count[i+1][j+1]+=1

for i in range(h):
  ans=""
  for j in range(w):
    if s[i][j]!="#":
  	  ans+=str(count[i][j])
    else:
      ans+="#"
  print(ans)

Buggy submission (verdict: Runtime Error):
h,w = list(map(int,input().split()))
s=[i for i in range(h)]
for i in range(h):
  s[i] = input()
#0行、0列、hー1行、wー1列が場合わけ、ただし4隅は重複
#s[0][0],s[0][w-1],s[h-1][0],s[h-1][w-1]
count = [[0 for i in range(w)]for j in range(h)]

for i in range(h):
  for j in range(w):
    if s[i][j]=="#":
      if i==0:
        if j==0:
          count[i+1][j]+=1
          count[i+1][j+1]+=1
          count[i][j+1]+=1
        elif j==w-1:
          count[i][j-1]+=1
          count[i+1][j-1]+=1
          count[i+1][j]+=1
        else:
          count[i][j-1]+=1
          count[i][j+1]+=1
          count[i+1][j-1]+=1
          count[i+1][j]  +=1
          count[i+1][j+1]+=1
      elif i==h-1:
        if j==0:
          count[i][j+1]+=1
          count[i-1][j]+=1
          count[i-1][j+1]+=1
        elif j==w-1:
          count[i][j-1]+=1
          count[i-1][j-1]+=1
          count[i-1][j]+=1
        else:
          count[i][j-1]+=1
          count[i][j+1]+=1
          count[i-1][j-1]+=1
          count[i-1][j]+=1
          count[i-1][j+1]+=1
      else:
        if j==0:
          count[i-1][j]+=1
          count[i-1][j+1]+=1
          count[i][j+1]+=1
          count[i+1][j]+=1
          count[i+1][j+1]+=1
        elif j==w-1:
          count[i-1][j-1]+=1
          count[i-1][j]+=1
          count[i][j-1]+=1
          count[i+1][j-1]+=1
          count[i+1][j]+=1
        else:
          count[i-1][j-1]+=1
          count[i-1][j]+=1
          count[i-1][j+1]+=1
          count[i][j-1]+=1
          count[i][j+1]+=1
          count[i+1][j-1]+=1
          count[i+1][j]+=1
          count[i+1][j+1]+=1

for i in range(h):
  ans=""
  for j in range(w):
    if s[i][j]!="#":
  	  ans+=str(count[i][j])
    else:
      ans+="#"
  print(ans)