Buggy python submission (verdict: Runtime Error):
[h,w] = [int(i) for i in input().split()]
s = []
for i in range(h):
    s.append(input())
ans1 = []
if h != 1 and w != 1:
    for i in range(h):
        ans = []
  #上下のエラーを排除
        if i != 0 and i != h-1:
            for j in range(w):
            #左右のエラーを排除
                if j != 0 and j != w-1:
                    if s[i][j] == ".":
                        ans.append((s[i-1][j]+s[i+1][j]+s[i][j-1]+s[i][j+1]+s[i-1][j-1]+s[i-1][j+1]+s[i+1][j-1]+s[i+1][j+1]).count("#"))
                    else:
                        ans.append(s[i][j])
                elif j == 0:
                    if s[i][j] == ".":
                        ans.append((s[i-1][j]+s[i+1][j]+s[i][j+1]+s[i-1][j+1]+s[i+1][j+1]).count("#"))
                    else:
                        ans.append(s[i][j])
                else:
                    if s[i][j] == ".":
                        ans.append((s[i-1][j]+s[i+1][j]+s[i][j-1]+s[i-1][j-1]+s[i+1][j-1]).count("#"))
                    else:
                        ans.append(s[i][j])
        elif i == 0:
            for j in range(w):
                #左右のエラーを排除
                if j != 0 and j != w-1:
                    if s[i][j] == ".":
                        ans.append((s[i+1][j]+s[i][j-1]+s[i][j+1]+s[i+1][j-1]+s[i+1][j+1]).count("#"))
                    else:
                        ans.append(s[i][j])
                elif j == 0:
                    if s[i][j] == ".":
                        ans.append((s[i+1][j]+s[i][j+1]+s[i+1][j+1]).count("#"))
                    else:
                        ans.append(s[i][j])
                else:
                    if s[i][j] == ".":
                        ans.append((s[i+1][j]+s[i][j-1]+s[i+1][j-1]).count("#"))
                    else:
                        ans.append(s[i][j])
        else:
            for j in range(w):
        #左右のエラーを排除
                if j != 0 and j != w-1:
                    if s[i][j] == ".":
                        ans.append((s[i-1][j]+s[i][j-1]+s[i][j+1]+s[i-1][j-1]+s[i-1][j+1]).count("#"))
                    else:
                        ans.append(s[i][j])
                elif j == 0:
                    if s[i][j] == ".":
                        ans.append((s[i-1][j]+s[i][j+1]+s[i-1][j+1]).count("#"))
                    else:
                        ans.append(s[i][j])
                else:
                    if s[i][j] == ".":
                        ans.append((s[i-1][j]+s[i][j-1]+s[i-1][j-1]).count("#"))
                    else:
                        ans.append(s[i][j])
        ans1.append(ans)

elif h != 1 and w == 1:
    #hは非1でwが1
    for i in range(h):
        ans = []
        if i !=0 and i != h-1:
            if s[i][0] == ".":
                ans.append((s[i-1][0]+s[i+1][0]).count("#"))
            else:
                ans.append(s[i][0])
        elif i == 0:
            if s[i][0] == ".":
                ans.append((s[i+1][0]).count("#"))
            else:
                ans.append(s[i][0])
        else:
            if s[i][0] == ".":
                ans.append((s[i-1][0]).count("#"))
            else:
                ans.append(s[i][0])
        ans1.append(ans)
elif h == 1 and w != 1:
    #hは1でwが非1
    for j in range(w):
        ans = []
        if j != 0 and j!= w-1:
            if s[0][j] == ".":
                ans.append((s[0][j-1]+s[0][j+1]).count("#"))
            else:
                ans.append(s[0][j])
        elif j == 0:
            if s[0][j] == ".":
                ans.append((s[0][j+1]).count("#"))
            else:
                ans.append(s[0][j])
        else:
            if s[0][j] == ".":
                ans.append((s[0][j-1]).count("#"))
            else:
                ans.append(s[0][j])
        ans1.append(ans)
else:
    ans = []
    if s[0][0] == ".":
        ans.append(0)
    else:
        ans.append(s[0][0])
    ans1.append(ans)

for i in range(h):
    for j in range(w):
        print(ans1[i][j],end = "")
    print("")


Fixed submission:
[h,w] = [int(i) for i in input().split()]
s = []
for i in range(h):
    s.append(input())
