Problem p03574 (python)

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

move = [[0,1],[0,-1],[1,0],[-1,0],[1,1],[-1,1],[1,-1],[-1,-1]]
for i in range(h):
    ans = []
    for j in range(w):
        cnt = "#"
        if board[i][j] == "#":
            ans.append("#")
            continue
        cnt = 0
        for mx,my in move:
            x,y = i+mx,j+my
            if 0 > x or x >= h or 0 > y or y >= w:continue
            if board[x][y] == "#":
                cnt += 1
        ans.append(str(cnt))
    print("".join(ans))

Buggy submission (verdict: Runtime Error):
from collections import deque
import heapq
import itertools
import math
from collections import Counter


h,w=map(int,input().split())
s=[]
neighbor=[-1,0,1]
for i in range(h):
    tmp=list(input())
    s.append(tmp)
ans=[[-1]*w for i in range(h)]
for i in range(h):
    for j in range(w):
        cnt=0
        if s[i][j]=="#":
            ans[i][j]="#"
        elif i==0:
            if j==0:
                if s[0][1]=="#":
                    cnt+=1
                if s[1][1]=="#":
                    cnt+=1
                if s[1][0]=="#":
                    cnt+=1
                ans[0][0]=str(cnt)
            elif j==w-1:
                if s[0][w-2]=="#":
                    cnt+=1
                if s[1][w-2]=="#":
                    cnt+=1
                if s[1][w-1]=="#":
                    cnt+=1
                ans[0][w-1]=str(cnt)
            else:
                if s[0][j+1]=="#":
                    cnt+=1
                if s[0][j-1]=="#":
                    cnt+=1
                for k in neighbor:
                    if s[1][j+k]=="#":
                        cnt+=1
                ans[0][j]=str(cnt)
        elif i==h-1:
            if j==w-1:
                if s[h-2][w-1]=="#":
                    cnt+=1
                if s[h-2][w-2]=="#":
                    cnt+=1
                if s[h-1][w-2]=="#":
                    cnt+=1
                ans[h-1][w-1]=str(cnt)
            elif j==0:
                if s[h-1][1]=="#":
                    cnt+=1
                if s[h-2][1]=="#":
                    cnt+=1
                if s[h-1][0]=="#":
                    cnt+=1
                ans[h-1][0]=str(cnt)
            else:
                if s[h-1][j+1]=="#":
                    cnt+=1
                if s[h-1][j-1]=="#":
                    cnt+=1
                for k in neighbor:
                    if s[h-2][j+k]=="#":
                        cnt+=1
                ans[h-1][j]=str(cnt)
        elif j==0:
            if s[i+1][0]=="#":
                cnt+=1
            if s[i-1][0]=="#":
                cnt+=1
            for k in neighbor:
                if s[i+k][1]=="#":
                    cnt+=1
            ans[i][0]=str(cnt)
        elif j==w-1:
            if s[i+1][w-1]=="#":
                cnt+=1
            if s[i-1][w-1]=="#":
                cnt+=1
            for k in neighbor:
                if s[i+k][w-2]=="#":
                    cnt+=1
            ans[i][w-1]=str(cnt)
        else:
            for k in neighbor:
                if s[i+k][j-1]=="#":
                    cnt+=1
                if s[i+k][j+1]=="#":
                    cnt+=1
            if s[i+1][j]=="#":
                    cnt+=1
            if s[i-1][j]=="#":
                    cnt+=1
            ans[i][j]=str(cnt)
for i in range(h):
    answer=''.join(ans[i])
    print(answer)

Buggy submission (verdict: Runtime Error):
from collections import deque
import heapq
import itertools
import math
from collections import Counter


h,w=map(int,input().split())
s=[]
neighbor=[-1,0,1]
for i in range(h):
    tmp=list(input())
    s.append(tmp)
