看了其他人的思路,把矩阵向外扩展即可访问任意8个方向

n,m = map(int,input().split())
mat = ['-'*(m+2)]
for i in range(n):
    mat.append('-' + input() + '-')
add = ['-'*m]
mat.append('-'*(m+2))
new = []
for i in range(n+2):
    t= []
    for j in range(m+2):
        t.append(mat[i][j])
    new.append(t)
for i in range(1,n+1):
    for j in range(1,m+1):
        if new[i][j] != '*':
            count = 0
            if new[i-1][j-1] == '*':
                count += 1
            if new[i-1][j] == '*':
                count += 1
            if new[i-1][j+1] == '*':
                count += 1
            if new[i][j-1] == '*':
                count += 1
            if new[i][j+1] == '*':
                count += 1
            if new[i+1][j-1] == '*':
                count += 1
            if new[i+1][j] == '*':
                count += 1
            if new[i+1][j+1] == '*':
                count += 1
            new[i][j] = count
for i in range(1,n+1):
    for j in range(1,m+1):
        print(new[i][j],end=(''))
    print()