ans1 = []
if h != 1 and w != 1:
    for i in range(h):
        ans = []
  #上下のエラーを排除
        if i != 0 and i != h-1:
            for j in range(w):
            #左右のエラーを排除
                if j != 0 and j != w-1:
                    if s[i][j] == ".":
                        ans.append((s[i-1][j]+s[i+1][j]+s[i][j-1]+s[i][j+1]+s[i-1][j-1]+s[i-1][j+1]+s[i+1][j-1]+s[i+1][j+1]).count("#"))
                    else:
                        ans.append(s[i][j])
                elif j == 0:
                    if s[i][j] == ".":
                        ans.append((s[i-1][j]+s[i+1][j]+s[i][j+1]+s[i-1][j+1]+s[i+1][j+1]).count("#"))
                    else:
                        ans.append(s[i][j])
                else:
                    if s[i][j] == ".":
                        ans.append((s[i-1][j]+s[i+1][j]+s[i][j-1]+s[i-1][j-1]+s[i+1][j-1]).count("#"))
                    else:
                        ans.append(s[i][j])
        elif i == 0:
            for j in range(w):
                #左右のエラーを排除
                if j != 0 and j != w-1:
                    if s[i][j] == ".":
                        ans.append((s[i+1][j]+s[i][j-1]+s[i][j+1]+s[i+1][j-1]+s[i+1][j+1]).count("#"))
                    else:
                        ans.append(s[i][j])
                elif j == 0:
                    if s[i][j] == ".":
                        ans.append((s[i+1][j]+s[i][j+1]+s[i+1][j+1]).count("#"))
                    else:
                        ans.append(s[i][j])
                else:
                    if s[i][j] == ".":
                        ans.append((s[i+1][j]+s[i][j-1]+s[i+1][j-1]).count("#"))
                    else:
                        ans.append(s[i][j])
        else:
            for j in range(w):
        #左右のエラーを排除
                if j != 0 and j != w-1:
                    if s[i][j] == ".":
                        ans.append((s[i-1][j]+s[i][j-1]+s[i][j+1]+s[i-1][j-1]+s[i-1][j+1]).count("#"))
                    else:
                        ans.append(s[i][j])
                elif j == 0:
                    if s[i][j] == ".":
                        ans.append((s[i-1][j]+s[i][j+1]+s[i-1][j+1]).count("#"))
                    else:
                        ans.append(s[i][j])
                else:
                    if s[i][j] == ".":
                        ans.append((s[i-1][j]+s[i][j-1]+s[i-1][j-1]).count("#"))
                    else:
                        ans.append(s[i][j])
        ans1.append(ans)
    for i in range(h):
        for j in range(w):
            print(ans1[i][j],end = "")
        print("")

elif h != 1 and w == 1:
    #hは非1でwが1
    ans = []
    for i in range(h):
        if i !=0 and i != h-1:
            if s[i][0] == ".":
                ans.append((s[i-1][0]+s[i+1][0]).count("#"))
            else:
                ans.append(s[i][0])
        elif i == 0:
            if s[i][0] == ".":
                ans.append((s[i+1][0]).count("#"))
            else:
                ans.append(s[i][0])
        else:
            if s[i][0] == ".":
                ans.append((s[i-1][0]).count("#"))
            else:
                ans.append(s[i][0])

    for i in range(h):
        print(ans[i])

elif h == 1 and w != 1:
    #hは1でwが非1
    ans = []
    for j in range(w):
        if j != 0 and j!= w-1:
            if s[0][j] == ".":
                ans.append((s[0][j-1]+s[0][j+1]).count("#"))
            else:
                ans.append(s[0][j])
        elif j == 0:
            if s[0][j] == ".":
                ans.append((s[0][j+1]).count("#"))
            else:
                ans.append(s[0][j])
        else:
            if s[0][j] == ".":
                ans.append((s[0][j-1]).count("#"))
            else:
                ans.append(s[0][j])
    for j in range(w):
        print(ans[j],end = "")
else:
    ans = []
    if s[0][0] == ".":
        ans.append(0)
    else:
        ans.append(s[0][0])
    print(ans[0])

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

for i in range(h):
    ans = []
  #上下のエラーを排除
    if i != 0 and i != h-1:
        for j in range(w):
            #左右のエラーを排除
            if j != 0 and j != w-1:
                if s[i][j] == ".":
                    ans.append((s[i-1][j]+s[i+1][j]+s[i][j-1]+s[i][j+1]+s[i-1][j-1]+s[i-1][j+1]+s[i+1][j-1]+s[i+1][j+1]).count("#"))
                else:
                    ans.append(s[i][j])
            elif j == 0:
                if s[i][j] == ".":
                    ans.append((s[i-1][j]+s[i+1][j]+s[i][j+1]+s[i-1][j+1]+s[i+1][j+1]).count("#"))
                else:
                    ans.append(s[i][j])
            else:
                if s[i][j] == ".":
                    ans.append((s[i-1][j]+s[i+1][j]+s[i][j-1]+s[i-1][j-1]+s[i+1][j-1]).count("#"))
                else:
                    ans.append(s[i][j])
    elif i == 0:
        for j in range(w):
            #左右のエラーを排除
            if j != 0 and j != w-1:
                if s[i][j] == ".":
                    ans.append((s[i+1][j]+s[i][j-1]+s[i][j+1]+s[i+1][j-1]+s[i+1][j+1]).count("#"))
                else:
                    ans.append(s[i][j])
            elif j == 0:
                if s[i][j] == ".":
                    ans.append((s[i+1][j]+s[i][j+1]+s[i+1][j+1]).count("#"))
                else:
                    ans.append(s[i][j])
            else:
                if s[i][j] == ".":
                    ans.append((s[i+1][j]+s[i][j-1]+s[i+1][j-1]).count("#"))
                else:
                    ans.append(s[i][j])
    else:
        for j in range(w):
        #左右のエラーを排除
            if j != 0 and j != w-1:
                if s[i][j] == ".":
                    ans.append((s[i-1][j]+s[i][j-1]+s[i][j+1]+s[i-1][j-1]+s[i-1][j+1]).count("#"))
                else:
                    ans.append(s[i][j])
            elif j == 0:
                if s[i][j] == ".":
                    ans.append((s[i-1][j]+s[i][j+1]+s[i-1][j+1]).count("#"))
                else:
                    ans.append(s[i][j])
            else:
                if s[i][j] == ".":
                    ans.append((s[i-1][j]+s[i][j-1]+s[i-1][j-1]).count("#"))
                else:
                    ans.append(s[i][j])
    ans1.append(ans)

