Buggy python submission (verdict: Runtime Error):
#6
h,w = map(int,input().split())
s = []
ans = [[] for i in range(h)]
for i in range(h):
    s.append(input())
    
cnt = 0
for i in range(h):
    for j in range(w):
        if s[i][j] !="#":
            if i == 0 :              #上底のコーナーケース
                if j == 0 :
                    for m in range(2):
                        for n in range(2):
                            if s[m][n] == "#":
                                cnt += 1
                elif j == w-1:
                    for m in range(2):
                        for n in range(w-2,w):
                            if s[m][n] == "#":
                                cnt += 1
                else:
                    for m in range(2):
                        for n in range(j-1,j+2):
                            if s[m][n] == "#":
                                cnt += 1
            elif i == h-1 :              #下底のコーナーケース
                if j == 0 :
                    for m in range(h-2,h):
                        for n in range(2):
                            if s[m][n] == "#":
                                cnt += 1
                elif j == w-1:
                    for m in range(h-2,h):
                        for n in range(w-2,w):
                            if s[m][n] == "#":
                                cnt += 1
                else:
                    for m in range(h-2,h):
                        for n in range(j-1,j+2):
                            if s[m][n] == "#":
                                cnt += 1
            else:
                if j == 0:             #上底下底以外の左端列
                    for m in range(i-1,i+2):
                        for n in range(2):
                            if s[m][n] == "#":
                                cnt += 1
                elif j == w-1:           #上底下底以外の右端列
                    for m in range(i-1,i+2):
                        for n in range(w-2,w):
                            if s[m][n] == "#":
                                cnt += 1
                else:                  #その他中央部
                    for m in range(i-1,i+2):
                        for n in range(j-1,j+2):
                            if s[m][n] == "#":
                                cnt += 1
            ans[i].append(str(cnt))
            cnt = 0
        else:
            ans[i].append("#")
        
for i in ans:
    print("".join(i))

Fixed submission:
#6re
h,w = map(int,input().split())
s = []
s.append(list("." for i in range(w+2)))
for i in range(h):
    s.append(list("."+input()+"."))
s.append(list("." for i in range(w+2)))
ans = [[0 for j in range(w) ] for i in range(h) ]
    
for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j] != "#":
            for m in range(i-1,i+2):
                for n in range(j-1,j+2):
                    if s[m][n] == "#":
                        ans[i-1][j-1] += 1
        else:
            ans[i-1][j-1] = "#"
                
for i in range(h):
    for j in range(w):
        ans[i][j] = str(ans[i][j])
        
for i in range(h):
    print("".join(ans[i]))

Buggy python submission (verdict: Runtime Error):
#6
h,w = map(int,input().split())
s = []
ans = [[] for i in range(h)]
for i in range(h):
    s.append(input())
    
cnt = 0
for i in range(h):
    for j in range(w):
        if s[i][j] !="#":
            if i == 0 :              #上底のコーナーケース
                if j == 0 :
                    for m in range(2):
                        for n in range(2):
                            if s[m][n] == "#":
                                cnt += 1
                elif j == w-1:
                    for m in range(2):
                        for n in range(w-2,w):
                            if s[m][n] == "#":
                                cnt += 1
                else:
                    for m in range(2):
                        for n in range(j-1,j+2):
                            if s[m][n] == "#":
                                cnt += 1
            elif i == h-1 :              #下底のコーナーケース
                if j == 0 :
                    for m in range(h-2,h):
                        for n in range(2):
                            if s[m][n] == "#":
                                cnt += 1
                elif j == w-1:
                    for m in range(h-2,h):
                        for n in range(w-2,w):
                            if s[m][n] == "#":
                                cnt += 1
                else:
                    for m in range(h-2,h):
                        for n in range(j-1,j+2):
                            if s[m][n] == "#":
                                cnt += 1
            else:
                if j == 0:             #上底下底以外の左端列
                    for m in range(i-1,i+2):
                        for n in range(2):
                            if s[m][n] == "#":
                                cnt += 1
                elif j == w-1:           #上底下底以外の右端列
                    for m in range(i-1,i+2):
                        for n in range(w-2,w):
                            if s[m][n] == "#":
                                cnt += 1
                else:                  #その他中央部
                    for m in range(i-1,i+2):
                        for n in range(j-1,j+2):
                            if s[m][n] == "#":
                                cnt += 1
            ans[i].append(str(cnt))
            cnt = 0
        else:
            ans[i].append("#")
        
for i in ans:
    print("".join(i))

Fixed submission:
#6re
h,w = map(int,input().split())
s = []
s.append(list("." for i in range(w+2)))
for i in range(h):
    s.append(list("."+input()+"."))
s.append(list("." for i in range(w+2)))
ans = [[0 for j in range(w) ] for i in range(h) ]
    
for i in range(1,h+1):
    for j in range(1,w+1):
        if s[i][j] != "#":
            for m in range(i-1,i+2):
                for n in range(j-1,j+2):
                    if s[m][n] == "#":
                        ans[i-1][j-1] += 1
        else:
            ans[i-1][j-1] = "#"
                
for i in range(h):
    for j in range(w):
        ans[i][j] = str(ans[i][j])
        
for i in range(h):
    print("".join(ans[i]))