ans=[[-1]*w for i in range(h)]
for i in range(h):
    for j in range(w):
        cnt=0
        if s[i][j]=="#":
            ans[i][j]="#"
        elif i==0:
            if j==0:
                if s[0][1]=="#":
                    cnt+=1
                if s[1][1]=="#":
                    cnt+=1
                if s[1][0]=="#":
                    cnt+=1
                ans[0][0]=str(cnt)
            elif j==w-1:
                if s[0][w-2]=="#":
                    cnt+=1
                if s[1][w-2]=="#":
                    cnt+=1
                if s[1][w-1]=="#":
                    cnt+=1
                ans[0][w-1]=str(cnt)
            else:
                if s[0][j+1]=="#":
                    cnt+=1
                if s[0][j-1]=="#":
                    cnt+=1
                for k in neighbor:
                    if s[1][j+k]=="#":
                        cnt+=1
                ans[0][j]=str(cnt)
        elif i==h-1:
            if j==w-1:
                if s[h-2][w-1]=="#":
                    cnt+=1
                if s[h-2][w-2]=="#":
                    cnt+=1
                if s[h-1][w-2]=="#":
                    cnt+=1
                ans[h-1][w-1]=str(cnt)
            elif j==0:
                if s[h-1][1]=="#":
                    cnt+=1
                if s[h-2][1]=="#":
                    cnt+=1
                if s[h-1][0]=="#":
                    cnt+=1
                ans[h-1][0]=str(cnt)
            else:
                if s[h-1][j+1]=="#":
                    cnt+=1
                if s[h-1][j+1]=="#":
                    cnt+=1
                for k in neighbor:
                    if s[h-2][j+k]=="#":
                        cnt+=1
                ans[h-1][j]=str(cnt)
        elif j==0:
            if s[i+1][0]=="#":
                cnt+=1
            if s[i-1][0]=="#":
                cnt+=1
            for k in neighbor:
                if s[i+k][1]=="#":
                    cnt+=1
            ans[i][0]=str(cnt)
        elif j==w-1:
            if s[i+1][w-1]=="#":
                cnt+=1
            if s[i-1][w-1]=="#":
                cnt+=1
            for k in neighbor:
                if s[i+k][w-2]=="#":
                    cnt+=1
            ans[i][w-1]=str(cnt)
        else:
            for k in neighbor:
                if s[i+k][j-1]=="#":
                    cnt+=1
                if s[i+k][j+1]=="#":
                    cnt+=1
            if s[i+1][j]=="#":
                    cnt+=1
            if s[i-1][j]=="#":
                    cnt+=1
            ans[i][j]=str(cnt)
for i in range(h):
    answer=''.join(ans[i])
    print(answer)

Buggy submission (verdict: Runtime Error):
from collections import deque
import heapq
import itertools
import math
from collections import Counter


h,w=map(int,input().split())
s=[]
neighbor=[-1,0,1]
for i in range(h):
    tmp=list(input())
    s.append(tmp)
ans=[[-1]*w for i in range(h)]
for i in range(h):
    for j in range(w):
        cnt=0
        if s[i][j]=="#":
            ans[i][j]="#"
        elif i==0:
            if j==0:
                if s[0][1]=="#":
                    cnt+=1
                if s[1][1]=="#":
                    cnt+=1
                if s[1][0]=="#":
                    cnt+=1
                ans[0][0]=str(cnt)
            elif j==w-1:
                if s[0][w-2]=="#":
                    cnt+=1
                if s[1][w-2]=="#":
                    cnt+=1
                if s[1][w-1]=="#":
                    cnt+=1
                ans[0][w-1]=str(cnt)
            else:
                if s[0][j+1]=="#":
                    cnt+=1
                if s[0][j-1]=="#":
                    cnt+=1
                for k in neighbor:
                    if s[1][j+k]=="#":
                        cnt+=1
                ans[0][j]=str(cnt)
        elif i==h-1:
            if j==w-1:
                if s[h-2][w-1]=="#":
                    cnt+=1
                if s[h-2][w-2]=="#":
                    cnt+=1
                if s[h-1][w-2]=="#":
                    cnt+=1
                ans[h-1][w-1]=str(cnt)
            elif j==0:
                if s[h-1][1]=="#":
                    cnt+=1
                if s[h-2][1]=="#":
                    cnt+=1
                if s[h-1][0]=="#":
                    cnt+=1
                ans[h-1][0]=str(cnt)
            else:
                if s[h-1][j+1]=="#":
                    cnt+=1
                if s[h-1][j+1]=="#":
                    cnt+=1
                for k in neighbor:
                    if s[h-2][j+k]=="#":
                        cnt+=1
                ans[h-1][j]=str(cnt)
        elif j==0:
            if s[i+1][0]=="#":
                cnt+=1
            if s[i-1][0]=="#":
                cnt+=1
            for k in neighbor:
                if s[i+k][1]=="#":
                    cnt+=1
            ans[i][0]=str(cnt)
        elif j==w-1:
            if s[i+1][w-1]=="#":
                cnt+=1
            if s[i-1][w-1]=="#":
                cnt+=1
            for k in neighbor:
                if s[i+k][w-2]=="#":
                    cnt+=1
            ans[i][w-1]=str(cnt)
        else:
            for k in neighbor:
                if s[i+k][j-1]=="#":
                    cnt+=1
                if s[i+k][j+1]=="#":
                    cnt+=1
                if s[i+1][j]=="#":
                    cnt+=1
                if s[i-1][j]=="#":
                    cnt+=1
            ans[i][j]=str(cnt)
for i in range(h):
    answer=''.join(ans[i])
    print(answer)