for i in range(h):
    for j in range(w):
        print(ans1[i][j],end = "")
    print("\n")


Fixed submission:
[h,w] = [int(i) for i in input().split()]
s = []
for i in range(h):
    s.append(input())
ans1 = []
if h != 1 and w != 1:
    for i in range(h):
        ans = []
  #上下のエラーを排除
        if i != 0 and i != h-1:
            for j in range(w):
            #左右のエラーを排除
                if j != 0 and j != w-1:
                    if s[i][j] == ".":
                        ans.append((s[i-1][j]+s[i+1][j]+s[i][j-1]+s[i][j+1]+s[i-1][j-1]+s[i-1][j+1]+s[i+1][j-1]+s[i+1][j+1]).count("#"))
                    else:
                        ans.append(s[i][j])
                elif j == 0:
                    if s[i][j] == ".":
                        ans.append((s[i-1][j]+s[i+1][j]+s[i][j+1]+s[i-1][j+1]+s[i+1][j+1]).count("#"))
                    else:
                        ans.append(s[i][j])
                else:
                    if s[i][j] == ".":
                        ans.append((s[i-1][j]+s[i+1][j]+s[i][j-1]+s[i-1][j-1]+s[i+1][j-1]).count("#"))
                    else:
                        ans.append(s[i][j])
        elif i == 0:
            for j in range(w):
                #左右のエラーを排除
                if j != 0 and j != w-1:
                    if s[i][j] == ".":
                        ans.append((s[i+1][j]+s[i][j-1]+s[i][j+1]+s[i+1][j-1]+s[i+1][j+1]).count("#"))
                    else:
                        ans.append(s[i][j])
                elif j == 0:
                    if s[i][j] == ".":
                        ans.append((s[i+1][j]+s[i][j+1]+s[i+1][j+1]).count("#"))
                    else:
                        ans.append(s[i][j])
                else:
                    if s[i][j] == ".":
                        ans.append((s[i+1][j]+s[i][j-1]+s[i+1][j-1]).count("#"))
                    else:
                        ans.append(s[i][j])
        else:
            for j in range(w):
        #左右のエラーを排除
                if j != 0 and j != w-1:
                    if s[i][j] == ".":
                        ans.append((s[i-1][j]+s[i][j-1]+s[i][j+1]+s[i-1][j-1]+s[i-1][j+1]).count("#"))
                    else:
                        ans.append(s[i][j])
                elif j == 0:
                    if s[i][j] == ".":
                        ans.append((s[i-1][j]+s[i][j+1]+s[i-1][j+1]).count("#"))
                    else:
                        ans.append(s[i][j])
                else:
                    if s[i][j] == ".":
                        ans.append((s[i-1][j]+s[i][j-1]+s[i-1][j-1]).count("#"))
                    else:
                        ans.append(s[i][j])
        ans1.append(ans)
    for i in range(h):
        for j in range(w):
            print(ans1[i][j],end = "")
        print("")

elif h != 1 and w == 1:
    #hは非1でwが1
    ans = []
    for i in range(h):
        if i !=0 and i != h-1:
            if s[i][0] == ".":
                ans.append((s[i-1][0]+s[i+1][0]).count("#"))
            else:
                ans.append(s[i][0])
        elif i == 0:
            if s[i][0] == ".":
                ans.append((s[i+1][0]).count("#"))
            else:
                ans.append(s[i][0])
        else:
            if s[i][0] == ".":
                ans.append((s[i-1][0]).count("#"))
            else:
                ans.append(s[i][0])

    for i in range(h):
        print(ans[i])

elif h == 1 and w != 1:
    #hは1でwが非1
    ans = []
    for j in range(w):
        if j != 0 and j!= w-1:
            if s[0][j] == ".":
                ans.append((s[0][j-1]+s[0][j+1]).count("#"))
            else:
                ans.append(s[0][j])
        elif j == 0:
            if s[0][j] == ".":
                ans.append((s[0][j+1]).count("#"))
            else:
                ans.append(s[0][j])
        else:
            if s[0][j] == ".":
                ans.append((s[0][j-1]).count("#"))
            else:
                ans.append(s[0][j])
    for j in range(w):
        print(ans[j],end = "")
else:
    ans = []
    if s[0][0] == ".":
        ans.append(0)
    else:
        ans.append(s[0][0])
    print(ans[0])