Problem p03574 (python)

Fixed submission:
H, W = map(int,input().split())
S = [list(input()) for i in range(H)]
ansm = [[""]*W for i in range(H)]
for i in range(H):
    for j in range(W):
        cnt = 0
        if S[i][j]=="#":
            ansm[i][j]="#"
        else:
            if i>0 and j>0:
                if S[i-1][j-1]=="#":
                    cnt +=1
            if i>0:
                if S[i-1][j]=="#":
                    cnt+=1
            if i>0 and j<(W-1):
                if S[i-1][j+1]=="#":
                    cnt+=1
            if j>0:
                if S[i][j-1]=="#":
                    cnt+=1
            if j<W-1:
                if S[i][j+1]=="#":
                    cnt+=1
            if i<H-1 and j>0:
                if S[i+1][j-1]=="#":
                    cnt+=1
            if i<H-1:
                if S[i+1][j]=="#":
                    cnt+=1
            if i<H-1 and j<(W-1):
                if S[i+1][j+1]=="#":
                    cnt+=1
            ansm[i][j]=str(cnt)
ans = [""]*H
for i in range(H):
    for j in range(W):
        ans[i]=ans[i]+ansm[i][j]
for i in range(H):
    print(ans[i])

Buggy submission (verdict: Runtime Error):
H, W = map(int,input().split())
S = [list(input()) for i in range(H)]
ansm = [[""]*W for i in range(H)]
for i in range(H):
    for j in range(W):
        cnt = 0
        if S[i][j]=="#":
            ansm[i][j]="#"
        else:
            if i>0 and j>0:
                if S[i-1][j-1]=="#":
                    cnt +=1
            if i>0:
                if S[i-1][j]=="#":
                    cnt+=1
            if i>0 and j<(W-1):
                if S[i-1][j+1]=="#":
                    cnt+=1
            if j>0:
                if S[i][j-1]=="#":
                    cnt+=1
            if j<W-1:
                if S[i][j+1]=="#":
                    cnt+=1
            if i<H-1 and j>0:
                if S[i+1][j-1]=="#":
                    cnt+=1
            if i<H-1:
                if S[i+1][j]=="#":
                    cnt+=1
            if i<H-1 and j<(W-1):
                if S[i+1][j+1]=="#":
                    cnt+=1
            ansm[i][j]=str(cnt)
ans = [""]*W
for i in range(H):
    for j in range(W):
        ans[i]=ans[i]+ansm[i][j]
for i in range(H):
    print(ans[i])

Buggy submission (verdict: Runtime Error):
H, W = map(int,input().split())
S = [list(input()) for i in range(H)]
ansm = [[""]*W for i in range(H)]
for i in range(H):
    for j in range(W):
        cnt = 0
        if S[i][j]=="#":
            ansm[i][j]="#"
        else:
            if i>0 and j>0:
                if S[i-1][j-1]=="#":
                    cnt +=1
            if i>0:
                if S[i-1][j]=="#":
                    cnt+=1
            if i>0 and j<(W-1):
                if S[i-1][j+1]=="#":
                    cnt+=1
            if j>0:
                if S[i][j-1]=="#":
                    cnt+=1
            if j<W-1:
                if S[i][j+1]=="#":
                    cnt+=1
            if i<H-1 and j>0:
                if S[i+1][j-1]=="#":
                    cnt+=1
            if i<H-1:
                if S[i+1][j]=="#":
                    cnt+=1
            if i<H-1 and j<(W-1):
                if S[i+1][j+1]=="#":
                    cnt+=1
            ansm[i][j]=str(cnt)
ans = [""]*W
for i in range(H):
    for j in range(W):
        ans[i]=ans[i]+ansm[i][j]
for i in range(H):
    print(ans[i])

Buggy submission (verdict: Runtime Error):
H, W = map(int,input().split())
S = [list(input()) for i in range(H)]
ansm = [[""]*W for i in range(H)]
for i in range(H):
    for j in range(W):
        cnt = 0
        if S[i][j]=="#":
            ansm[i][j]="#"
        else:
            if i>0 and j>0:
                if S[i-1][j-1]=="#":
                    cnt +=1
            if i>0:
                if S[i-1][j]=="#":
                    cnt+=1
            if i>0 and j<(W-1):
                if S[i-1][j+1]=="#":
                    cnt+=1
            if j>0:
                if S[i][j-1]=="#":
                    cnt+=1
            if j<W-1:
                if S[i][j+1]=="#":
                    cnt+=1
            if i<H-1 and j>0:
                if S[i+1][j-1]=="#":
                    cnt+=1
            if i<H-1:
                if S[i+1][j]=="#":
                    cnt+=1
            if i<H-1 and j<(W-1):
                if S[i+1][j+1]=="#":
                    cnt+=1
            ansm[i][j]=str(cnt)
ans = [""]*3
for i in range(H):
    for j in range(W):
        ans[i]=ans[i]+ansm[i][j]
for i in range(H):
    print(ans[i])