Problem p03574 (python)

Fixed submission:
h,w=map(int,input().split())
slist=[input() for i in range(h)]

if h==1 and w==1:
  s=slist[0]
  if s=="#":
    print(s)
  else:
    print(str(0))
  exit()
  
# 以下どちらかはnon 1

if h==1:
  s_new = ""
  # 1行w列のcase
  s=slist[0]
  for i in range(w):
    if i==0 and s[0]==".":
      if s[1]=="#":
        s_new += str(1)
      else:
        s_new += str(0)
    elif i==w-1 and s[i]==".":
      if s[i-1]=="#":
        s_new += str(1)
      else:
        s_new += str(0)
    elif s[i]==".":
      m=0
      if s[i-1]=="#":
        m+=1
      if s[i+1]=="#":
        m+=1
      s_new += str(m)
    else:
      s_new += "#"
  print(s_new)
  exit()

# 以下 h > 1

if w==1:
  for i in range(h):
    if i==0 and slist[0]==".":
      if slist[1]=="#":
        print(str(1))
      else:
        print(str(0))
    elif i==h-1 and slist[i]==".":
      if slist[i-1]=="#":
        print(str(1))
      else:
        print(str(0))
    elif slist[i]==".":
      m=0
      if slist[i-1]=="#":
        m+=1
      if slist[i+1]=="#":
        m+=1
      print(str(m))
    else:
      print("#")
  exit()

for i in range(h):
  s=slist[i]
  s_new = ""
  if i==0:
    t=slist[1]
    for j in range(w):
      if j==0 and s[0]==".":
        m=0
        if s[1]=="#":
          m+=1
        if t[0]=="#":
          m+=1
        if t[1]=="#":
          m+=1
        s_new += str(m)
      elif j==w-1 and s[w-1]==".":
        m=0
        if s[w-2]=="#":
          m+=1
        if t[w-2]=="#":
          m+=1
        if t[w-1]=="#":
          m+=1
        s_new += str(m)
      elif s[j]==".":
        m=0
        if s[j-1]=="#":
          m+=1
        if s[j+1]=="#":
          m+=1
        if t[j-1]=="#":
          m+=1
        if t[j]=="#":
          m+=1
        if t[j+1]=="#":
          m+=1
        s_new += str(m)
      else:
        s_new += "#"
    print(s_new)
  elif i==h-1:
    r = slist[i-1]
    for j in range(w):
      if j==0 and s[0]==".":
        m=0
        if s[1]=="#":
          m+=1
        if r[0]=="#":
          m+=1
        if r[1]=="#":
          m+=1
        s_new += str(m)
      elif j==w-1 and s[w-1]==".":
        m=0
        if s[w-2]=="#":
          m+=1
        if r[w-2]=="#":
          m+=1
        if r[w-1]=="#":
          m+=1
        s_new += str(m)
      elif s[j]==".":
        m=0
        if s[j-1]=="#":
          m+=1
        if s[j+1]=="#":
          m+=1
        if r[j-1]=="#":
          m+=1
        if r[j]=="#":
          m+=1
        if r[j+1]=="#":
          m+=1
        s_new += str(m)
      else:
        s_new += "#"
    print(s_new)
  else:
    r = slist[i-1]
    t = slist[i+1]
    for j in range(w):
      if j==0 and s[j]==".":
        m=0
        if r[j]=="#":
          m+=1
        if r[j+1]=="#":
          m+=1
        if s[j+1]=="#":
          m+=1
        if t[j+1]=="#":
          m+=1
        if t[j]=="#":
          m+=1
        s_new += str(m)
      elif j==w-1 and s[j]==".":
        m=0
        if r[j]=="#":
          m+=1
        if r[j-1]=="#":
          m+=1
        if s[j-1]=="#":
          m+=1
        if t[j-1]=="#":
          m+=1
        if t[j]=="#":
          m+=1
        s_new += str(m)
      elif s[j]==".":
        m=0
        if r[j]=="#":
          m+=1
        if r[j-1]=="#":
          m+=1
        if s[j-1]=="#":
          m+=1
        if t[j-1]=="#":
          m+=1
        if r[j+1]=="#":
          m+=1
        if s[j+1]=="#":
          m+=1
        if t[j+1]=="#":
          m+=1
        if t[j]=="#":
          m+=1
        s_new += str(m)
      else:
        s_new += "#"
    print(s_new)

Buggy submission (verdict: Runtime Error):
h,w=map(int,input().split())
slist=[input() for i in range(h)]
for i in range(h):
  s=slist[i]
  s_new = ""
  if i==0:
    t=slist[1]
    for j in range(w):
      if j==0 and s[0]==".":
        m=0
        if s[1]=="#":
          m+=1
        if t[0]=="#":
          m+=1
        if t[1]=="#":
          m+=1
        s_new += str(m)
      elif j==w-1 and s[w-1]==".":
        m=0
        if s[w-2]=="#":
          m+=1
        if t[w-2]=="#":
          m+=1
        if t[w-1]=="#":
          m+=1
        s_new += str(m)
      elif s[j]==".":
        m=0
        if s[j-1]=="#":
          m+=1
        if s[j+1]=="#":
          m+=1
        if t[j-1]=="#":
          m+=1
        if t[j]=="#":
          m+=1
        if t[j+1]=="#":
          m+=1
        s_new += str(m)
      else:
        s_new += "#"
    print(s_new)
  elif i==h-1:
    r = slist[i-1]
    for j in range(w):
      if j==0 and s[0]==".":
        m=0
        if s[1]=="#":
          m+=1
        if r[0]=="#":
          m+=1
        if r[1]=="#":
          m+=1
        s_new += str(m)
      elif j==w-1 and s[w-1]==".":
        m=0
        if s[w-2]=="#":
          m+=1
        if r[w-2]=="#":
          m+=1
        if r[w-1]=="#":
          m+=1
        s_new += str(m)
      elif s[j]==".":
        m=0
        if s[j-1]=="#":
          m+=1
        if s[j+1]=="#":
          m+=1
        if r[j-1]=="#":
          m+=1
        if r[j]=="#":
          m+=1
        if r[j+1]=="#":
          m+=1
        s_new += str(m)
      else:
        s_new += "#"
    print(s_new)
  else:
    r = slist[i-1]
    t = slist[i+1]
    for j in range(w):
      if j==0 and s[j]==".":
        m=0
        if r[j]=="#":
          m+=1
        if r[j+1]=="#":
          m+=1
        if s[j+1]=="#":
          m+=1
        if t[j+1]=="#":
          m+=1
        if t[j]=="#":
          m+=1
        s_new += str(m)
      elif j==w-1 and s[j]==".":
        m=0
        if r[j]=="#":
          m+=1
        if r[j-1]=="#":
          m+=1
        if s[j-1]=="#":
          m+=1
        if t[j-1]=="#":
          m+=1
        if t[j]=="#":
          m+=1
        s_new += str(m)
      elif s[j]==".":
        m=0
        if r[j]=="#":
          m+=1
        if r[j-1]=="#":
          m+=1
        if s[j-1]=="#":
          m+=1
        if t[j-1]=="#":
          m+=1
        if r[j+1]=="#":
          m+=1
        if s[j+1]=="#":
          m+=1
        if t[j+1]=="#":
          m+=1
        if t[j]=="#":
          m+=1
        s_new += str(m)
      else:
        s_new += "#"
    print(s_new)