Problem p03574 (python)

Fixed submission:
# ABC075 B - Minesweeper
H,W = map(int,input().split())

G = [['' for _ in range(W+2)] for _ in range(H+2)]
Ans = [[0 for _ in range(W+2)] for _ in range(H+2)]

for i in range(1,H+1):
    S = input()
    for j,s in enumerate(S):
        G[i][j+1]= s

for i in range(1,H+1):
    for j in range(W):
        # 1行目
        if i == 1:
            if G[i][j+2] == '#':
                Ans[i][j+1] += 1
            # if G[i][j+1] == '#':
            #     Ans[i][j+1] = '#' 
            if G[i][j] == '#':
                Ans[i][j+1] += 1
            if G[i+1][j+2] == '#':
                Ans[i][j+1] += 1
            if G[i+1][j+1] == '#':
                Ans[i][j+1] += 1
            if G[i+1][j] == '#':
                Ans[i][j+1] += 1
            continue
        # 最終行
        if i == H:
            if G[i][j+2] == '#':
                Ans[i][j+1] += 1
            # if G[i][j+1] == '#':
            #     Ans[i][j+1] = '#' 
            if G[i][j] == '#':
                Ans[i][j+1] += 1
            if G[i-1][j+2] == '#':
                Ans[i][j+1] += 1
            if G[i-1][j+1] == '#':
                Ans[i][j+1] += 1
            if G[i-1][j] == '#':
                Ans[i][j+1] += 1
            continue

        if G[i][j+2] == '#':
            Ans[i][j+1] += 1
        # if G[i][j+1] == '#':
        #     Ans[i][j+1] = '#' 
        if G[i][j] == '#':
            Ans[i][j+1] += 1
        if G[i+1][j+2] == '#':
            Ans[i][j+1] += 1
        if G[i+1][j+1] == '#':
            Ans[i][j+1] += 1
        if G[i+1][j] == '#':
            Ans[i][j+1] += 1 
        if G[i-1][j+2] == '#':
            Ans[i][j+1] += 1
        if G[i-1][j+1] == '#':
            Ans[i][j+1] += 1
        if G[i-1][j] == '#':
            Ans[i][j+1] += 1

for i in range(1,H+1):
    for j in range(1,W+1):
        Ans[i][j] = str(Ans[i][j])
        if G[i][j] == '#':
            Ans[i][j] = '#'

for i in range(1,H+1):
    s = ''
    for j in range(1,W+1):
        s += Ans[i][j]
    print(s)

Buggy submission (verdict: Runtime Error):
h,w=map(int,input().split())
s=[input() for i in range(h)]
a=[[0]*w for i in range(h)]
for i in range(h):
  for j in range(w):
    t=0
    if i==0:
      if j==0:
        if s[i][j+1]=="#":t+=1
        if s[i+1][j+1]=="#":t+=1
        if s[i+1][j]=="#":t+=1
      elif 0<j<w-1:
        if s[i][j+1]=="#":t+=1
        if s[i+1][j+1]=="#":t+=1
        if s[i+1][j]=="#":t+=1
        if s[i+1][j-1]=="#":t+=1
        if s[i][j-1]=="#":t+=1        
      elif j==w-1:
        if s[i+1][j]=="#":t+=1
        if s[i+1][j-1]=="#":t+=1
        if s[i][j-1]=="#":t+=1 
    elif 0<i<h-1:
      if j==0:
        if s[i-1][j]=="#":t+=1
        if s[i-1][j+1]=="#":t+=1
        if s[i][j+1]=="#":t+=1
        if s[i+1][j+1]=="#":t+=1
        if s[i+1][j]=="#":t+=1
      elif 0<j<w-1:
        if s[i][j+1]=="#":t+=1
        if s[i+1][j+1]=="#":t+=1
        if s[i+1][j]=="#":t+=1
        if s[i+1][j-1]=="#":t+=1
        if s[i][j-1]=="#":t+=1
        if s[i-1][j-1]=="#":t+=1
        if s[i-1][j]=="#":t+=1
        if s[i-1][j+1]=="#":t+=1
      elif j==w-1:
        if s[i+1][j]=="#":t+=1
        if s[i+1][j-1]=="#":t+=1
        if s[i][j-1]=="#":t+=1
        if s[i-1][j-1]=="#":t+=1
        if s[i-1][j]=="#":t+=1      
    elif i==h-1:
      if j==0:
        if s[i][j+1]=="#":t+=1
        if s[i-1][j+1]=="#":t+=1
        if s[i-1][j]=="#":t+=1
      elif 0<j<w-1:
        if s[i][j+1]=="#":t+=1
        if s[i-1][j+1]=="#":t+=1
        if s[i-1][j]=="#":t+=1
        if s[i-1][j-1]=="#":t+=1
        if s[i][j-1]=="#":t+=1        
      elif j==w-1:
        if s[i-1][j]=="#":t+=1
        if s[i-1][j-1]=="#":t+=1
        if s[i][j-1]=="#":t+=1
    if s[i][j]=="#":
      a[i][j]="#"
    else:
       a[i][j]=t
for aa in a:
  tt=""
  for aaa in aa:
    tt+=str(aaa)
  print(tt)