Buggy python submission (verdict: Runtime Error):
h,w=map(int,input().split())
s=[]
for _ in range(h):
    s.append(list(input()))

for i in range(h):
    for j in range(w):
        count=0
        if s[i][j]==".":
            if ((i==0)&(j==0))|((i==0)&(j==w-1))|((i==h-1)&(j==0))|((i==h-1)&(j==w-1)):
                if (i==0)&(j==0):
                    if s[1][0]=="#":
                        count+=1
                    if s[0][1]=="#":
                        count+=1
                    if s[1][1]=="#":
                        count+=1
                if (i==0)&(j==w-1):
                    if s[1][w-1]=="#":
                        count+=1
                    if s[0][w-2]=="#":
                        count+=1
                    if s[1][w-2]=="#":
                        count+=1
                if (i==h-1)&(j==0):
                    if s[h-2][0]=="#":
                        count+=1
                    if s[h-1][1]=="#":
                        count+=1
                    if s[h-2][1]=="#":
                        count+=1
                if (i==h-1)&(j==w-1):
                    if s[h-1][w-2]=="#":
                        count+=1
                    if s[h-2][w-1]=="#":
                        count+=1
                    if s[h-2][w-2]=="#":
                        count+=1
            elif i==0:
                if s[i][j-1]=="#":
                    count+=1
                if s[i][j+1]=="#":
                    count+=1
                if s[i+1][j-1]=="#":
                    count+=1
                if s[i+1][j]=="#":
                    count+=1
                if s[i+1][j+1]=="#":
                    count+=1
            elif i==h-1:
                if s[i][j-1]=="#":
                    count+=1
                if s[i][j+1]=="#":
                    count+=1
                if s[i-1][j-1]=="#":
                    count+=1
                if s[i-1][j]=="#":
                    count+=1
                if s[i-1][j+1]=="#":
                    count+=1
            elif j==0:
                if s[i-1][j]=="#":
                    count+=1
                if s[i+1][j]=="#":
                    count+=1
                if s[i-1][j+1]=="#":
                    count+=1
                if s[i][j+1]=="#":
                    count+=1
                if s[i+1][j+1]=="#":
                    count+=1
            elif j==w-1:
                if s[i-1][j]=="#":
                    count+=1
                if s[i+1][j]=="#":
                    count+=1
                if s[i-1][j-1]=="#":
                    count+=1
                if s[i][j-1]=="#":
                    count+=1
                if s[i+1][j-1]=="#":
                    count+=1
            else:
                if s[i-1][j-1]=="#":
                    count+=1
                if s[i-1][j]=="#":
                    count+=1
                if s[i-1][j+1]=="#":
                    count+=1
                if s[i][j-1]=="#":
                    count+=1
                if s[i][j+1]=="#":
                    count+=1
                if s[i+1][j-1]=="#":
                    count+=1
                if s[i+1][j]=="#":
                    count+=1
                if s[i+1][j+1]=="#":
                    count+=1
            s[i][j]=str(count)
for a in range(h):
    print("".join(s[a]))

Fixed submission:
h,w=map(int,input().split())
s=[]
for _ in range(h):
    s.append(list(input()))

dx=[-1,-1,-1,0,0,1,1,1]
dy=[1,0,-1,1,-1,1,0,-1]

for i in range(h):
    for j in range(w):
        if s[i][j]=="#":
            continue

        count=0
        for k in range(8):
            di=i+dx[k]
            dj=j+dy[k]

            if (di<0)|(di>h-1):
                continue
            if (dj<0)|(dj>w-1):
                continue

            if s[di][dj]=="#":
                count+=1

        s[i][j]=str(count)

for _ in range(h):
    print("".join(s[_]))