You are given an H × W grid.

The squares in the grid are described by H strings, S_1,...,S_H.

The j-th character in the string S_i corresponds to the square at the i-th row from the top and j-th column from the left (1 \leq i \leq H,1 \leq j \leq W).
. stands for an empty square, and # stands for a square containing a bomb.  
Dolphin is interested in how many bomb squares are horizontally, vertically or diagonally adjacent to each empty square.

(Below, we will simply say "adjacent" for this meaning. For each square, there are at most eight adjacent squares.)

He decides to replace each . in our H strings with a digit that represents the number of bomb squares adjacent to the corresponding empty square.  
Print the strings after the process.  

-----Constraints-----
 - 1 \leq H,W \leq 50
 - S_i is a string of length W consisting of # and ..

-----Input-----
Input is given from Standard Input in the following format:  
H W
S_1
:
S_H

-----Output-----
Print the H strings after the process.

The i-th line should contain a string T_i of length W, where the j-th character in T_i corresponds to the square at the i-th row from the top and j-th row from the left in the grid (1 \leq i \leq H, 1 \leq j \leq W).  

-----Sample Input-----
3 5
.....
.#.#.
.....

-----Sample Output-----
11211
1#2#1
11211

For example, let us observe the empty square at the first row from the top and first column from the left.

There is one bomb square adjacent to this empty square: the square at the second row and second column.

Thus, the . corresponding to this empty square is replaced with 1.

Difficulty: introductory

Starter code:

Solutions:
# 愚直
h, w = map(int, input().split())
table = [list(input()) for _ in range(h)]
for i in range(h):
    for j in range(w):
        if table[i][j] == ".":
            num = 0
            for y in [-1, 0, 1]:
                for x in [-1, 0, 1]:
                    if 0 <= i + y < h and 0 <= j + x < w:
                        if table[i + y][j + x] == "#":
                            num += 1
            table[i][j] = str(num)
for t in table:
    print("".join(t))

h,w=map(int,input().split())
l = [input() for _ in range(h)]
k = [['#' for x in range(w) ] for _ in range(h)]
dx = [0,0,1,-1,1,1,-1,-1]
dy = [1,-1,0,0,1,-1,1,-1]
for i in range(h):
    for j in range(w):
        if l[i][j] == '.':
            cnt = 0
            for di in range(8):
                x = j+dx[di]
                y = i+dy[di]
                if x<0 or y<0 or x >=w or y >=h:
                    continue
                if l[y][x] == '#':
                    cnt +=1
            k[i][j] = cnt
for i in range(h):
    print(''.join(map(str, k[i])))

import sys
H, W = map(int, input().split())
if H == 1:
    if W == 1:
        s = input()
        if s == "#":
            print("#")
        else:
            print(0)
    else:
        cnt = [0 for _ in range(W)]
        s = list(input())
        for w in range(W):
            if w == 0:
                cnt[w] += s[w+1] == "#"
            elif w == W-1:
                cnt[w] += s[w-1] == "#"
            else:
                cnt[w] += s[w+1] == "#"
                cnt[w] += s[w-1] == "#"
        for w in range(W):
            if s[w] == "#":
                cnt[w] = "#"
        ans = map(str, cnt)
        print("".join(ans))
    return
if W == 1:
    cnt = [0 for _ in range(H)]
    s = []
    for h in range(H):
        s.append(input())
    for h in range(H):
        if h == 0:
            cnt[h] += s[h+1] == "#"
        elif h == H-1:
            cnt[h] += s[h-1] == "#"
        else:
            cnt[h] += s[h-1] == "#"
            cnt[h] += s[h+1] == "#"
    for h in range(H):
        if s[h] == "#":
            print("#")
        else:
            print(cnt[h])
    return
    
S = []
cnt = [[0 for _ in range(W)] for _ in range(H)]
for h in range(H):
    s = list(input())
    for w in range(W):
        if w == 0:
            cnt[h][w] += s[w+1] == "#"
        elif w == W-1:
            cnt[h][w] += s[w-1] == "#"
        else:
            cnt[h][w] += s[w+1] == "#"
            cnt[h][w] += s[w-1] == "#"
    S.append(s)
    
for h in range(H):
    for w in range(W):
        if h == 0:
            cnt[h][w] += S[h+1][w] == "#"
            if w == 0:
                cnt[h][w] += S[h+1][w+1] == "#"
            elif w == W-1:
                cnt[h][w] += S[h+1][w-1] == "#"
            else:
                cnt[h][w] += S[h+1][w-1] == "#"
                cnt[h][w] += S[h+1][w+1] == "#"
        elif h == H-1:
            cnt[h][w] += S[h-1][w] == "#"
            if w == 0:
                cnt[h][w] += S[h-1][w+1] == "#"
            elif w == W-1:
                cnt[h][w] += S[h-1][w-1] == "#"
            else:
                cnt[h][w] += S[h-1][w-1] == "#"
                cnt[h][w] += S[h-1][w+1] == "#"
        else:
            cnt[h][w] += S[h+1][w] == "#"
            if w == 0:
                cnt[h][w] += S[h+1][w+1] == "#"
            elif w == W-1:
                cnt[h][w] += S[h+1][w-1] == "#"
            else:
                cnt[h][w] += S[h+1][w-1] == "#"
                cnt[h][w] += S[h+1][w+1] == "#"
            cnt[h][w] += S[h-1][w] == "#"
            if w == 0:
                cnt[h][w] += S[h-1][w+1] == "#"
            elif w == W-1:
                cnt[h][w] += S[h-1][w-1] == "#"
            else:
                cnt[h][w] += S[h-1][w-1] == "#"
                cnt[h][w] += S[h-1][w+1] == "#"
                
for h in range(H):
    s = S[h]
    for w in range(W):
        if s[w] == "#":
            cnt[h][w] = "#"
    ans = map(str, cnt[h])
    print("".join(ans))

H,W = map(int,input().split())

S = [input() for _ in range(H)]
M = [[""]*W for _ in range(H)]

def bomb_cnt(i,j):
  min_h = max(0,i-1)
  max_h = min(i+2,H)
  min_w = max(0,j-1)
  max_w = min(j+2,W)
  cnt = 0
  for k in range(min_h,max_h):
    for g in range(min_w,max_w):
      if (S[k][g] == "#"):
        cnt += 1
        
  return str(cnt)

for i in range(H):
  for j in range(W):
    if S[i][j] == "#":
      M[i][j] = "#"
    else:
      M[i][j] = bomb_cnt(i,j)
      
      
for i in range(H):
  print(*M[i],sep="")

h, w = list(map(int, input().split()))
s = [list(map(str, input())) for i in range(h)]
ans = s

di = [1, 0, -1, 0, 1, -1, -1, 1]
dj = [0, 1, 0, -1, 1, 1, -1, -1]

for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            continue
        cnt = 0
        for d in range(8):
            if i + di[d] <0 or i + di[d] > h - 1 or j + dj[d] < 0 or j + dj[d] > w - 1:
                continue
            if s[i + di[d]][j +dj[d]] == '#':
                cnt += 1
        ans[i][j] = cnt

for i in range(h):
    print((''.join(map(str, ans[i]))))


H,W=map(int,input().split())
S=[]
for i in range(H):
  S.append(list(input()))
for j in range(H):
  for p in range(W):
    if S[j][p]==".":
      c=0
      for h,w in [(1,0),(0,1),(-1,0),(0,-1),(1,1),(-1,-1),(-1,1),(1,-1)]:
        if 0<=j+h<=H-1 and 0<=p+w<=W-1 and S[j+h][p+w]=="#":
          c+=1
        S[j][p]=c
for i in S:
  print(*i,sep="")

def resolve():
    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 x in range(h):
        for y in range(w):
            if s[x][y]=='#':
                for k in range(8):
                    nx=x+dx[k]
                    ny=y+dy[k]
                    if not(nx<h and 0<=nx and ny<w and 0<=ny) or s[nx][ny]=='#':
                        continue
                    if s[nx][ny]=='.':
                        s[nx][ny]='1'
                    else:
                        s[nx][ny]=str(ord(s[nx][ny])-ord('0')+1)
    for i in range(h):
        for j in range(w):
            if s[i][j]=='.':
                s[i][j]='0'
    for i in range(h):
        print(''.join(s[i]))
resolve()

H,W=map(int,input().split())
S=[list(input()) for _ in range(H)]

di=[1,1,0,-1,-1,-1,0,1]
dj=[0,1,1,1,0,-1,-1,-1]

for i in range(H):
    for j in range(W):
        ans=0
        if S[i][j]=='#':
            continue
        for k in range(8):
            ni=i+di[k]
            nj=j+dj[k]
            if ni<0 or nj<0 or ni>=H or nj>=W:
                continue
            if S[ni][nj]=='#':
                ans+=1
        S[i][j]=str(ans)
for i in range(H):
    for j in range(W):
        print(S[i][j],end='')
    print()


import numpy as np
H, W = map(int, input().split())
S = []
pad = list('.'* (W+2))
S.append(pad)
for i in range(H):
    _S = list(input())
    _S = ['.'] + _S +[ '.']
    S.append(_S)
S.append(pad)
S = np.array(S)

for h in range(1, H+1):
    for w in range(1, W+1):
        if S[h, w] == '#':
            print('#', end='')
        else:
            print(np.sum(S[h-1:h+2, w-1:w+2]=='#'), end="")
    print()

        

h,w = map(int,input().split())
l = [["."]*(w+2)]
for i in range(h):
    l.append(["."] + [i for i in input()] + ["."])
l.append(["."]*(w+2))
 
def count(i,j):
    if l[i][j] == "#":
        return "#"
    c = 0
    for n in range(i-1,i+2):
        for m in range(j-1,j+2):
            if l[n][m] == "#":
                c += 1
    return str(c)
 
ans = ["".join([count(i,j) for j in range(1,1+w)]) for i in range(1,1+h)]
 
print("\n".join(ans))

h, w = map(int, input().split())
s = []
t = []
for i in range(h):
    s.append(input())

for i in range(h):
    t.append([])
    for j in range(w):
        mines = 0
        if s[i][j] == '.':
            if i > 0 and j > 0:
                if s[i - 1][j - 1] == '#':
                    mines += 1
            if i < h - 1 and j > 0:
                if s[i + 1][j - 1] == '#':
                    mines += 1
            if i > 0 and j < w - 1:
                if s[i - 1][j + 1] == '#':
                    mines += 1
            if i < h - 1 and j < w - 1:
                if s[i + 1][j + 1] == '#':
                    mines += 1
            if i > 0:
                if s[i - 1][j] == '#':
                    mines += 1
            if i < h - 1:
                if s[i + 1][j] == '#':
                    mines += 1
            if j > 0:
                if s[i][j - 1] == '#':
                    mines += 1
            if j < w - 1:
                if s[i][j + 1] == '#':
                    mines += 1
            t[i].append(str(mines))
        else:
            t[i].append('#')

for i in range(len(t)):
    print(''.join(t[i]))

H,W=map(int,input().split())
S=[input() for _ in range(H)]
ans=[[0]*W for _ in range(H)]
x=(-1,0,1,-1,0,1,-1,0,1)
y=(1,1,1,0,0,0,-1,-1,-1)
for i in range(H):
  for j in range(W):
    if S[i][j]==".":
      for k in range(9):
        a=i+y[k]
        b=j+x[k]
        if 0<=a<H and 0<=b<W:
          if S[a][b]=="#":
            ans[i][j]+=1
for i in range(H):
  for j in range(W):
    if S[i][j]=="#":
      print("#",end="")
    else:
      print(ans[i][j],end="")
  print()


from itertools import product as pro
h, w = list(map(int, input().split()))
s = [list(input()) for i in range(h)]
ans = [["#"] * len(s[0]) for i in range(len(s))]
visited = []

def dfs(p):
    if p in visited or s[p[0]][p[1]] == "#":
        return None

    temp = [1 if s[y+p[0]][x+p[1]] == '#' else 0 for (y, x) in pro([-1, 0, 1], [-1, 0, 1]) if 0 <= x+p[1] < len(s[0]) and 0 <= y+p[0] < len(s)]
    s[p[0]][p[1]] = sum(temp)


for i in range(len(s)):
    for j in range(len(s[0])):
        dfs([i, j])


for i in s:
    print((''.join(map(str, i))))


# -*- coding: utf-8 -*-

def get_count(S, H, W, i, j):
    num = 0
    start_i = start_j = -1
    end_i = end_j = 2
    if W == 1:
        start_i = 0 
        end_i = 1
    else:
        if i == 0:
            start_i = 0
        elif i == W-1:
            end_i = 1
    if H == 1:
        start_j = 0
        end_j = 1
    else:
        if j == 0:
            start_j = 0
        elif j == H-1:
            end_j = 1

    for y in range(start_j, end_j, 1):
        for x in range(start_i, end_i, 1):
            if S[j+y][i+x] == "#":
                num += 1

    return num


H,W = list(map(int, input().split()))
S = []
for i in range(H):
    S.append(list(input()))

for y in range(H):
    for x in range(W):
        if S[y][x] == ".":
            num = get_count(S, H, W, x, y)
            S[y][x] = str(num)

for y in range(H):
    print(("".join(S[y])))            


from itertools import product
h, w = map(int, input().split())
G = [list(input()) for _ in range(h)]
for y, x in product(range(h), range(w)):
    if G[y][x]=='.':
        G[y][x]=0
        for dy, dx in product(range(-1, 2), repeat=2):
            ny = y+dy
            nx = x+dx
            if 0 <= ny < h and 0 <= nx < w:
                G[y][x] += G[ny][nx]=='#'
for g in G:
    print(*g, sep='')


h,w = list(map(int,input().split()))
m = [input() for _ in range(h)]
vx=[0,1,1,1,0,-1,-1,-1]
vy=[1,1,0,-1,-1,-1,0,1]
ans=[['#']*w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if m[i][j]=='#':
            ans[i][j]='#'
            continue
        count=0
        for k in range(8):
            ny=vy[k]+i
            nx=vx[k]+j
            if 0<=ny and ny<h and 0<=nx and nx<w:
                if m[ny][nx]=='#':
                    count+=1
        ans[i][j]=str(count)
for i in ans:
    print((''.join(i)))


h,w = map(int, input().split())
s = []
for _ in range(h):
	s.append(list(input()))
    
if (h == 1)&(w==1):
    if s[0][0] == ".":
        s[0][0] = "0"

elif (h == 1)&(w!=1):
    for j in range(w):
        if j == 0:
            s[0][j] = s[0][j].replace(".",str(s[0][j+1].count("#")))
        elif j == w-1:
            s[0][j] = s[0][j].replace(".",str(s[0][j-1].count("#")))
        else:
            s[0][j] = s[0][j].replace(".",str([s[0][j-1],s[0][j+1]].count("#")))

elif (h != 1)&(w==1):
    for i in range(h):
        if i == 0:
            s[i][0] = s[i][0].replace(".",str(s[i+1][0].count("#")))
        elif i == h-1:
            s[i][0] = s[i][0].replace(".",str(s[i-1][0].count("#")))
        else:
            s[i][0] = s[i][0].replace(".",str([s[i-1][0],s[i+1][0]].count("#")))
else:
    for i in range(len(s)):
        if i == 0:
            for j in range(w):
                if j == 0:
                    s[i][j] = s[i][j].replace(".",str([s[i][j+1],s[i+1][j],s[i+1][j+1]].count("#")))
                elif j == w-1:
                    s[i][j] = s[i][j].replace(".",str([s[i][j-1],s[i+1][j],s[i+1][j-1]].count("#")))
                else:
                    s[i][j] = s[i][j].replace(".",str([s[i][j-1],s[i][j+1],s[i+1][j],s[i+1][j+1],s[i+1][j-1]].count("#")))
        elif i == h-1:
            for j in range(w):
                if j == 0:
                    s[i][j] = s[i][j].replace(".",str([s[i][j+1],s[i-1][j],s[i-1][j+1]].count("#")))
                elif j == w-1:
                    s[i][j] = s[i][j].replace(".",str([s[i][j-1],s[i-1][j],s[i-1][j-1]].count("#")))
                else:
                    s[i][j] = s[i][j].replace(".",str([s[i][j-1],s[i][j+1],s[i-1][j],s[i-1][j+1],s[i-1][j-1]].count("#")))
        else:
            for j in range(w):
                if j == 0:
                    s[i][j] = s[i][j].replace(".",str([s[i][j+1],s[i-1][j],s[i-1][j+1],s[i+1][j],s[i+1][j+1]].count("#")))
                elif j == w-1:
                    s[i][j] = s[i][j].replace(".",str([s[i][j-1],s[i-1][j],s[i-1][j-1],s[i+1][j],s[i+1][j-1]].count("#")))
                else:
                    s[i][j] = s[i][j].replace(".",str([s[i][j-1],s[i][j+1],s[i-1][j],s[i-1][j+1],s[i-1][j-1],s[i+1][j],s[i+1][j+1],s[i+1][j-1]].count("#")))

for k in s:
    print("".join(k))

import numpy as np
H, W = map(int, input().split())
mat = []
for h in range(H):
    row = []
    A = list(input())
    for a in A:
        if a == '.':
            row.append(0)
        elif a == '#':
            row.append('#')
    mat.append(row)

for i in range(H):
    for j in range(W):
        if mat[i][j] != '#':
            for h, w in zip([0,1,1,1,0,-1,-1,-1],[1,1,0,-1,-1,-1,0,1]):
                if i+h >= 0 and i+h < H and j+w >= 0 and j+w < W:
                    if mat[i+h][j+w] == '#':
                        mat[i][j] += 1
for i in mat:
    print(*i, sep='')


import copy

h, w = map(int, input().split())
s = [input() for _ in range(h)]

a = []
for i in range(-1, 2):
    for j in range(-1, 2):
        a.append((i, j))

ans = [[0]*w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            ans[i][j] = '#'

for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            cnt = 0
            for y, x in a:
                iy, jx = i+y, j+x
                if 0 <= iy < h and 0 <= jx < w:
                    if s[iy][jx] == '#':
                        cnt += 1
            ans[i][j] = cnt
for i in ans:
    print(''.join(map(str, i)))

h, w = map(int, input().split())
s = []
for i in range(h):
    s.append(input())

p = [[0] * (w + 2) for i in range(h + 2)]

for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            p[i][j] += 1
            p[i][j+1] += 1
            p[i][j+2] += 1
            p[i+1][j] += 1
            p[i+1][j+2] += 1
            p[i+2][j] += 1
            p[i+2][j+1] += 1
            p[i+2][j+2] += 1

for m in range(1, h+1):
    for n in range(1, w+1):
        i = m - 1
        j = n - 1
        if s[i][j] == '#':
            p[m][n] ='#'

for m in range(1, h+1):
    print(''.join(map(str, p[m][1:w+1])))

h,w = [int(x) for x in input().split()]
m = [["."] * (w + 2)]
for i in range(h):
  m.append(["."] + list(input()) + ["."])
m.append(m[0])
#print(m,sep = "\n")

def check(x,y):
  res = 0
  for i in range(3):
    for j in range(3):
      if m[x-1+i][y-1+j] == "#":
        res += 1
  return str(res)

for i in range(1,h+1):
  for j in range(1,w+1):
    if m[i][j] == ".":
      m[i][j] = check(i,j)
ans = []
for i in range(h):
  ans.append(m[i+1][1:w+1])
for i in range(h):
  print("".join(ans[i]))

H,W = map(int,input().split())
s = []
for i in range(H):
    a = list(input())
    s.append(a)
for i in range(H):
    for j in range(W):
        count = 0
        if s[i][j] == ".":
            for k in range(i-1,i+2):
                for l in range(j-1,j+2):
                    if (0 <= k <= H-1) and (0 <= l <= W-1) and s[k][l] == "#":
                        count += 1
            s[i][j] = str(count)
for i in range(H):
    print("".join(s[i]))

H, W = [int(i) for i in input().split()]
SS = [input() for _ in range(H)]

def s(y, x):
  if x >= W or x < 0 or y >= H or y < 0:
    return 0

  return 1 if SS[y][x] == '#' else 0

for i in range(H):
  S = SS[i]
  l = []
  for j in range(W):
    c = S[j]
    if c == '.':
      l.append(str(sum([s(i-1, j-1), s(i-1, j), s(i-1, j+1), s(i, j-1), s(i, j+1), s(i+1, j-1), s(i+1, j), s(i+1, j+1)])))
    else:
      l.append('#')
  print((''.join(l)))


def period_to_zero(value:str):
    if value == ".":
        return 0
    return value

H,W = map(int, input().split())
S = list()
S.append([0]*(W+2))
for i in range(H):
    s = list()
    s.append(0)
    s.extend(list(map(period_to_zero, list(input()))))
    s.append(0)
    S.append(s)
S.append([0]*(W+2))

for i in range(1, len(S)-1):
    s_prev = S[i-1]
    s_current = S[i]
    s_next = S[i+1]
    for j in range(1, len(s_current)-1):
        if s_current[j] != "#":
            s_current[j] = (s_prev[j-1:j+2] + s_next[j-1:j+2] + [s_current[j-1], s_current[j+1]]).count("#")
        
for i in range(1, len(S)-1):
    s = S[i]
    print(*s[1:len(s)-1],sep="")

h, w = map(int, input().split())

s = [['.']*(w+2)]+ [list('.'+ input() + '.') for i in range(h)] + [['.']*(w+2)]
a = [[0]*w for i in range(h)]


for i in range(1, h+1):
  for j in range(1,w+1):
    if s[i][j] == '#':
      a[i-1][j-1] = '#'
    else:
      b = [s[i-1][j-1],s[i-1][j], s[i-1][j+1], s[i][j-1], s[i][j+1], s[i+1][j-1], s[i+1][j], s[i+1][j+1]]
      c = b.count('#')
           
      a[i-1][j-1] = c

for i in range(h):
  a[i] = [str(x) for x in a[i]]
  print(''.join(a[i]))

h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == ".":
            c = 0
            for x in range(-1, 2):
                for y in range(-1, 2):
                    if x == 0 and y == 0:
                        continue
                    else:
                        if 0 <= i+y and 0 <= j+x and i+y < h and j+x < w:
                            if s[i+y][j+x] == "#":
                                c += 1
            s[i][j] = str(c)
for a in range(h):
    print("".join(s[a]))

h,w=map(int,input().split())
a=[input() for i in range(h)]
b=[]
c=0
for i in range(h):
    for j in range(w):
        if a[i][j]=='#':
                b.append("#")
        else:
            for k in a[max(0,i-1):min(h,i+2)]:
                c+=k[max(0,j-1):min(w,j+2)].count("#")
            b.append(str(c))
            c=0
            
d="".join(b)
for i in range(h):
    print(d[i*w:(i+1)*w])

h,w=map(int,input().split())
s=["."*(w+2)]
for i in range(h):
    s.append("."+input()+".")
s.append("."*(w+2))
dx=[-1,0,1,1,1,0,-1,-1]
dy=[1,1,1,0,-1,-1,-1,0]
ans=[]
for i in range(1,h+1):
    wp=""
    for j in range(1,w+1):
        if s[i][j]=="#":
            wp+="#"
            continue
        count=0
        for k in range(8):
            if s[i+dy[k]][j+dx[k]]=="#":
                count+=1
        wp+=str(count)
    ans.append(wp)
print(*ans,sep="\n")

h,w = map(int, input().split())
l = [[] for _ in range(h+2)]
l[0] = list("."*(w+2))
l[h+1] = list("."*(w+2))
for i in range(1,h+1):
  l[i] = list(("." + input() + "."))
for j in range(1,h+1):
  for k in range(1,w+1):
    c = []
    if l[j][k] == ".":
      c += [l[j-1][k-1], l[j-1][k], l[j-1][k+1], l[j][k-1], l[j][k+1], l[j+1][k-1], l[j+1][k], l[j+1][k+1]]
      l[j][k] = str(c.count("#"))
for i in range(1,h+1):
  ln = l[i][1:w+1]
  s = "".join(ln)
  print(s)

H, W = map(int, input().split())

S = [["" for w in range(W)] for h in range(H)]

for h in range(H):
    S[h] = list(input())

for h in range(H):
    for w in range(W):
        if S[h][w] == "#":
            continue
        ymin = max(0, h - 1)
        ymax = min(H, h + 2)
        xmin = max(0, w - 1)
        xmax = min(W, w + 2)

        S[h][w] = 0
        for y in range(ymin, ymax):
            S[h][w] += S[y][xmin:xmax].count("#")

for h in range(H):
    for w in range(W):
        print(S[h][w], end="")
    print("")

import sys, itertools
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**20
def I(): return int(input())
def F(): return float(input())
def S(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()

def resolve():
    H, W = LI()
    s = [list(S()) for _ in range(H)]

    for i, j in itertools.product(list(range(H)), list(range(W))):
        if s[i][j] != '#':
            cnt = 0
            for k, l in itertools.product(list(range(-1, 2)), repeat=2):
                if 0 <= i + k < H and 0 <= j + l < W:
                    if s[i+k][j+l] == '#':
                        cnt += 1
            s[i][j] = str(cnt)

    for i in s:
        print((''.join(i)))

def __starting_point():
    resolve()

__starting_point()

H, W = map(int, input().split())
S = [input() for i in range(H)]

for h in range(H):
    new_s = ''
    for w in range(W):
        if S[h][w] == '#': 
            new_s += '#'
            continue
        count = 0
        ary = [-1, 0, 1]
        for i in ary:
            for j in ary:
                if h+i < 0 or h+i >= H or w+j < 0 or w+j >=W: continue
                if S[h+i][w+j] == '#':
                    count += 1
        new_s += str(count)
    
    S[h] = new_s

for s in S:
    print(s)

h,w = map(int, input().split())
sl = []
bl = [["" for _ in range(w)] for _ in range(h)]

for i in range(h):
    s = input()
    sl.append(s)

def scheck(s):
    if s == "#":
        return 1
    else:
        return 0
        
def check(i,j,sl):
    cnt = 0
    for k in range(-1,2):
        for l in range(-1,2):
            if h > i+k >= 0 and w > j+l >= 0:
                cnt += scheck(sl[i+k][j+l])
    return str(cnt)

for i in range(h):
    for j in range(w):
        if sl[i][j] == ".":
            bl[i][j] = check(i,j,sl)
        else:
            bl[i][j] = "#"

for b in bl:
    t = ''.join(b)
    print(t)

# coding:utf-8
h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
ans = [[0] * w for _ in range(h)]
x = [-1, 0, 1, -1, 1, -1, 0, 1]
y = [-1, -1, -1, 0, 0, 1, 1, 1]

# print(s)


for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            ans[i][j] = '#'
        else:
            for k in range(len(x)):
                if 0 <= (i + x[k]) < h and 0 <= (j + y[k]) < w and s[i + x[k]][j + y[k]] == '#':
                    ans[i][j] += 1

for i in range(h):
    print(*ans[i], sep='')


h,w=map(int,input().split())
l=[]
for i in range(h):
  s=list(input().replace(".","0"))
  l.append(s)
for i in range(h):
  for j in range(w):
    if l[i][j]=="#":
      for x in range(i-1,i+2):
        for y in range(j-1,j+2):
          if 0<=x<=h-1 and 0<=y<=w-1:
            if l[x][y]!="#":
              l[x][y] = str(int(l[x][y]) + 1)
for i in range(h):
  print("".join(l[i]))

H, W = list(map(int, input().split()))
masu = [["." for _ in range(W+2)] for _ in range(H+2)]

for i in range(H):
  masu[i+1][1:W+1] = input()
  
result = [[0 for _ in range(W)] for _ in range(H)]
for i in range(1, H+1):
  for j in range(1, W+1):
    if masu[i][j] == '#':
      result[i-1][j-1] = '#'
    else:
      x = masu[i-1][j-1:j+2]+masu[i][j-1:j+2]+masu[i+1][j-1:j+2]
      cnt = x.count('#')
      result[i-1][j-1] = cnt
for s in result:
  s = list(map(str, s))
  print(("".join(s)))


H,W=map(int,input().split())
f = []
for _ in range(H):
  f.append(list(input()))

dx = [ 0,  1, 1, 1, 0,-1, -1, -1]
dy = [-1, -1, 0, 1, 1, 1,  0, -1]
for h in range(H):
  for w in range(W):
    if f[h][w] == '#':
      continue
    cnt = 0
    for i in range(8):
      nw = w + dx[i]
      nh = h + dy[i]
      if nw < 0 or W <= nw or nh < 0 or H <= nh:
        continue
      if f[nh][nw] == '#':
        cnt += 1
    f[h][w] = str(cnt)

for row in f:
  print(''.join(row))

h, w = list(map(int, input().split()))
s = []
for i in range(h):
  s.append(list(input()))
  
dx = [1, 0, -1, 0, 1, -1, 1, -1]
dy = [0, 1, 0, -1, 1, 1, -1, -1]

for i in range(h):
  for j in range(w):
    if s[i][j] == "#":
      continue
    
    tmp = 0
    for k in range(8):
      ni = i + dx[k]
      nj = j + dy[k]
      
      if ni < 0 or ni >= h:
        continue
      if nj < 0 or nj >= w:
        continue
      if s[ni][nj] == "#":
        tmp += 1
    s[i][j] = str(tmp)

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


H, W = map(int, input().split())
S = [[a for a in input()] for _ in range(H)]
d = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
for i in range(H):
    for j in range(W):
        if S[i][j] == "#": continue
        c = 0
        for ii, jj in d:
            ni = i + ii
            nj = j + jj
            if 0 <= ni < H and 0 <= nj < W and S[ni][nj] == "#":
                c += 1
        S[i][j] = str(c)
for s in S:
    print("".join(s))

h,w=map(int,input().split())
l=[list(input()) for i in range(h)]

ans=[[0]*w for i in range(h)]

for i in range(h):
  for j in range(w):
    if l[i][j]=='#':
      ans[i][j]='#'
    else:
      for p in range(-1,2):
        for q in range(-1,2):
          if 0<=i+p<h and 0<=j+q<w and l[i+p][j+q]=='#':
            ans[i][j]+=1
            
for i in range(h):
  print(*ans[i],sep='')


from collections import Counter
import math
import statistics
import itertools
a,b=map(int,input().split())
# b=input()
# c=[]
# for i in a:
#     c.append(int(i))
# A,B,C= map(int,input().split())
# f = list(map(int,input().split()))
g = [list(map(lambda x: '{}'.format(x), list(input()))) for _ in range(a)]
# h = []
# for i in range(a):
#     h.append(list(map(int,input().split())))
# a = [[0] for _ in range(H)]#nizigen

for i in range(a):
    for k in range(b):
        count=0
        if g[i][k]==".":
            if k-1>=0:
                if g[i][k-1]=="#":
                    count+=1
            if i-1>=0:
                if g[i-1][k]=="#":
                    count+=1
            if i-1>=0 and k-1>=0:
                if g[i-1][k-1]=="#":
                    count+=1
            if k+1<=b-1:
                if g[i][k+1]=="#":
                    count+=1
            if i+1<=a-1:
                if g[i+1][k]=="#":
                    count+=1
            if i+1<=a-1 and k+1<=b-1:
                if g[i+1][k+1]=="#":
                    count+=1
            if i-1>=0 and k+1<=b-1:
                if g[i-1][k+1]=="#":
                    count+=1
            if i+1<=a-1 and k-1>=0:
                if g[i+1][k-1]=="#":
                    count+=1
            g[i][k]=str(count)


for i in g:
    print("".join(i))

H,W=list(map(int,input().split()))
l=[list(input()) for i in range(H)]
ans_l=[[0]*W for i in range(H)]
for i in range(H):
   for j in range(W):
      if l[i][j] == ".":
         tmp=0
         for x in range(max(0,j-1),min(W-1,j+1)+1):
            for y in range(max(0,i-1),min(H-1,i+1)+1):
               if l[y][x]=="#":
                  tmp+=1
         ans_l[i][j]=str(tmp)
      else:
         ans_l[i][j]="#"
for i in ans_l:
   print(*i,sep="")

h, w = list(map(int, input().split()))
table = []
for i in range(h):
    line = input()
    table.append(line)

for i in range(h):
    line = ""
    for j in range(w):
        hs = max(0, i - 1)
        he = min(h - 1, i + 1)
        ws = max(0, j - 1)
        we = min(w - 1, j + 1)
        counter = 0
        for hi in range(hs, he + 1):
            for wi in range(ws, we + 1):
                if table[hi][wi] == "#":
                    counter += 1
        if table[i][j] == "#":
            line += "#"
        else:
            line += str(counter)
    print(line)


h, w = (int(x) for x in input().split())
s = []
ans = []
rev = ""
for row in range(1, h+1):
    for col, t in enumerate(input()):
        s.append([(row,col+1),t])

for index, factor in enumerate(s):
    count =0
    if factor[1] != "#":
        if w == 1:
            for point in [index-1, index+1]:
                if 0 <= point and point < len(s) and s[point][1] == "#":
                    count += 1
        elif (index+1)%w == 1:
            for point in [index +1,index -w+1, index -w, index +w, index +w+1]:
                if 0 <= point and point < len(s) and s[point][1] == "#":
                    count += 1
        elif (index+1)%w == 0:
            for point in [index -1,index -w, index -w-1, index +w-1, index +w]:
                if 0 <= point and point < len(s) and s[point][1] == "#":
                    count += 1
        else:
            for point in [index -1, index +1,index -w+1, index -w, index -w-1, index +w-1, index +w, index +w+1]:
                if 0 <= point and point < len(s) and s[point][1] == "#":
                    count += 1
        ans.append(count)
    else:
        ans.append("#")

while(ans != []):
    for num in range(w):
        rev += str(ans[0])
        del ans[0]
    rev += "\n"

print(rev)

h,w = list(map(int, input().split()))
s = list()
t = list()
a = [0 for i in range(w+2)]
s.append(a)
for i in range(h):
    b = str(input())
    c = list(('0'+b+'0').replace('.','0').replace('#','1'))
    d = list(map(int, c))
    s.append(d)
    t.append(b)
s.append(a)

for j in range(1,h+1):
    for k in range(1,w+1):
        if s[j][k] == 0:
            cnt = 0
            cnt = (s[j-1][k-1]+s[j-1][k]+s[j-1][k+1]) + (s[j][k-1]+s[j][k+1]) + (s[j+1][k-1]+s[j+1][k]+s[j+1][k+1])
            t[j-1] = t[j-1].replace('.', str(cnt), 1)
            
        else:
            continue
for i in range(h):
    print((t[i]))


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

# 入力
H, W = map(int, input().split())
S = [input() for _ in range(H)]

# Python3 では str 型の要素の書き換えはできない
# 答えを格納する二次元配列を別途用意する ('.' のところは 0)
result = [[0 if v == '.' else '#' for v in row] for row in S]

# 各マス (i, j) について順に処理
for i in range(H):
    for j in range(W):
        if S[i][j] != '.':
            continue
        for d in range(8):
            ni = i + dx[d]
            nj = j + dy[d]
            if ni < 0 or ni >= H or nj < 0 or nj >= W:
                continue
            if S[ni][nj] == '#':
                result[i][j] += 1

for row in result:
    print(*row, sep = '')

H, W = map(int,input().split())

mas = [list(map(str,input())) for _ in range(H)]
mas2 = [[0 for _ in range(W+2)] for __ in range(H+2)]

for i in range(1,H+1):
  for j in range(1,W+1):
    if mas[i-1][j-1] == '#':
      mas2[i][j] = -9
      mas2[i+1][j] += 1
      mas2[i+1][j-1] += 1
      mas2[i][j-1] += 1
      mas2[i-1][j-1] += 1
      mas2[i-1][j] += 1
      mas2[i-1][j+1] += 1
      mas2[i][j+1] += 1
      mas2[i+1][j+1] += 1

mas3 = [l[1:-1] for l in mas2[1:-1]]

for k in range(H):
  print(*["#" if i < 0 else i for i in mas3[k]],sep="")

H, W = (int(x) for x in input().split())
S = [input() for _ in range(H)]
ans = [''] * H  
for i in range(H):
    for j in range(W):
        if S[i][j] == '#':
            ans[i] += '#'
            continue
        bom = 0
        for k in range(3):
            for l in range(3):
                if 0 <= i-1+k < H and 0 <= j-1+l < W:
                    if S[i-1+k][j-1+l] == '#':
                        bom += 1
        ans[i] += str(bom)
for s in ans:
    print(s)
                    


m, n = map(int,input().split())

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

S = [input() for i in range(m)]

for i in range(m):
  for j in range(n):
    num = 0
    if not S[i][j] == '#' :
      for d in range(8):
        nx = i + dx[d]
        ny = j + dy[d]
        if nx < 0 or nx >= m :
          continue
        if ny < 0 or ny >= n :
          continue
        if S[nx][ny] == '#' :
          num += 1
      A = list(S[i])
      A[j] = str(num)
      S[i] = "".join(A)
    
for i in range(m):
  print(S[i])

H, W = map(int, input().split())
m = [] 

for i in range(H):
  s=input()
  t = []
  for i in s:
    t.append(1 if i == "#" else 0)
  m.append(t)
  
for i in range(H):
  for j in range(W):
    if m[i][j] == 1:
      print("#", end="")
      continue
    count = 0
    if i !=0 :
      count += m[i-1][j]
      if j != 0:
        count += m[i-1][j-1]
      if j != W-1:
        count += m[i-1][j+1]
    if i != H-1:
      count += m[i+1][j]
      if j != 0:
        count += m[i+1][j-1]
      if j != W-1:
        count += m[i+1][j+1]
    if j != 0:
      count += m[i][j-1]
    if j != W-1:
      count += m[i][j+1]
    print(count, end="")
  print()

    
  

X,Y = map(int,input().split())
F = list(list(input()) for _ in range(X))
DX = [-1,-1,-1,0,0,1,1,1]
DY = [-1,0,1,-1,1,-1,0,1]

for i in range(X):
  for j in range(Y):
    if F[i][j] == '.':
      cnt = 0
      for k in range(8):
        x = i + DX[k]
        y = j + DY[k]
        if 0 <= x < X and 0 <= y < Y and F[x][y] == '#':
          cnt += 1
      F[i][j] = str(cnt)
      
for f in F:
  print(''.join(f))

H, W = map(int, input().split())
L = ['.' + input() + '.' for _ in range(H)]
L = ['.' * (W + 2)] + L + ['.' * (W + 2)]
A = []
for i in range(1, H + 1):
  S = ""
  for j in range(1, W + 1):
    if L[i][j] == '.':
      t = [L[i-1][j-1], L[i-1][j], L[i-1][j+1], L[i][j+1], L[i+1][j+1], L[i+1][j], L[i+1][j-1], L[i][j-1]]
      S += str(t.count('#'))
    else:
      S += '#'
  A.append(S)
[print(i) for i in A]

import typing
from typing import List, Counter
from itertools import product
class Grid:
    def __init__(self, field: List[str]):
        self.setField(field)
    
    def setField(self, field) -> None:
        self.column: int = len(field)
        self.row: int = len(field[0])
        self.field: List[List[str]] = [list(line) for line in field]

    def searchBomb(self) -> None:
        for i,j in product(range(self.column), range(self.row)):
            conn = tuple((i-1+k//3, j-1+k%3) for k in range(9))
            conn = tuple((x,y) for x,y in conn if 0<=x<self.column and 0<=y<self.row)
            if self.field[i][j] == '.':
                self.field[i][j] = str(Counter(self.field[x][y] for x,y in conn)['#'])
    
    def print(self) -> None:
        for line in self.field:
            print(''.join(line))

def main():
    with open(0) as f:
        H, W = map(int, f.readline().split())
        S = [line.strip() for line in f.readlines()]
    grid = Grid(S)
    grid.searchBomb()
    grid.print()

def __starting_point():
    main()
__starting_point()

h,w = map(int,input().split())
s = [list(input()) for i in range(h)]
for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            m = 0
            if i-1 >= 0 and j-1 >= 0:
                if s[i-1][j-1] == '#':
                    m += 1
            if i-1 >= 0:
                if s[i-1][j] == '#':
                    m += 1
            if i-1 >= 0 and j+1 < w:
                if s[i-1][j+1] == '#':
                    m += 1
            if j-1 >= 0:
                if s[i][j-1] == '#':
                    m += 1
            if j+1 < w:
                if s[i][j+1] == '#':
                    m += 1
            if i+1 < h and j-1 >= 0:
                if s[i+1][j-1] == '#':
                    m += 1
            if i+1 < h:
                if s[i+1][j] == '#':
                    m += 1
            if i+1 < h and j+1 < w:
                if s[i+1][j+1] == '#':
                    m += 1
            s[i][j] = str(m)
for i in range(h):
    z = list(s[i])
    print(''.join(z))

h,w = map(int,input().split())
grid = [""]*h
for i in range(h):
    grid[i] = list(input())
# print(grid)
search = [(row,col) for row in range(-1,2) for col in range(-1,2) if not(row == 0 and col == 0)]
# print(search)

for i in range(h):
    ans = ""
    for j in range(w):
        if(grid[i][j] == "."):
            bomb = 0
            for k in range(8):
                row,col = search[k]
                if(0<=i+row<h and 0<=j+col<w):
                    if(grid[i+row][j+col] == "#"):
                        bomb += 1
            grid[i][j] = bomb
        ans+=str(grid[i][j])
    print(ans)

l = [int(c) for c in input().split()]
H = l[0]
W = l[1]
S = [input() for c in range(H)]

for i in range(H):
    for j in range(W):
        if S[i][j]==".":
            cnt = 0
            for k in range(3):
                for m in range(3):
                    if 0 <= i-1+k and i-1+k <= H-1 and 0 <= j-1+m and j-1+m <= W-1:
                        if S[i-1+k][j-1+m] == "#":
                            cnt+=1
            S[i]=S[i][:j]+str(cnt)+S[i][j+1:]
    print(S[i])

import numpy as np

h,w = map(int,(input().split()))
s = [list(map(str,input())) for i in range(h)]
l = [[0]*(w+2) for i in range(h+2)]


def addFlag(i,j):
    for x in range(3):
        for y in range(3):
            if l[i+x][j+y] != '#':
                l[i+x][j+y] += 1
                
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            l[i+1][j+1] = '#'
            addFlag(i,j)

l = np.delete(l,[0,h+1],0)
l = np.delete(l,[0,w+1],1)


for i in l:
    ans = ''.join(map(str,i))
    print(ans)

h, w = map(int, input().split())
a = [input() for i in range(h)]
for i in range(h):
  b = ""
  for j in range(w):
    if a[i][j] == "#":
      b += "#"
    else:
      b += str(sum(k[max(0, j-1):min(w, j+2)].count("#") for k in a[max(0, i-1):min(h, i+2)]))
  print(b)

H,W=map(int, input().split())
S=[list(input()) for i in range(H)]

dh = [0,1,-1,0,1,-1,-1,1]
dw = [1,0,0,-1,1,1,-1,-1]

for h in range(H):
    for w in range(W):
        if S[h][w] == "#":
            continue
        else:
            cnt = 0
            for k in range(8):
                next_h = h+dh[k]
                next_w = w+dw[k]
                if not(0<= next_h <= H-1) or not(0<= next_w <= W-1):
                    continue
                elif S[next_h][next_w] == "#":
                    cnt += 1
        S[h][w] = str(cnt)
        
for s in S:
    print("".join(s))

h,w=list(map(int,input().split()))
s=[input() for i in range(h)]
ss=[[0 for _ in range(w+2)] for _ in range(h+2)]
for j in range(w):
    for i in range(h):
        ss[i+1][j+1]=s[i][j]
s=[[0 for _ in range(w)] for _ in range(h)]
for j in range(1,w+1):
    for i in range(1,h+1):
        for a in range(-1,2):
            for b in range(-1,2):
                if a==0 and b ==0:
                    continue
                if ss[i+a][j+b]=='#':
                    s[i-1][j-1]+=1
for j in range(w):
    for i in range(h):
        if ss[i+1][j+1]=='#':
            s[i][j]='#'
for i,d in enumerate(s):
    d=list(map(str,d))
    print((''.join(d)))


def solve():
    H, W = list(map(int, input().split()))
    S = [input() for i in range(H)]

    for i in range(H):
        S[i] = list(S[i])

    for i in range(H):
        for k in range(W):

            if S[i][k]!="#":

                c = 0; #counter

                if i>0 and k>0 and S[i-1][k-1]=="#": # upper left
                    c += 1

                if i>0  and S[i-1][k]=="#": # right above
                    c += 1

                if i>0 and k<W-1 and S[i-1][k+1]=="#": # upper right
                    c+= 1

                if k>0 and S[i][k-1]=="#": # left
                    c += 1
           
                if k<W-1 and S[i][k+1]=="#": # right
                    c += 1

                if i<H-1 and k>0 and S[i+1][k-1]=="#": # lower left
                    c += 1

                if i<H-1 and S[i+1][k]=="#": # right under
                    c += 1

                if i<H-1 and k<W-1 and S[i+1][k+1]=="#": # lower right
                    c += 1

                S[i][k] = str(c)

    for i in range(H):
        print(("".join(S[i])))


def __starting_point():
    solve()


__starting_point()

import copy

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

dir = [(i, j) for i in range(-1, 2) for j in range(-1, 2)]
ans = copy.deepcopy(s)
for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            cnt = 0
            for y, x in dir:
                iy, jx = i+y, j+x
                if 0 <= iy < h and 0 <= jx < w:
                    if s[iy][jx] == '#':
                        cnt += 1
            ans[i][j] = cnt
for i in ans:
    print((''.join(map(str, i))))


#!/usr/local/bin/python3
# https://atcoder.jp/contests/abc075/tasks/abc075_b

H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]


def check(S, i, j):
    if S[i][j] == '#':
        return '#'

    num = 0
    for _i in range(max(0, i-1), min(H, i+2)):
        for _j in range(max(0, j-1), min(W, j+2)):
            if S[_i][_j] == '#':
                num += 1
    return str(num)


for i in range(H):
    for j in range(W):
        S[i][j] = check(S, i, j)

for i in range(H):
    for j in range(W):
        print(S[i][j], end="")
    print()


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):
        a = 0
        if s[i][j] == '.':
            for k in range(i - 1, i + 2):
                for l in range(j - 1, j + 2):
                    if (0 <= k and k <= h - 1) and (0 <= l and l <= w - 1) and s[k][l] == '#':
                        a += 1
            s[i][j] = str(a)
for m in range(h):
    print(''.join(s[m]))

h,w = map(int,input().split())
s = [[0]*(w+2)]
for i in range(h):
  s += [[0]+list(input())+[0]]
s += [[0]*(w+2)]


for i in range(1,h+1):
    for j in range(1,w+1):
        cnt = 0
        if s[i][j]== "#":
            continue
        if s[i][j+1]=="#":
            cnt += 1
        if s[i][j-1] == "#":
            cnt += 1
        if s[i-1][j] == "#":
            cnt += 1
        if s[i-1][j+1] == "#":
             cnt += 1
        if s[i-1][j-1] == "#":
             cnt += 1
        if s[i+1][j] == "#":
            cnt += 1
        if s[i+1][j-1] == "#":
            cnt += 1
        if s[i+1][j+1] == "#":
            cnt += 1
        s[i][j] = cnt
    
for k in range(1,h+1):
  print(*s[k][1:-1],sep="")

import numpy as np


def get_num_bomb(arr, i, j):
    """
    docstring
    """
    temp_arr = np.full([arr.shape[0] + 2, arr.shape[1] + 2], False)
    temp_arr[1:1 + arr.shape[0], 1:1 + arr.shape[1]] = arr[:, :]
    slice = temp_arr[i:i + 3, j:j + 3]
    return slice.sum()


h, w = tuple([int(x) for x in input().split(" ")])
arr = []
ret_arr = [[0] * w for i in range(h)]
for i in range(h):
    x = list([(False if x == "." else True) for x in input()])
    # x = list(map(lambda x: x, input()))
    arr.append(x)
arr = np.array(arr)
# print(arr)
for i in range(h):
    for j in range(w):
        if arr[i, j] == True:
            ret_arr[i][j] = "#"
        else:
            ret_arr[i][j] = get_num_bomb(arr, i, j)
for lis in ret_arr:
    print(("".join(list([str(x) for x in lis]))))


h,w = list(map(int, input().split()))
S = [input() for _ in range(h)]
S = ['.' + s + '.' for s in S]
S = ['.'*(w+2)] + S + ['.'*(w+2)]
for i in range(1, h+1):
    l = ''
    for j in range(1, w+1):
        if S[i][j] == '#':
            l += '#'
        else:
            l += str([S[i-1][j-1], S[i-1][j],S[i-1][j+1],S[i][j+1],S[i+1][j+1],S[i+1][j],S[i+1][j-1],S[i][j-1]].count('#'))
    print(l)






H, W = map(int, input().split())
x = [list(map(str, list(input()))) for l in range(H)]

#両端にドットを追加
for i in range(len(x)):
    x[i].insert(0, ".")
    x[i].append(".")
#最上部と最下部の行にドットを追加
x.insert(0, ["."]*(W+2))
x.append(["."]*(W+2))

#3*3→5*5 from[1][1] to[3][3] to[H][W]
tmp = 0
for i in range(1, H+1):
    for j in range(1, W+1):
        if x[i][j] == "#":
            x[i][j] = "#"
            continue
        if x[i-1][j-1] == "#":
            tmp += 1
        if x[i-1][j] == "#":
            tmp += 1
        if x[i-1][j+1] == "#":
            tmp += 1
        if x[i][j-1] == "#":
            tmp += 1
        if x[i][j+1] == "#":
            tmp += 1
        if x[i+1][j-1] == "#":
            tmp += 1
        if x[i+1][j] == "#":
            tmp += 1
        if x[i+1][j+1] == "#":
            tmp += 1
        x[i][j] = str(tmp)
        tmp = 0
#最上部と最下部の行のドットを削除
del x[0]
del x[-1]
#両端のドットを削除
for i in range(len(x)):
    del x[i][0]
    del x[i][-1]

for i in range(len(x)):
    print(''.join(x[i]))

import sys

input = sys.stdin.readline
H, W = map(int, input().split())
S = [(input()) for _ in range(H)]
T = [[0 for _ in range(W)] for _ in range(H)]

for h in range(H):
    for w in range(W):
        for i in range(h - 1, h + 2):
            for j in range(w - 1, w + 2):
                if i < 0 or j < 0 or i >= H or j >= W:
                    continue
                else:
                    if S[h][w] == '#':
                        T[h][w] = -1
                    elif S[i][j] == '#':
                        T[h][w] += 1

for t in T:
    print((''.join(map(str, t))).replace('-1', '#'))

h, w = list(map(int, input().split()))
ans = [[0 for _ in range(w+2)] for _ in range(h+2)]

for i in range(h):
    S = input()
    for j, s in enumerate(S):
        if s == "#":
            ans[i+1][j+1] = "#"
            if ans[i][j+1] != "#":
                ans[i][j+1] += 1
            if ans[i+1][j] != "#":
                ans[i+1][j] += 1
            if ans[i][j] != "#":
                ans[i][j] += 1
            if ans[i+1][j + 2] != "#":
                ans[i+1][j + 2] += 1
            if ans[i][j + 2] != "#":
                ans[i][j + 2] += 1
            if ans[i + 2][j+1] != "#":
                ans[i + 2][j+1] += 1
            if ans[i + 2][j + 2] != "#":
                ans[i + 2][j + 2] += 1
            if ans[i + 2][j] != "#":
                ans[i + 2][j] += 1

for a in ans[1:-1]:
    print(("".join(map(str, a))[1:-1]))


#6 B - Minesweeper
H,W = map(int,input().split())
S = [list(input()) for _ in range(H)]

dh = [0,1,0,-1,1,-1,1,-1]
dw = [1,0,-1,0,1,-1,-1,1]

for h in range(H):
    for w in range(W):
        #マス(h,w)について
        if S[h][w] == '#':
            continue
        else:
            cnt= 0
            for v in range(8):
                nxt_h = h + dh[v]
                nxt_w = w + dw[v]
                if not(-1<nxt_h<H) or not(-1<nxt_w<W):
                    continue
                elif S[nxt_h][nxt_w] == '#':
                    cnt += 1
                S[h][w] = str(cnt)
if H==1 and W==1:
    print(0)
else:
    for i in S:
        print(''.join(i))

H, W = list(map(int, input().split()))
grid = []

for _ in range(H):
  grid.append([s for s in input()])

for i in range(H):
  for j in range(W):
    r_ij = grid[i][j]
    if r_ij == "#":
      continue
    c_ij = 0
    if 1<=i<=H-1 and 1<=j<=W-1:
      if grid[i-1][j-1] == "#":
        c_ij += 1
    if 1<=i<=H-1 and 0<=j<=W-1:
      if grid[i-1][j] == "#":
        c_ij += 1
    if 1<=i<=H-1 and 0<=j<=W-2:
      if grid[i-1][j+1] == "#":
        c_ij += 1
    if 0<=i<=H-1 and 1<=j<=W-1:
      if grid[i][j-1] == "#":
        c_ij += 1
    if 0<=i<=H-1 and 0<=j<=W-2:
      if grid[i][j+1] == "#":
        c_ij += 1
    if 0<=i<=H-2 and 1<=j<=W-1:
      if grid[i+1][j-1] == "#":
        c_ij += 1
    if 0<=i<=H-2 and 0<=j<=W-1:
      if grid[i+1][j] == "#":
        c_ij += 1
    if 0<=i<=H-2 and 0<=j<=W-2:
      if grid[i+1][j+1] == "#":
        c_ij += 1
    grid[i][j] = str(c_ij)

str_list = ["".join(s) for s in grid]
for s in str_list:
  print(s)


h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
# ans = [[0]*w for _ in range(h)]

import copy

dir = [(i, j) for i in range(-1, 2) for j in range(-1, 2)]
ans = copy.deepcopy(s)
for i in range(h):
    for j in range(w):
        if s[i][j] == '.':
            cnt = 0
            for y, x in dir:
                iy, jx = i+y, j+x
                if 0 <= iy < h and 0 <= jx < w:
                    if s[iy][jx] == '#':
                        cnt += 1
            ans[i][j] = cnt




for i in range(h):
    moji = map(str,ans[i])
    print("".join(moji))

h, w = map(int, input().split())

sl = [list(input()) for _ in range(h)]
ans = [[0]*(w+2) for _ in range(h+2)]
# print(sl,ans)

for i in range(h):
    for j in range(w):
        row = i+1
        col = j+1
        # print(sl[i][j])
        if sl[i][j] == '#':
            ans[row][col] = '#'
            if ans[row-1][col] != '#':ans[row-1][col] += 1
            if ans[row+1][col] != '#':ans[row+1][col] += 1
            if ans[row][col-1] != '#':ans[row][col-1] += 1
            if ans[row][col+1] != '#':ans[row][col+1] += 1
            if ans[row-1][col-1] != '#':ans[row-1][col-1] += 1
            if ans[row-1][col+1] != '#':ans[row-1][col+1] += 1
            if ans[row+1][col-1] != '#':ans[row+1][col-1] += 1
            if ans[row+1][col+1] != '#':ans[row+1][col+1] += 1



for i in range(h+2):
    if i != 0 and i != h+1:
        print(*ans[i][1:-1], sep='')


import sys

H, W = map(int, input().split())
l = []
for i in range(H):
  l.append(list(input()))

for i in range(H):
  for j in range(W):
    c=0
   
    if l[i][j]=="#":
      pass
   
    else:
      # 左上
      if 0<i and 0<j and l[i-1][j-1]=='#':
        c+=1
      # 上
      if 0<i and l[i-1][j]=='#':
        c+=1
      # 右上
      if 0<i and j<W-1 and l[i-1][j+1]=='#':
        c+=1
      # 左
      if 0<j and l[i][j-1]=='#':
        c+=1
      # 右
      if j<W-1 and l[i][j+1]=='#':
        c+=1
      # 左下
      if i<H-1 and 0<j and l[i+1][j-1]=='#':
        c+=1
      # 下
      if i<H-1 and l[i+1][j]=='#':
        c+=1
      # 右下
      if i<H-1 and j<W-1 and l[i+1][j+1]=='#':
        c+=1
        
              # 地雷隣接数を記載
      l[i][j]=str(c)

# 出力
for i in range(H):
  print(''.join(l[i]))

import itertools
H,W=map(int,input().split())
S=[list(input()) for _ in range(H)]

for x,y in itertools.product(range(W),range(H)):
    if S[y][x]==".":
        res=0
        for i,j in itertools.product(range(-1,2),range(-1,2)):
            if 0<=x+i and x+i<W and 0<=y+j and y+j<H:
                if S[y+j][x+i]=="#":
                    res+=1
        
        S[y][x]=res

for j in range(H):
    L=[str(a) for a in S[j]]
    print(''.join(L))

h, w = list(map(int, input().split()))
field = [list(input()) for _ in range(h)]
dx = [1, 0, -1, 0, 1, -1, -1, 1]
dy = [0, 1, 0, -1, 1, 1, -1, -1]
for i in range(h):
    for j in range(w):
        if field[i][j] == "#":
            continue
        num = 0
        for k in range(8):
            ni = i + dy[k]
            nj = j + dx[k]
            if ni < 0 or h <= ni:
                continue
            if nj < 0 or w <= nj:
                continue
            if field[ni][nj] == "#":
                num += 1
        field[i][j] = str(num)
for out in field:
    print(("".join(out)))


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

# 入力
H, W = map(int, input().split())
S = [input() for _ in range(H)]

# 各マス (i, j) について順に処理
for i in range(H):
    for j in range(W):
        if S[i][j] != '.':
            continue
        counter = 0
        for d in range(8):
            ni = i + dx[d]
            nj = j + dy[d]
            if ni < 0 or ni >= H or nj < 0 or nj >= W:
                continue
            if S[ni][nj] == '#':
                counter += 1
        S[i] = S[i].replace('.', str(counter), 1)

for row in S:
    print(row)

h,w=map(int,input().split())
s=[list(input()) for i in range(h)]

data=[[0]*w for i in range(h)]
for i in range(h):
  for j in range(w):
    if s[i][j]=='#':
      data[i][j]='#'
    else:
      for p in range(-1,2):
        for q in range(-1,2):
          if 0<=i+p<h and 0<=j+q<w and s[i+p][j+q]=='#':
            data[i][j]+=1
for i in range(h):
  print(*data[i],sep='')

# 0: 下、1: 右、2: 上、3: 左、4: 右下、5: 右上、6: 左上、7: 左下
dx = [1, 0, -1, 0, 1, -1, -1, 1]
dy = [0, 1, 0, -1, 1, 1, -1, -1]

# 入力
H, W = map(int, input().split())
S = [input() for _ in range(H)]

# Python3 では str 型の要素の書き換えはできない
# 答えを格納する二次元配列を別途用意する ('.' のところは 0 とする)
result = [[0 if v == '.' else '#' for v in row] for row in S]

# 各マス (i, j) について順に処理
for i in range(H):
    for j in range(W):
        if S[i][j] != '.':
            continue
        for d in range(8):
            ni = i + dx[d]
            nj = j + dy[d]
            if ni < 0 or ni >= H or nj < 0 or nj >= W:
                continue
            if S[ni][nj] == '#':
                result[i][j] += 1

for row in result:
    # 出力形式に合わせて出力
    print(*row, sep = '')

import sys

input = sys.stdin.readline

def main():
    ans = []
    b = "#"
    h, w = map(int, input().split())
    s = [input().rstrip('\n') for _ in range(h)]
    for y in range(h):
        ans.append([])
        for x in range(w):
            if s[y][x] == b:
                ans[y].append(b)
            else:
                ans[y].append(0)
    for y in range(h):
        for x in range(w):
            if s[y][x] == b:
                if y > 0 and x > 0 and ans[y-1][x-1] != b:
                    ans[y-1][x-1] += 1
                if y > 0 and ans[y-1][x] != b:
                    ans[y-1][x] += 1
                if y > 0 and x < w - 1 and ans[y-1][x+1] != b:
                    ans[y-1][x+1] += 1
                if x > 0 and ans[y][x-1] != b:
                    ans[y][x-1] += 1
                if x < w - 1 and ans[y][x+1] != b:
                    ans[y][x+1] += 1
                if y < h - 1 and x > 0 and ans[y+1][x-1] != b:
                    ans[y+1][x-1] += 1
                if y < h - 1 and ans[y+1][x] != b:
                    ans[y+1][x] += 1
                if y < h - 1 and x < w - 1 and ans[y+1][x+1] != b:
                    ans[y+1][x+1] += 1
    for y in range(h):
        for x in range(w):
            if ans[y][x] == 0 and s[y][x] == b:
                ans[y][x] = "#"
            else:
                ans[y][x] = str(ans[y][x])
        print(''.join(ans[y]))

def __starting_point():
    main()
__starting_point()

H,W = list(map(int,input().split()))
s = []
for i in range(H):
    a = list(input())
    s.append(a)
for i in range(H):
    for j in range(W):
        count = 0
        if s[i][j] == ".":
            for k in range(i-1,i+2):
                for l in range(j-1,j+2):
                    if (0 <= k <= H-1) and (0 <= l <= W-1) and s[k][l] == "#":
                        count += 1
            s[i][j] = str(count)
for i in range(H):
    print(("".join(s[i])))


lst = input().split()
H = int(lst[0])
W = int(lst[1])

field = []
for i in range(H):
   l = []
   s = input()
   for j in range(W):
      l.append(s[j])
   field.append(l)

def count(x, y):
   result = 0
   for P in [[x-1, y-1], [x, y-1], [x+1, y-1], [x-1, y], [x+1, y], [x-1, y+1], [x, y+1], [x+1, y+1]]:
      try:
         if (0 <= P[0]) and (0 <= P[1]) and (field[P[1]][P[0]] == '#'):
            result += 1
      except:
         pass
   return result

for i in range(H):
   for j in range(W):
      if field[i][j] == '.':
         field[i][j] = str(count(j, i))

def list2str(L):
   result = ''
   for t in L:
      result += t
   return result

for I in field:
   print(list2str(I))

h,w=map(int,input().split())
s=[list(input()) for i in range(h)]

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

result = [[0]*w for _ in range(h)]
for y in range(h):
  for x in range(w):
    if s[y][x] == '#':
      result[y][x] = '#'
      continue

    count = 0
    for i in range(8):
      x_round = x + dx[i]
      y_round = y + dy[i]
      
      if x_round < 0 or w <= x_round or y_round < 0 or h <= y_round:
        continue
        
      if s[y_round][x_round] == '#':
        count += 1

    result[y][x] = count

for y in range(h):
  for x in range(w):
    print(result[y][x], end='')
  print('')


def mapt(fn, *args):
    return tuple(map(fn, *args))


def Input():
    return mapt(int, input().split(" "))


def check(i, j, s):
    bomb = 0
    for x, y in [(i+1, j), (i-1, j), (i, j+1), (i, j-1), (i-1, j-1), (i-1, j+1), (i+1, j-1), (i+1, j+1)]:
        if s[x][y] == "#":
            bomb += 1
    return str(bomb)


def main():
    h, w = Input()
    p = ["x"] * (w + 2)
    s = [p] + [["x"] + list(input()) + ["x"] for i in range(h)] + [p]
    for i in range(1, h + 1):
        ans = ""
        for j in range(1, w + 1):
            if s[i][j] == ".":
                ans += check(i, j, s)
            else:
                ans += "#"
        print(ans)


main()

def sarch(i,j):
    cnt=0
    i1=[i-1,i,i+1]
    j1=[j-1,j,j+1]
    for x in range(3):
        for y in range(3):
            if S[i1[x]][j1[y]]=="#":
                cnt+=1
    return cnt

import numpy as np
H,W=map(int,input().split())
S=np.array([list(str(input())) for i in range(H)])


gyo_0=np.array(["."]*W)
retu_0=np.array([["."]]*(H+2))

S=np.vstack((gyo_0,S))
S=np.vstack((S,gyo_0))
S=np.hstack((retu_0,S))
S=np.hstack((S,retu_0))

for i in range(1,H+1):
    for j in range(1,W+1):
        if S[i][j]==".":
            S[i][j]=sarch(i,j)
        print(S[i][j],end="")
    print()

h,w = list(map(int,input().split()))
l = [["."]*(w+2)]
for i in range(h):
    l.append(["."] + [i for i in input()] + ["."])
l.append(["."]*(w+2))

def count(i,j):
    if l[i][j] == "#":
        return "#"
    c = 0
    for n in range(i-1,i+2):
        for m in range(j-1,j+2):
            if l[n][m] == "#":
                c += 1
    return str(c)

ans = ["".join([count(i,j) for j in range(1,1+w)]) for i in range(1,1+h)]

print(("\n".join(ans)))


h,w = map(int, input().split())
m=[]

for i in range(h):
  m.append(input())

ans=[["#"]*(w) for _ in range(h)]
for i in range(h):
  for j in range(w):
    cnt = 0
    if h > i-1 >= 0:
      if m[i-1][j] == "#":
        cnt += 1
      if w > j-1 >=0 and m[i-1][j-1] == "#":
        cnt += 1
      if w > j+1 >=0 and m[i-1][j+1] == "#":
        cnt += 1
    if h > i+1 >= 0:
      if m[i+1][j] == "#":
        cnt += 1
      if w > j-1 >=0 and m[i+1][j-1] == "#":
        cnt += 1
      if w > j+1 >=0 and m[i+1][j+1] == "#":
        cnt += 1
      
    if w > j-1 >=0 and m[i][j-1] == "#":
      cnt += 1
    if w > j+1 >=0 and m[i][j+1] == "#":
      cnt += 1
    if m[i][j] != "#":
      ans[i][j] = str(cnt)

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

import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: list(map(int, sys.stdin.readline().split()))
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7


h, w = rm()
s = [rr() for _ in range(h)]
x = [0, 0, 1, -1, 1, 1, -1, -1]
y = [1, -1, 0, 0, 1, -1, 1, -1]
ans = ['']*h
for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            ans[i] += '#'
        else:
            c = 0
            for dx, dy in zip(x, y):
                if 0 <= i + dx < h and 0 <= j + dy < w:
                    if s[i+dx][j+dy] == '#':
                        c += 1
            ans[i] += str(c)
for i in ans:
    print(i)






h,w = map(int,input().split())
m = [input() for _ in range(h)]
vx = [0,1,1,1,0,-1,-1,-1]
vy = [1,1,0,-1,-1,-1,0,1]
ans = [['#']*w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if m[i][j] == '#':
            ans[i][j] = '#'
            continue
        count = 0
        for k in range(8):
            ny = vy[k] + i
            nx = vx[k] + j
            if 0<=ny and ny<h and 0<=nx and nx<w:
                if m[ny][nx] == '#':
                    count += 1
        ans[i][j] = str(count)
for i in ans:
    print(''.join(i))

h, w = map(int, input().split())
s = [input() for  i in range(h)]

ans  =[]
for row in s:
        ans.append(list(row))
        
dxy = [(-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1)]
for x in range(h):
        for y in range(w):
                if s[x][y] == "#":
                        continue
                c = 0
                for dx, dy in dxy:
                        if x +dx < 0 or x +dx > h-1 or y + dy < 0 or y + dy > w-1:
                                continue
                        else:
                                if s[x + dx][y + dy] == "#":
                                        c += 1
                ans[x][y] = c
for row in ans:
        print("".join(list(map(str, row))))

from collections import deque
h, w = map(int, input().split())
mine = [[] for _ in range(h+2)]
mine[0] = deque(["." for _ in range(w+2)])
mine[-1] = deque(["." for _ in range(w+2)])
for i in range(h):
    mine[i+1] = deque(list(input()))
    mine[i+1].append(".")
    mine[i+1].appendleft(".")
for i in range(1, h+1):
    for j in range(1, w+1):
        if mine[i][j] == "#":
            print("#", end="")
        else:
            counter = 0
            for k in range(-1, 2):
                for l in range(-1, 2):
                    if (k or l) and mine[i+k][j+l] == "#":
                        counter += 1
            print(counter, end="")
    print()

h,w = map(int,input().split())
s = [list(input()) for _ in range(h)]
bomb_count = 0
for i in range(h):
  for j in range(w):
    if s[i][j] == "#":
      continue
    else:
      for k in range(3):
        for l in range(3):
          a = i-1+k
          b = j-1+l
          if a < 0:
            continue
          elif a > h-1:
            continue
          if b < 0:
            continue
          elif b > w-1:
            continue
          if s[a][b] == "#":
            bomb_count += 1
      s[i][j] = str(bomb_count)
      bomb_count = 0
for i in range(h):
  print("".join(s[i]))


def mine_count(x, y, ans):
    ans[y][x] = 0
    for nx, ny in (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1):
        if 0 <= y+ny < H and 0 <= x+nx < W:
            if ans[y+ny][x+nx] == "#":
                ans[y][x] += 1
    return ans


H, W = map(int, input().split())
S = [list(input()) for _ in range(H)]
ans = S
for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            ans = mine_count(j, i, ans)

for row in ans:
    for item in row:
        print(item, end="")
    print()


h,w=map(int, input().split())
s=["."*(w+2)]
for i in range(h):
  s.append("."+input()+".")
s.append("."*(w+2))
d=[-1,0,1]
for i in range(1,h+1):
  new=""
  for j in range(1,w+1):
    if s[i][j]=="#":
      new=new+"#"
    else:
      count=0
      for k in range(3):
        for l in range(3):
          if s[i+d[k]][j+d[l]]=="#":
            count+=1
      new=new+str(count)
  print(new)

H,W = map(int,input().split())
S = [list(input()) for _ in range(H)]

ans = [[0 for _ in range(W)] for _ in range(H)]
move = [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]

for i in range(H):
  for j in range(W):
    if S[i][j] == "#":
      ans[i][j] = "#"
    else:
      for x, y in move:
        nx, ny = i+x, j+y
        if nx < 0 or H <= nx or ny < 0 or W <= ny:
          continue
        elif S[nx][ny] == "#":
          ans[i][j] += 1
          
for a in ans:
  for i in range(W):
    a[i] = str(a[i])
  print("".join(a))

h, w = map(int, input().split())
s = [list(input()) for i in range(h)]
ans = [[0] * (w + 2) for i in range(h + 2)]

for i in range(1, h + 1):
    for j in range(1, w + 1):
        if s[i - 1][j - 1] == "#":
            ans[i - 1][j - 1] += 1
            ans[i - 1][j] += 1
            ans[i - 1][j + 1] += 1
            ans[i][j - 1] += 1
            ans[i][j + 1] += 1
            ans[i + 1][j - 1] += 1
            ans[i + 1][j] += 1
            ans[i + 1][j + 1] += 1

del ans[0]
del ans[-1]
for i in range(h):
    del ans[i][0]
    del ans[i][-1]
for i, _ in enumerate(s):
    tmp = ""
    for j, word in enumerate(_):
        if word == "#":
            tmp += "#"
        else:
            tmp += str(ans[i][j])
    print(tmp, end="\n")


H, W = map(int, input().split())
L = []
for i in range(H):
    S = list(input())
    L.append(S)

for i in range(H):
    for j in range(len(L[i])):
        if L[i][j] == '.':
            cnt = 0
            for k in range(-1, 2):
                for l in range(-1, 2):
                    if ((i+k) < H) and ((j+l) < len(L[i])) and ((j+l) >= 0) and ((i+k) >= 0):
                        if L[i+k][j+l] == '#':
                            cnt += 1
            L[i][j] = str(cnt)

for i in range(len(L)):
    print(''.join(L[i]))

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):
        if S[i][j] == '.':
            cnt = 0
            for k in range(-1, 2):
                for l in range(-1, 2):
                    if 0 <= i + k < H and 0 <= j + l < W and S[i+k][j+l] == '#':
                        cnt += 1
            S[i][j] = str(cnt)
for i in range(H):
    print(''.join(S[i]))