AtCoder Beginner Contest 075 - Minesweeper

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: EASY

Starter code:

Solutions:
(h, w) = map(int, input().split())
s = [input() for _ in range(h)]
for i in range(h):
	l = ''
	for j in range(w):
		if s[i][j] == '#':
			l += '#'
		else:
			x = 0
			for dh in [-1, 0, 1]:
				for dw in [-1, 0, 1]:
					if 0 <= i + dh < h and 0 <= j + dw < w and (s[i + dh][j + dw] == '#'):
						x += 1
			l += str(x)
	print(l)


(h, w) = map(int, input().split())
e = ['*' for i in range(w + 2)]
s = [e]
for i in range(h):
	s.append(['*'] + list(input()) + ['*'])
s.append(e)
for i in range(1, h + 1):
	for j in range(1, w + 1):
		if s[i][j] == '.':
			s[i][j] = str(sum((s[k][j - 1:j + 2].count('#') for k in range(i - 1, i + 2))))
	print(''.join(s[i][1:w + 1]))


import sys
(h, w) = map(int, input().split())
s = [['.' for _ in range(w + 2)]] + [['.'] + list(input()) + ['.'] for _ in range(h)] + [['.' for _ in range(w + 2)]]
for r in range(1, h + 1):
	for c in range(1, w + 1):
		if s[r][c] == '.':
			s[r][c] = str([s[r + dr][c + dc] for dr in [-1, 0, 1] for dc in [-1, 0, 1]].count('#'))
for r in range(1, h + 1):
	print(''.join(s[r][1:w + 1]))


(h, w) = map(int, input().split())
s = [['a'] * (w + 2)] + [['a'] + list(input()) + ['a'] for i in range(h)] + [['a'] * (w + 2)]
for i in range(1, h + 1):
	for j in range(1, w + 1):
		if s[i][j] != '#':
			c = [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]]
			s[i][j] = str(c.count('#'))
for i in range(1, h + 1):
	print(''.join(s[i][1:-1]))


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


(H, W) = map(int, input().split())
S = [list(input()) for _ in range(H)]
d = [[-1, -1], [-1, 0], [0, -1], [1, 0], [-1, 1], [0, 1], [1, 1], [1, -1]]
for i in range(H):
	for j in range(W):
		if S[i][j] == '.':
			cnt = 0
			for k in d:
				(nx, ny) = (i - k[0], j - k[1])
				if 0 <= nx < H and 0 <= ny < W and (S[nx][ny] == '#'):
					cnt += 1
			S[i][j] = str(cnt)
for s in S:
	print(''.join(s))


(h, w) = map(int, input().split())
l = []
d = [-1, 0, 1]
for i in range(h):
	a = list(input())
	l.append(a)
for i in range(h):
	for j in range(w):
		cnt = 0
		for x in d:
			for y in d:
				if 0 <= i + x < h and 0 <= j + y < w:
					if l[i + x][j + y] == '#':
						cnt += 1
		if l[i][j] == '.':
			l[i][j] = cnt
for i in l:
	print(''.join(map(str, i)))


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


(h, w) = [int(v) for v in input().split()]
field = [list(input()) for i in range(h)]
ansfield = [['#' for j in range(w)] for i in range(h)]
for i in range(h):
	for j in range(w):
		if field[i][j] == '.':
			ansfield[i][j] = str(sum([[field[i][j] for j in range(max(j - 1, 0), min(w, j + 2))].count('#') for i in range(max(i - 1, 0), min(h, i + 2))]))
for i in range(h):
	print(''.join(ansfield[i]))


from collections import deque
(h, w) = map(int, input().split())
hw = [list(input()) for _ in range(h)]
for i in range(h):
	for j in range(w):
		if hw[i][j] == '.':
			cnt = 0
			for x in [-1, 0, 1]:
				for y in [-1, 0, 1]:
					if i + y in range(h) and j + x in range(w):
						if hw[i + y][j + x] == '#':
							cnt += 1
			hw[i][j] = cnt
for line in hw:
	print(''.join(list(map(str, line))))


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


(h, w) = map(int, input().strip().split())
F = []
for _ in range(h):
	F.append(list(input()))
for i in range(h):
	for j in range(w):
		if F[i][j] == '.':
			u = max(i - 1, 0)
			d = min(i + 2, h)
			l = max(j - 1, 0)
			r = min(j + 2, w)
			n = 0
			for line in F[u:d]:
				n += line[l:r].count('#')
			F[i][j] = str(n)
for line in F:
	print(''.join(line))


(H, W) = map(int, input().split())
S = [input() for _ in range(H)]
for h in range(H):
	l = ''
	for w in range(W):
		if S[h][w] == '#':
			l += '#'
		else:
			x = 0
			for dh in [-1, 0, 1]:
				for dw in [-1, 0, 1]:
					if 0 <= h + dh < H and 0 <= w + dw < W and (S[h + dh][w + dw] == '#'):
						x += 1
			l += str(x)
	print(l)


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
	elif i == 0:
		start_i = 0
	elif i == W - 1:
		end_i = 1
	if H == 1:
		start_j = 0
		end_j = 1
	elif 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]))


(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 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='')


(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 = [list(input()) for _ in range(h)]
d = [-1, 0, 1]
for i in range(h):
	for j in range(w):
		if s[i][j] == '.':
			total = 0
			for dy in d:
				for dx in d:
					if 0 <= i + dy < h and 0 <= j + dx < w and (s[i + dy][j + dx] == '#'):
						total += 1
			s[i][j] = str(total)
for i in range(h):
	print(''.join(s[i]))


(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 = []
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) = 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))


(h, w) = map(int, input().split())
s = eval('list(input()),' * h)
for i in range(h):
	for j in range(w):
		c = 0
		for a in range(i - 1, i + 2):
			for b in range(j - 1, j + 2):
				c += all((min(a, b) >= 0, a < h, b < w)) and s[a][b] == '#'
		if s[i][j] == '.':
			s[i][j] = c
for c in s:
	print(*c, sep='')


(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) = map(int, input().split())
grid = ['.' * (w + 2)] + ['.' + input() + '.' for _ in range(h)] + ['.' * (w + 2)]
for y in range(1, h + 1):
	for x in range(1, w + 1):
		if grid[y][x] == '#':
			print('#', end='')
		else:
			print(sum((grid[i][j] == '#' for i in range(y - 1, y + 2) for j in range(x - 1, x + 2))), end='')
	print('')


(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] == '.':
			S[i][j] = str(sum((S[x][y] == '#' for x in range(max(0, i - 1), min(H, i + 2)) for y in range(max(0, j - 1), min(W, j + 2)))))
for s in S:
	print(''.join(s))


(h, w) = map(int, input().split())
slis = []
a = ['.'] * (w + 2)
slis.append(a)
for i in range(h):
	s = list('.' + input() + '.')
	slis.append(s)
slis.append(a)
for i in range(1, h + 1):
	for j in range(1, w + 1):
		a = slis[i - 1][j - 1:j + 2] + slis[i][j - 1:j + 2] + slis[i + 1][j - 1:j + 2]
		if slis[i][j] != '#':
			slis[i][j] = a.count('#')
for i in range(1, h + 1):
	print(*slis[i][1:w + 1], sep='')


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)]
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())
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='')


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())
s = ['*' * (w + 2)]
for i in range(h):
	s.append('*' + input() + '*')
s += ['*' * (w + 2)]
a = [[0] * w for _ in range(h)]
for i in range(h):
	for j in range(w):
		if s[i + 1][j + 1] == '#':
			a[i][j] = '#'
		else:
			a[i][j] = (s[i][j:j + 3] + s[i + 1][j:j + 3] + s[i + 2][j:j + 3]).count('#')
for i in a:
	print(''.join(map(str, i)))


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)))


(h, w) = map(int, input().split())
m = [input() for _ in range(h)]
for i in range(h):
	l = ''
	for j in range(w):
		if m[i][j] == '#':
			l += '#'
		else:
			l += str(sum([t[max(0, j - 1):min(w, j + 2)].count('#') for t in m[max(0, i - 1):min(h, i + 2)]]))
	print(l)


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]))


(H, W) = map(int, input().split())
area = []
for i in range(H):
	area.append(input())
for i in range(H):
	for j in range(W):
		if area[i][j] == '#':
			print('#', end='')
		else:
			count = 0
			for k in range(max(0, i - 1), min(i + 1, H - 1) + 1):
				for l in range(max(0, j - 1), min(j + 1, W - 1) + 1):
					if area[k][l] == '#':
						count += 1
			print(count, end='')
	print()


import numpy as np

def get_num_bomb(arr, i, j):
	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()])
	arr.append(x)
arr = np.array(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) = map(int, input().split())
S = [input() for i in range(h)]
for i in range(h):
	for j in range(w):
		if S[i][j] == '#':
			print('#', end='')
			continue
		cnt = 0
		for dx in [-1, 0, 1]:
			for dy in [-1, 0, 1]:
				if not 0 <= i + dx < h or not 0 <= j + dy < w:
					continue
				if S[i + dx][j + dy] == '#':
					cnt += 1
		print(cnt, end='')
	print()


(h, w) = map(int, input().split())
ss = [input() for _ in [0] * h]
for i in range(h):
	ans = ''
	for j in range(w):
		if ss[i][j] == '#':
			ans += '#'
			continue
		n = 0
		if i > 0:
			n += ss[i - 1][max(0, j - 1):j + 2].count('#')
		n += ss[i][max(0, j - 1):j + 2].count('#')
		if i != h - 1:
			n += ss[i + 1][max(0, j - 1):j + 2].count('#')
		ans += str(n)
	print(ans)


def checkmine(i, j):
	count = 0
	for a in range(-1, 2):
		for b in range(-1, 2):
			if 0 <= a + i < n and 0 <= b + j < m:
				if field[a + i][b + j] == '#':
					count += 1
	return count
(n, m) = map(int, input().split())
field = []
for i in range(n):
	field.append(list(input()))
for i in range(n):
	for j in range(m):
		if field[i][j] == '.':
			field[i][j] = checkmine(i, j)
for i in field:
	print(''.join(map(str, i)))


(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]))


(h, w) = map(int, input().split())
S = [input() for i in range(h)]
for r in range(h):
	l = ''
	for c in range(w):
		if S[r][c] == '#':
			l += '#'
		else:
			l += str(sum([x[max(0, c - 1):min(c + 2, w)].count('#') for x in S[max(0, r - 1):min(r + 2, h)]]))
	print(l)


(H, W) = map(int, input().split())
S = []
for _ in range(H):
	S.append([s for s in input()])
for h in range(H):
	for w in range(W):
		if S[h][w] == '.':
			a = 0
			for x in [-1, 0, 1]:
				for y in [-1, 0, 1]:
					if 0 <= h + x < H and 0 <= w + y < W:
						if S[h + x][w + y] == '#':
							a += 1
			S[h][w] = str(a)
for _ in range(H):
	print(''.join(S[_]))


(h, w) = map(int, input().split())
s = ['.' * (w + 2)] + [list('.' + input() + '.') for _ in range(h)] + ['.' * (w + 2)]
for i in range(1, h + 1):
	for j in range(1, w + 1):
		if s[i][j] == '.':
			s[i][j] = sum([s[i + _i][j + _j] == '#' for _i in range(-1, 2) for _j in range(-1, 2)])
	print(*s[i][1:w + 1], sep='')


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 = [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='')


(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 = [[0] * (w + 2)]
for i in range(h):
	t = [0]
	u = input()
	for j in range(w):
		t.append(u[j])
	t.append(0)
	s.append(t)
s.append([0] * (w + 2))
for i in range(1, h + 1):
	v = []
	for j in range(1, w + 1):
		x = [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]]
		if s[i][j] == '#':
			v.append('#')
		else:
			v.append(x.count('#'))
	print(*v, sep='')


(H, W) = map(int, input().split())
grid = [input() for _ in range(H)]
ans = []
for (i, g) in enumerate(grid):
	r = ''
	for (j, s) in enumerate(g):
		if s == '#':
			r += s
		else:
			r += str(sum([temp[max(j - 1, 0):min(j + 1, W) + 1].count('#') for temp in grid[max(i - 1, 0):min(i + 1, H) + 1]]))
	ans.append(r)
print(*ans, sep='\n')


(h, w) = map(int, input().split())
s = [list(input().replace('.', '0')) for i in range(h)]
for i in range(h):
	for j in range(w):
		if s[i][j] == '#':
			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] != '#'):
						s[i + k][j + l] = str(int(s[i + k][j + l]) + 1)
for i in range(h):
	for j in range(w):
		print(s[i][j], end='')
	print()


(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('')


(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])))


(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()


(H, W) = map(int, input().split())
S = [input() for _ in range(H)]
for i in range(H):
	rs = ''
	for j in range(W):
		if S[i][j] == '#':
			rs += '#'
		else:
			tmp = 0
			for s in S[max(0, i - 1):min(H, i + 2)]:
				tmp += s[max(0, j - 1):min(W, j + 2)].count('#')
			rs += str(tmp)
	print(rs)


(h, w) = [int(x) for x in input().split()]
grid = [[b for b in input()] for i in range(h)]
result = []
for i in range(h):
	row = ''
	for j in range(w):
		if grid[i][j] == '#':
			row += '#'
		else:
			row += str(sum([area[max(0, j - 1):min(w, j + 2)].count('#') for area in grid[max(0, i - 1):min(h, i + 2)]]))
	result.append(row)
print('\n'.join(result))


(h, w) = map(int, input().split())
f = [input() for _ in range(h)]
for i in range(h):
	for j in range(w):
		if f[i][j] == '#':
			print('#', end='')
			continue
		cnt = 0
		for k in range(i - 1, i + 2):
			if k < 0 or h <= k:
				continue
			for l in range(j - 1, j + 2):
				if l < 0 or w <= l:
					continue
				if f[k][l] == '#':
					cnt += 1
		print(cnt, end='')
	print()


(H, W) = map(int, input().split())
S = [[-9999 if s == '#' else 0 for s in input()] for _ in range(H)]
for h in range(H):
	for w in range(W):
		if S[h][w] < 0:
			for xh in range(max(0, h - 1), min(H, h + 2)):
				for xw in range(max(0, w - 1), min(W, w + 2)):
					if xh != h or xw != w:
						S[xh][xw] += 1
print('\n'.join([''.join(['#' if x < 0 else str(x) for x in row]) for row in S]))


(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')


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) = (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)


(H, W) = map(int, input().split())
A = []
for i in range(H):
	A.append(list(input()))
for h in range(H):
	for w in range(W):
		if A[h][w] == '#':
			print('#', end='')
		elif A[h][w] == '.':
			c = 0
			for i in range(-1, 2):
				for j in range(-1, 2):
					if 0 <= h + i <= H - 1 and 0 <= w + j <= W - 1 and (A[h + i][w + j] == '#'):
						c += 1
			print(c, end='')
	print('')


(H, W) = map(int, input().split())
S = [input() for _ in [0] * H]
print('\n'.join((''.join(([str(sum((s[max(0, w - 1):min(W, w + 2)].count('#') for s in S[max(0, h - 1):min(H, h + 2)]))), '#'][S[h][w] == '#'] for w in range(W))) for h in range(H))))


(h, w) = map(int, input().split())
s = [input() for _ in range(h)]
for i in range(h):
	ans = ''
	for j in range(w):
		if s[i][j] == '#':
			ans += '#'
		else:
			tmp = 0
			for k in s[max(0, i - 1):min(h, i + 2)]:
				tmp += k[max(0, j - 1):min(w, j + 2)].count('#')
			ans += str(tmp)
	print(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) = 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))


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

def search(x, y):
	return sum(((s[x + i][y + j] == '#') + 0 for i in range(-1, 2) for j in range(-1, 2)))
for i in range(1, h + 1):
	for j in range(1, w + 1):
		print(search(i, j) if s[i][j] == '.' else '#', end='')
	print()


(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] == '#':
			continue
		cnt = 0
		for x in [-1, 0, 1]:
			for y in [-1, 0, 1]:
				if not 0 <= i + y < h or not 0 <= j + x < w:
					continue
				if S[i + y][j + x] == '#':
					cnt += 1
		S[i][j] = str(cnt)
for s in S:
	print(''.join(s))


(H, W) = map(int, input().split())
T = []
T.append(['.'] * (W + 2))
for _ in range(H):
	s = '.' + input() + '.'
	T.append(list(s))
T.append(['.'] * (W + 2))
for h in range(1, H + 1):
	for w in range(1, W + 1):
		if T[h][w] == '.':
			S = [T[a][b] for b in range(w - 1, w + 2) for a in range(h - 1, h + 2)]
			T[h][w] = S.count('#')
for h in range(1, H + 1):
	print(*T[h][1:W + 1], 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) = map(int, input().split())
S = []
for _ in range(H):
	a = ['.']
	a.extend(list(map(str, input())))
	a.append('.')
	S.append(a)
S.insert(0, ['.'] * (W + 2))
S.insert(H + 1, ['.'] * (W + 2))
for i in range(1, H + 1):
	for j in range(1, W + 1):
		if S[i][j] == '.':
			S[i][j] = str((S[i - 1][j - 1:j + 2] + S[i][j - 1:j + 2] + S[i + 1][j - 1:j + 2]).count('#'))
	print(''.join(S[i][1:-1]))


(H, W) = map(int, input().split())
S = [list(input()) for i in range(H)]
dx = [0, -1, 1]
dy = [0, -1, 1]
for i in range(H):
	for j in range(W):
		cnt = 0
		if S[i][j] != '#':
			for x in dx:
				for y in dy:
					if 0 <= i + y < H and 0 <= j + x < W:
						if S[i + y][j + x] == '#':
							cnt += 1
			S[i][j] = str(cnt)
for s in S:
	print(''.join(s))


(H, W) = map(int, input().split())
s = [input() for i in range(H)]
for i in range(H):
	l = ''
	for j in range(W):
		if s[i][j] == '#':
			l += '#'
		else:
			l += str(sum([t[max(0, j - 1):min(W, j + 2)].count('#') for t in s[max(0, i - 1):min(H, i + 2)]]))
	print(l)


(h, w) = map(int, input().split())
s = [[x for x in input()] for i in range(h)]
for i in range(h):
	for j in range(w):
		if s[i][j] == '.':
			count = 0
			for di in range(-1, 2):
				for dj in range(-1, 2):
					if not (0 <= i + di < h and 0 <= j + dj < w):
						pass
					elif s[i + di][j + dj] == '#':
						count += 1
			s[i][j] = str(count)
	print(''.join(s[i]))


(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()


(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))


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])


(a, b) = map(int, input().split())
l = [['.'] * (b + 2)] + [['.'] + list(input()) + ['.'] for _ in range(a)] + [['.'] * (b + 2)]
for i in range(1, a + 1, 1):
	for j in range(1, b + 1, 1):
		if l[i][j] != '#':
			l[i][j] = str(sum((l[x][y] == '#' for x in [i - 1, i, i + 1] for y in [j - 1, j, j + 1])))
for i in range(1, a + 1, 1):
	print(''.join(l[i][1:-1]))


(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])


(h, w) = map(int, input().split())
S = [['.' for _ in range(w + 2)]]
S += [['.'] + [i for i in input()] + ['.'] for _ in range(h)]
S += [['.' for _ in range(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(sum((S[i + n][j + m] == '#' for n in range(-1, 2, 1) for m in range(-1, 2, 1))))
	print(l)


(H, W) = [int(_) for _ in input().split()]
S = ['.' * (H + 2)] + ['.' + input() + '.' for i in range(H)] + ['.' * (H + 2)]
for y in range(H):
	for x in range(W):
		c = S[y + 1][x + 1]
		if c != '#':
			c = str(sum([S[i][x:x + 3].count('#') for i in range(y, y + 3)]))
		print(c, sep='', end='')
	print()


(h, w) = map(int, input().split())
s = ['.' + input() + '.' for _ in range(h)]
s.insert(0, '.' * (w + 2))
s.append('.' * (w + 2))
ans = ''
for i in range(1, h + 1):
	for j in range(1, w + 1):
		if s[i][j] == '.':
			ans += 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:
			ans += '#'
	print(ans)
	ans = ''


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()


(H, W) = map(int, input().split())
S = [list(input() + '.') for i in range(H)]
S.append(['.'] * (W + 1))
a = [[0] * W for i in range(H)]
for y in range(H):
	for x in range(W):
		if S[y][x] == '.':
			a[y][x] = len([1 for i in range(-1, 2) for j in range(-1, 2) if S[i + y][j + x] == '#'])
		else:
			a[y][x] = '#'
for i in a:
	print(''.join(list(map(str, i))))


(H, W) = map(int, input().split(' '))
maps = [list('.' + input() + '.') for _ in range(H)]
maps = [['.'] * (W + 2)] + maps + [['.'] * (W + 2)]
for i in range(1, H + 1):
	for j in range(1, W + 1):
		if maps[i][j] == '.':
			maps[i][j] = (maps[i - 1][j - 1:j + 2] + maps[i][j - 1:j + 2] + maps[i + 1][j - 1:j + 2]).count('#')
maps.pop(0)
maps.pop(H)
for i in maps:
	print(''.join(map(str, i[1:-1])))


(H, W) = [int(i) for i in input().split()]
S = ['.' * (W + 2)]
for i in range(H):
	S.append('.' + input() + '.')
S.append('.' * (W + 2))
mine = [[0 for _ in range(W)] for _ in range(H)]
for j in range(H):
	for i in range(W):
		if S[j + 1][i + 1] == '#':
			mine[j][i] = '#'
		else:
			mine[j][i] = sum([s[i:i + 3].count('#') for s in S[j:j + 3]])
for m in mine:
	print(''.join([str(i) for i in m]))


(h, w) = map(int, input().split())
li = ['..' + '.' * w]
for i in range(h):
	li.append('.' + input() + '.')
li.append('..' + '.' * w)
for i in range(1, h + 1):
	ans = ''
	for j in range(1, w + 1):
		if li[i][j] == '#':
			ans += '#'
		else:
			count = 0
			for a in [-1, 0, 1]:
				for b in [-1, 0, 1]:
					if li[i + a][j + b] == '#':
						count += 1
			ans += str(count)
	print(ans)


(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())
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))


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)))


(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)]
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):
		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) = map(int, input().split())
A = ['.' * (W + 2)] + ['.' + input() + '.' for _ in range(H)] + ['.' * (W + 2)]
for i in range(1, H + 1):
	for j in range(1, W + 1):
		if A[i][j] == '#':
			L = '#'
		else:
			L = sum([1 for x in range(-1, 2) for y in range(-1, 2) if A[i + x][j + y] == '#'])
		print(L, end='')
	print()


from itertools import product
(H, W) = list(map(int, input().split()))
M = [list(input()) for _ in range(H)]
S = set(product([-1, 0, 1], repeat=2)) - {(0, 0)}
for i in range(H):
	for j in range(W):
		if M[i][j] == '#':
			continue
		c = 0
		for k in S:
			h = i + k[0]
			w = j + k[1]
			if 0 <= h < H and 0 <= w < W:
				if M[h][w] == '#':
					c += 1
		M[i][j] = str(c)
for i in range(H):
	print(''.join(M[i]))


(h, w) = map(int, input().split())
a = [list(input()) for _ in [0] * h]
for y in range(h):
	for x in range(w):
		c = 0
		if a[y][x] == '.':
			for i in range(8):
				u = x + [-1, -1, -1, 0, 0, 1, 1, 1][i]
				v = y + [-1, 0, 1, -1, 1, -1, 0, 1][i]
				if 0 <= u < w and 0 <= v < h and (a[v][u] == '#'):
					c += 1
			a[y][x] = c
for i in range(h):
	print(*a[i], sep='')


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()


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


(H, W) = map(int, input().split())
S = ['.' * (W + 2)] + ['.' + input() + '.' for _ in range(H)] + ['.' * (W + 2)]
for hi in range(1, H + 1):
	ans = ''
	for wi in range(1, W + 1):
		if S[hi][wi] == '#':
			ans += '#'
		else:
			now = 0
			for hii in [-1, 0, 1]:
				for wii in [-1, 0, 1]:
					if S[hi + hii][wi + wii] == '#':
						now += 1
			ans += str(now)
	print(ans)


(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 _ 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 = ['-' * (w + 2)]
for i in range(h):
	s += ['-' + input() + '-']
s += ['-' * (w + 2)]
for hi in range(1, h + 1):
	ans = ''
	for wi in range(1, w + 1):
		if s[hi][wi] == '#':
			ans += '#'
		else:
			c = [s[hi - 1][wi - 1], s[hi][wi - 1], s[hi + 1][wi - 1], s[hi - 1][wi], s[hi + 1][wi], s[hi - 1][wi + 1], s[hi][wi + 1], s[hi + 1][wi + 1]]
			ans += str(c.count('#'))
	print(ans)


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


(h, w) = map(int, input().split())
ls = [input() for _ in range(h)]
ll = [['#'] * w for _ in range(h)]
for x in range(h):
	for y in range(w):
		if ls[x][y] == '.':
			cnt = 0
			for (dx, dy) in ((1, 0), (0, 1), (-1, 0), (0, -1), (1, 1), (-1, 1), (-1, -1), (1, -1)):
				(nx, ny) = (x + dx, y + dy)
				if not (0 <= nx < h and 0 <= ny < w):
					continue
				if ls[nx][ny] == '#':
					cnt += 1
			ll[x][y] = cnt
for l in ll:
	print(''.join(map(str, l)))


(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
					elif 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) = 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())
gf = [['.'] + list(input()) + ['.'] for _ in range(h)]
gf = [['.'] * (w + 2)] + gf + [['.'] * (w + 2)]
for i in range(1, h + 1):
	for j in range(1, w + 1):
		if gf[i][j] != '#':
			gf[i][j] = str([gf[i + s][j + t] for s in range(-1, 2) for t in range(-1, 2) if i != 0 or j != 0].count('#'))
for i in range(1, h + 1):
	print(''.join(gf[i][1:w + 1]))


(h, w) = map(int, input().split())
(s, t) = ([list(input()) for i in range(h)], [[0 for j in range(w + 2)] for i in range(h + 2)])
for i in range(h):
	for j in range(w):
		if s[i][j] == '.':
			continue
		for p in range(9):
			t[i + p // 3][j + p % 3] += 1
for i in range(h):
	for j in range(w):
		if s[i][j] == '.':
			s[i][j] = str(t[i + 1][j + 1])
	print(''.join(s[i]))


(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())
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='')


(H, W) = map(int, input().split())
S = [['']] * H
for i in range(H):
	S[i] = input()
dh = [-1, -1, -1, 0, 0, 1, 1, 1]
dw = [-1, 0, 1, -1, 1, -1, 0, 1]
for i in range(H):
	for j in range(W):
		count = 0
		if S[i][j] == '.':
			for l in range(8):
				if H > i + dh[l] > -1 and W > j + dw[l] > -1:
					if S[i + dh[l]][j + dw[l]] == '#':
						count += 1
			S[i] = S[i][:j] + str(count) + S[i][j + 1:]
for i in range(H):
	print(S[i])


(h, w) = map(int, input().split())
S = [[j for j in input()] for i in range(h)]
for i in range(h):
	for j in range(w):
		a = 0
		if S[i][j] == '.':
			for p in range(-1, 2):
				for k in range(-1, 2):
					if 0 <= i + p < h and 0 <= j + k < w:
						if S[i + p][j + k] == '#':
							a += 1
			S[i][j] = str(a)
for i in S:
	print(''.join(i))


(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) = map(int, input().split())
l = [input() for i in range(h)]
cl = [[0] * w for i in range(h)]
for i in range(h):
	for j in range(w):
		if l[i][j] == '#':
			for a in {max(0, i - 1), i, min(h - 1, i + 1)}:
				for b in {max(0, j - 1), j, min(w - 1, j + 1)}:
					cl[a][b] += 1
cl = [[str(cl[i][j]) if l[i][j] != '#' else '#' for j in range(w)] for i in range(h)]
for i in range(h):
	print(''.join(cl[i]))


(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) = map(int, input().split())
S = [input() for _ in range(H)]
T = [''] * H
for (i, l) in enumerate(S):
	for (j, c) in enumerate(l):
		T[i] += str(sum((S[i + p][j + q] == '#' for p in [-1, 0, 1] for q in [-1, 0, 1] if (0 <= i + p < H) & (0 <= j + q < W)))) if c == '.' else c
print('\n'.join(T))


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


(h, w) = map(int, input().split())
m = [[0 for _ in range(w + 2)] for _ in range(h + 2)]
for y in range(h):
	s = input()
	for x in range(w):
		if s[x] == '#':
			for i in [0, 1, 2]:
				for j in [0, 1, 2]:
					m[y + j][x + i] += 1
			m[y + 1][x + 1] = 9
for y in range(1, h + 1):
	ans = ''
	for x in range(1, w + 1):
		if m[y][x] > 8:
			ans += '#'
		else:
			ans += str(m[y][x])
	print(ans)


(n, b) = (int(i) for i in input().split())
l = [input() for i in range(n)]
z = [-1, 0, 1]
for i in range(n):
	for ii in range(b):
		if l[i][ii] == '#':
			print('#', end='')
		else:
			print(sum([l[x + i][xx + ii] == '#' for x in z for xx in z if 0 <= x + i < n and 0 <= xx + ii < b]), end='')
	print('')


(H, W) = map(int, input().split())
S = [input() for _ in range(H)]
for i in range(H):
	T = ''
	for j in range(W):
		if S[i][j] == '#':
			T += '#'
		else:
			T += str(sum([x[max(0, j - 1):min(j + 2, W)].count('#') for x in S[max(0, i - 1):min(i + 2, H)]]))
	print(T)


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 = [['' for i in range(w + 2)] for j in range(h + 2)]
for i in range(h):
	S[i + 1][1:w + 1] = input()
for i in range(h):
	for j in range(w):
		if S[i + 1][j + 1] == '.':
			S[i + 1][j + 1] = str(1 * sum((S[k + i][j:j + 3].count('#') for k in range(3))))
	print(*S[i + 1][1:w + 1], sep='')


(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] == '#':
			continue
		cnt = 0
		for dx in [-1, 0, 1]:
			for dy in [-1, 0, 1]:
				if not (0 <= dx + i <= h - 1 and 0 <= dy + j <= w - 1):
					continue
				if s[i + dx][j + dy] == '#':
					cnt += 1
		s[i][j] = str(cnt)
for m in s:
	print(''.join(m))


(h, w) = map(int, input().split())
s = [['.' for i in range(w + 2)]]
for i in range(h):
	s.append(['.'] + list(input()) + ['.'])
s.append(['.'] * (w + 2))
l = []
for i in range(1, h + 1):
	l2 = []
	for j in range(1, w + 1):
		if s[i][j] == '#':
			l2.append('#')
		else:
			l2.append(str([s[i + 1][j], s[i + 1][j + 1], 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]].count('#')))
	l.append(''.join(l2))
print(*l, sep='\n')


(H, W) = map(int, input().split())
T = [',' * W + ',,'] + [',' + input() + ',' for _ in range(H)] + [',' * W + ',,']
ans = ''
d = [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)]
for i in range(1, H + 1):
	for j in range(1, W + 1):
		if T[i][j] == '#':
			ans += '#'
		else:
			ans += str(sum((T[i + x][j + y] == '#' for (x, y) in d)))
	ans += '\n'
print(ans)


(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='')


(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]))


(h, w) = map(int, input().split())
s = [input() for i in range(h)]
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):
		mine = 0
		for k in range(8):
			if 0 <= i + dy[k] < h and 0 <= j + dx[k] < w and (s[i + dy[k]][j + dx[k]] == '#'):
				mine += 1
		if s[i][j] == '.':
			s[i] = s[i].replace('.', str(mine), 1)
	print(s[i])


(h, w) = map(int, input().split())
s = [list(input()) for i in range(h)]
import itertools
for i in range(h):
	ans = ''
	for j in range(w):
		if s[i][j] == '#':
			ans += '#'
			continue
		temp = 0
		for (dx, dy) in itertools.product([-1, 0, 1], repeat=2):
			if i + dx < 0 or i + dx >= h:
				continue
			if j + dy < 0 or j + dy >= w:
				continue
			if s[i + dx][j + dy] == '#':
				temp += 1
		ans += str(temp)
	print(ans)


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) = map(int, input().split())
s = ['.' * (w + 2)]
for i in range(h):
	s.append('.' + input() + '.')
s.append('.' * (w + 2))
for i in range(1, h + 1):
	p = ''
	for j in range(1, w + 1):
		if s[i][j] == '.':
			x = s[i - 1][j - 1:j + 2] + s[i][j - 1:j + 2] + s[i + 1][j - 1:j + 2]
			p += str(x.count('#'))
		else:
			p += '#'
	print(p)


(h, w) = map(int, input().split())
s = [list(input()) for i in range(h)]
for y in range(h):
	for x in range(w):
		if s[y][x] == '.':
			cnt = 0
			for (dy, dx) in [[0, 1], [0, -1], [1, 0], [-1, 0], [1, 1], [-1, 1], [1, -1], [-1, -1]]:
				ny = y + dy
				nx = x + dx
				if 0 <= ny < h and 0 <= nx < w and (s[ny][nx] == '#'):
					cnt += 1
			s[y][x] = str(cnt)
for i in s:
	print(''.join(i))


(H, W) = map(int, input().split())
M = [input() for x in range(H)]
A = [[0 for x in range(W)] for y in range(H)]
nx = [-1, 0, 1]
ny = [-1, 0, 1]
for i in range(H):
	for j in range(W):
		if M[i][j] == '.':
			cnt = 0
			for x in nx:
				for y in ny:
					if 0 <= i + x < H and 0 <= j + y < W and (M[i + x][j + y] == '#'):
						cnt += 1
			A[i][j] = str(cnt)
		else:
			A[i][j] = '#'
for i in range(H):
	print(''.join(A[i]))


(h, w) = map(int, input().split())
s = [input() for _ in range(h)]
l = [[0] * (w + 2) for _ in range(h + 2)]
for i in range(h):
	for j in range(w):
		if s[i][j] == '#':
			for y in range(i, i + 3):
				for x in range(j, j + 3):
					if l[y][x] != '#':
						l[y][x] += 1
			l[i + 1][j + 1] = '#'
for i in range(1, h + 1):
	print(*l[i][1:w + 1], sep='')


(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('')


from collections import Counter
import math
import statistics
import itertools
(a, b) = map(int, input().split())
g = [list(map(lambda x: '{}'.format(x), list(input()))) for _ in range(a)]
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()))
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())
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))
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]))


(h, w) = map(int, input().split())
s = [input() for i in range(h)]
for i in range(h):
	for j in range(w):
		if s[i][j] == '#':
			print('#', end='')
		else:
			cnt = 0
			for x in range(-1, 2):
				for y in range(-1, 2):
					if 0 <= i + x < h and 0 <= j + y < w and (s[i + x][j + y] == '#'):
						cnt += 1
			print(cnt, end='')
	print()


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


(h, w) = map(int, input().split())
g = [input() for _ in range(h)]
a = [[0] * w for _ in range(h)]
for x in range(h):
	for y in range(w):
		if g[x][y] == '.':
			continue
		a[x][y] = '#'
		for (dx, dy) in [(1, 0), (0, 1), (-1, 0), (0, -1), (1, 1), (-1, 1), (-1, -1), (1, -1)]:
			(nx, ny) = (x + dx, y + dy)
			if 0 <= nx < h and 0 <= ny < w and (g[nx][ny] == '.'):
				a[nx][ny] += 1
for l in a:
	print(*l, sep='')


(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) = 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)


(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())
s = [list('.' + input() + '.') for _ in range(h)]
s = [['.'] * (w + 2)] + s + [['.'] * (w + 2)]
for i in range(1, h + 1):
	l = []
	for k in range(1, w + 1):
		if s[i][k] == '#':
			p = '#'
		else:
			p = 0
			p += s[i - 1][k - 1:k + 2].count('#')
			p += s[i][k - 1:k + 2].count('#')
			p += s[i + 1][k - 1:k + 2].count('#')
		l.append(str(p))
	print(''.join(l))


(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) = 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] == '#':
			continue
		tmp = 0
		for di in range(max(i - 1, 0), min(i + 2, H)):
			for dj in range(max(j - 1, 0), min(j + 2, W)):
				tmp += int(S[di][dj] == '#')
		S[i][j] = str(tmp)
for si in S:
	print(''.join(si))


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())
grid = [''] * h
for i in range(h):
	grid[i] = list(input())
search = [(row, col) for row in range(-1, 2) for col in range(-1, 2) if not (row == 0 and col == 0)]
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)


(h, w) = map(int, input().split())
li = [input() for _ in range(h)]
for i in range(h):
	res = ''
	for j in range(w):
		cnt = 0
		if li[i][j] == '.':
			for x in range(-1, 2):
				for y in range(-1, 2):
					if 0 <= i + x < h and 0 <= j + y < w and (li[i + x][j + y] == '#'):
						cnt += 1
			res += str(cnt)
		else:
			res += '#'
	print(res)


(H, W) = map(int, input().split())
grid = [list('.' + input() + '.') for i in range(H)]
grid.insert(0, list('.' * (W + 2)))
grid.append(list('.' * (W + 2)))
ans = []
for i in range(1, H + 1):
	for j in range(1, W + 1):
		if grid[i][j] == '#':
			ans.append('#')
		else:
			ans.append(str((grid[i - 1][j - 1:j + 2] + grid[i][j - 1:j + 2] + grid[i + 1][j - 1:j + 2]).count('#')))
	ans.append('\n')
print(''.join(ans))


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
				elif 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) = map(int, input().split())
a = ['.' + input() + '.' for _ in range(h)]
a.insert(0, '.' * (w + 2))
a.append('.' * (w + 2))
y = [1, 1, 1, -1, -1, -1, 0, 0]
x = [0, 1, -1, 0, 1, -1, 1, -1]
for i in range(1, h + 1):
	l = []
	for j in range(1, w + 1):
		if a[i][j] == '#':
			l += '#'
		else:
			l += str(sum([1 for k in range(8) if a[i + y[k]][j + x[k]] == '#']))
	print(''.join(l))


(h, w) = map(int, input().split())
x = [list(input()) for _ in range(h)]
y = [[0] * (w + 2) for _ in range(h + 2)]
for i in range(h):
	for j in range(w):
		if x[i][j] == '.':
			y[i + 1][j + 1] = 0
		else:
			y[i + 1][j + 1] = 1
for i in range(h):
	for j in range(w):
		if x[i][j] == '.':
			x[i][j] = str(sum(y[i][j:j + 3]) + sum(y[i + 1][j:j + 3]) + sum(y[i + 2][j:j + 3]))
	print(''.join(x[i]))


(h, w) = map(int, input().split())
s = [list(input()) for i in range(h)]
S2 = [[0] * (w + 2) for i in range(h + 2)]
for i in range(h):
	for j in range(w):
		if s[i][j] == '#':
			S2[i + 1][j + 1] = 1
for i in range(h):
	for j in range(w):
		if s[i][j] == '.':
			s[i][j] = sum(S2[i][j:j + 3]) + sum(S2[i + 1][j:j + 3]) + sum(S2[i + 2][j:j + 3])
	print(''.join(map(str, s[i])))


(H, W) = list(map(int, input().strip().split()))
S = [['.' for _ in range(W + 2)]] + [list('.' + input() + '.') for _ in range(H)] + [['.' for _ in range(W + 2)]]
for y in range(1, H + 1):
	for x in range(1, W + 1):
		if S[y][x] == '.':
			S[y][x] = str(len([1 for i in range(y - 1, y + 2) for j in range(x - 1, x + 2) if S[i][j] == '#']))
S = [s[1:-1] for s in S[1:-1]]
[print(''.join(s)) for s in S]


(H, W) = map(int, input().split())
S = [input() for _ in range(H)]
T = ['' for _ in range(H)]
for h in range(H):
	for w in range(W):
		T[h] += str(sum((S[i][j] == '#' for i in range(max(0, h - 1), min(H, h + 2)) for j in range(max(0, w - 1), min(W, w + 2))))) if S[h][w] == '.' else '#'
print(*T, sep='\n')


(h, w) = map(int, input().split())
S = [list(input()) for i in range(h)]
dx = [-1, 0, 1]
dy = [-1, 0, 1]
for i in range(h):
	for j in range(w):
		if S[i][j] == '#':
			continue
		count = 0
		for x in dx:
			for y in dy:
				if 0 <= i + y < h and 0 <= j + x < w and (S[i + y][j + x] == '#'):
					count += 1
		S[i][j] = str(count)
for s in S:
	print(''.join(s))


(r, c) = map(int, input().split())
d = [list(input()) for i in range(r)]
bom = [(x, y) for (x, i) in enumerate(d) for (y, j) in enumerate(i) if j == '#']
temp = []
for b in bom:
	temp += [(b[0] + x, b[1] + y) for y in [1, 0, -1] for x in [1, 0, -1]]
print('\n'.join([''.join([str(temp.count((i, j))) if d[i][j] != '#' else '#' for j in range(c)]) for i in range(r)]))


(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))


(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)


(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
			elif s[x + dx][y + dy] == '#':
				c += 1
		ans[x][y] = c
for row in ans:
	print(''.join(list(map(str, row))))


s = [int(x) for x in input().split()]
f = ['.' * (s[1] + 2)]
for i in range(s[0]):
	f.append('.' + input() + '.')
f.append('.' * (s[1] + 2))
for i in range(1, s[0] + 1):
	for j in range(1, s[1] + 1):
		if f[i][j] == '#':
			continue
		cnt = 0
		for v in [-1, 0, 1]:
			for h in [-1, 0, 1]:
				if f[i + v][j + h] == '#':
					cnt += 1
		f[i] = f[i][:j] + str(cnt) + f[i][j + 1:]
print('\n'.join([x[1:-1] for x in f[1:-1]]))


(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)


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 i in range(H)]
R = ['' for i in range(H)]
for i in range(H):
	for j in range(W):
		sum = 0
		if i != 0:
			sum += S[i - 1][max(0, j - 1):min(W, j + 2)].count('#')
		sum += S[i][max(0, j - 1):min(W, j + 2)].count('#')
		if i != H - 1:
			sum += S[i + 1][max(0, j - 1):min(W, j + 2)].count('#')
		if S[i][j] == '.':
			R[i] += str(sum)
		else:
			R[i] += '#'
print(*R, sep='\n')


(h, w) = map(int, input().split())
s = [input() for _ in [0] * h]
l = []
for i in range(h):
	for j in range(w):
		if s[i][j] == '#':
			l.extend([[i, j]])
a = ''
for i in range(h):
	for j in range(w):
		if [i, j] in l:
			a += '#'
		else:
			c = 0
			for m in l:
				if abs(i + j - sum(m)) + abs(i - j - m[0] + m[1]) == 2:
					c += 1
			a += str(c)
for i in range(h):
	print(a[w * i:w * (i + 1)])


(h, w) = map(int, input().split())
s = [[j for j in input()] for i in range(h)]
for i in range(h):
	for j in range(w):
		if s[i][j] == '.':
			tmp = 0
			for k in range(max(0, i - 1), min(i + 2, h)):
				tmp += s[k][max(0, j - 1):min(j + 2, w)].count('#')
			s[i][j] = str(tmp)
for s_ in s:
	print(''.join(s_))


(H, W) = map(int, input().split())
l = []
for i in range(H):
	l.append(list(input()))
for h in range(H):
	for w in range(W):
		if l[h][w] == '#':
			continue
		cnt = 0
		for x in range(-1, 2):
			for y in range(-1, 2):
				if h + x >= 0 and w + y >= 0 and (h + x < H) and (w + y < W):
					if l[h + x][w + y] == '#':
						cnt = cnt + 1
		l[h][w] = str(cnt)
for i in l:
	print(''.join(i))


(H, W) = map(int, input().split())
S = ['.' * (W + 2)] + ['.' + input() + '.' for _ in [0] * H] + ['.' * (W + 2)]
cells = [[0] * W for _ in [0] * H]
for i in range(H):
	for j in range(W):
		if S[i + 1][j + 1] == '#':
			cells[i][j] = '#'
		else:
			surrounder = S[i][j:j + 3] + S[i + 1][j:j + 3:2] + S[i + 2][j:j + 3]
			cells[i][j] = surrounder.count('#')
for x in cells:
	print(''.join(map(str, x)))


(H, W) = [int(i) for i in input().split()]
S = []
S.append('.' * (W + 2))
for i in range(H):
	S.append('.' + input() + '.')
S.append(['.'] * (W + 2))
for y in range(1, H + 1):
	for x in range(1, W + 1):
		if S[y][x] == '#':
			print('#', end='')
		else:
			print(S[y - 1][x - 1:x + 2].count('#') + S[y][x - 1:x + 2].count('#') + S[y + 1][x - 1:x + 2].count('#'), end='')
	print('')


(a, b) = map(int, input().split())
c = []
for i in range(a):
	c += [list(input())]
for i in range(a):
	for j in range(b):
		if c[i][j] == '.':
			d = 0
			for k in range(i - 1, i + 2):
				for l in range(j - 1, j + 2):
					if 0 <= k and k < a and (0 <= l) and (l < b):
						d += c[k][l] == '#'
			c[i][j] = str(d)
for i in c:
	print(''.join(i))


(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)


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 = [input() for _ in range(H)]
for i in range(H):
	A = ''
	for j in range(W):
		if S[i][j] == '#':
			A += '#'
		else:
			C = 0
			for SS in S[max(0, i - 1):min(H, i + 2)]:
				C += SS[max(0, j - 1):min(W, j + 2)].count('#')
			A += str(C)
	print(A)


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)]
result = [[0 if v == '.' else '#' for v in row] for row in S]
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())
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()


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

def check(y, x, s):
	ans = 0
	for i in range(3):
		for j in range(3):
			X = x + i - 1
			Y = y + j - 1
			if 0 <= X < w and 0 <= Y < h:
				if s[Y][X] == '#':
					ans += 1
	return ans
for i in range(h):
	for j in range(w):
		if s[i][j] == '.':
			copyS[i][j] = str(check(i, j, s))
for i in copyS:
	print(''.join(i))


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


(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()


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


(h, w) = map(int, input().split())
s = []
for _ in range(h):
	s.append(input())
ans = [[0] * w for _ in range(h)]
for i in range(h):
	for j in range(w):
		if s[i][j] == '#':
			ans[i][j] = '#'
			continue
		i0 = max(0, i - 1)
		i1 = min(i + 1, h - 1)
		j0 = max(0, j - 1)
		j1 = min(j + 1, w - 1)
		p = 0
		for _ in range(i0, i1 + 1):
			p += s[_][j0:j1 + 1].count('#')
		ans[i][j] = p
for ansi in ans:
	print(''.join(map(str, ansi)))


(h, w) = map(int, input().split())
S = []
for _ in range(h):
	S.append(input())
for i in range(h):
	ss = ''
	for j in range(w):
		if S[i][j] == '#':
			ss += '#'
		else:
			counter = 0
			for k in [-1, 0, 1]:
				for l in [-1, 0, 1]:
					if 0 <= i + k <= h - 1 and 0 <= j + l <= w - 1 and (S[i + k][j + l] == '#'):
						counter += 1
			ss += str(counter)
	print(ss)


(h, w) = map(int, input().split())
s = list((input() for _ in range(h)))
dx = [0, 1, 0, -1, 1, 1, -1, -1]
dy = [1, 0, -1, 0, 1, -1, 1, -1]
for i in range(h):
	for j in range(w):
		if s[i][j] == '.':
			c = 0
			for r in range(8):
				x = i + dx[r]
				y = j + dy[r]
				c += 0 <= x < h and 0 <= y < w and (s[x][y] == '#')
			print(c, end='')
		else:
			print('#', end='')
	print()


(h, w) = map(int, input().split())
m = [list(input()) for _ in range(h)]
ans = [[0] * w for _ in range(h)]
for i in range(h):
	for j in range(w):
		if m[i][j] == '#':
			ans[i][j] = '#'
		else:
			for k in range(max(0, i - 1), min(h, i + 2)):
				ans[i][j] += m[k][max(0, j - 1):min(w, j + 2)].count('#')
[print(*l, sep='') for l in ans]


(h, w) = map(int, input().split())
s = [list(input()) for _ in range(h)]
p = [-1, 0, 1]
for i in range(h):
	for j in range(w):
		count = 0
		for ph in p:
			for pw in p:
				if ph == 0 and pw == 0 or not 0 <= i - ph <= h - 1 or (not 0 <= j - pw <= w - 1):
					continue
				if s[max(i - ph, 0)][max(j - pw, 0)] == '#':
					count += 1
		if s[i][j] == '.':
			s[i][j] = str(count)
for si in s:
	print(''.join(si))


from itertools import product
(H, W) = list(map(int, input().split()))
S = [[0] * (W + 2)] + [[0] + list(input()) + [0] for _ in range(H)] + [[0] * (W + 2)]
D = list(product([-1, 0, 1], repeat=2))
for (h, w) in product(range(1, H + 1), range(1, W + 1)):
	if S[h][w] != '.':
		continue
	n = 0
	for (dx, dy) in D:
		if S[h + dy][w + dx] == '#':
			n += 1
	S[h][w] = str(n)
for s in S[1:-1]:
	print(''.join(s[1:-1]))


(h, w) = (int(i) for i in input().split())
s = [[j for j in input()] for i in range(h)]
for i in range(h):
	for j in range(w):
		cnt = 0
		if s[i][j] == '#':
			continue
		for k in range(-1, 2):
			for l in range(-1, 2):
				if 0 <= i + k <= h - 1 and 0 <= j + l <= w - 1 and (s[i + k][j + l] == '#'):
					cnt += 1
		s[i][j] = str(cnt)
for i in s:
	print(*i, sep='')


(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) = 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 = [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())
S = [list(input()) for _ in range(H)]
diff = [-1, 0, 1]
for i in range(H):
	for j in range(W):
		count = 0
		for x in diff:
			for y in diff:
				if 0 <= i + x < H and 0 <= j + y < W and (S[i + x][j + y] == '#'):
					count += 1
		if S[i][j] == '.':
			S[i][j] = count
for s in S:
	print(''.join(map(str, s)))


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 = [(2 + W) * '.'] + ['.' + input() + '.' for h in range(H)] + [(2 + W) * '.']
for h in range(1, 1 + H):
	for w in range(1, 1 + W):
		if S[h][w] != '#':
			s = S[h - 1][w - 1:w + 2] + S[h][w - 1] + S[h][w + 1] + S[h + 1][w - 1:w + 2]
			S[h] = S[h][:w] + str(s.count('#')) + S[h][w + 1:]
for h in range(1, 1 + H):
	print(S[h][1:1 + W])


(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])

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 = ['.' * (W + 2)] + ['.' + input() + '.' for _ in range(H)] + ['.' * (W + 2)]
for x in range(1, H + 1):
	for y in range(1, W + 1):
		if S[x][y] == '.':
			Bomb = (S[x - 1][y - 1:y + 2] + S[x][y - 1:y + 2] + S[x + 1][y - 1:y + 2]).count('#')
			S[x] = S[x][:y] + str(Bomb) + S[x][y + 1:]
for i in range(1, H + 1):
	print(S[i][1:W + 1])


(h, w) = map(int, input().split())
li = [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 li[i][j] == '#':
			ans[i][j] = '#'
			for k in range(-1, 2):
				for l in range(-1, 2):
					if 0 <= i + k < h and 0 <= j + l < w:
						if ans[i + k][j + l] != '#':
							ans[i + k][j + l] += 1
anss = [map(str, ans[i]) for i in range(h)]
for i in range(h):
	print(''.join(anss[i]))


(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) = map(int, input().split())
s = [['.'] * (W + 2) for i in range(H + 2)]
for i in range(H):
	S = input()
	t = '.' + S + '.'
	s[i + 1] = t
for i in range(H):
	for j in range(W):
		b = 0
		if s[i + 1][j + 1] != '#':
			for k in range(-1, 2, 1):
				for l in range(-1, 2, 1):
					if s[i + 1 + k][j + 1 + l] == '#':
						b += 1
		else:
			b = '#'
		print(b, end='')
	print()


(a, b) = map(int, input().split())
s = [input() for i in range(a)]
for i in range(a):
	for j in range(b):
		c = 0
		if s[i][j] == '.':
			p = s[i]
			s[i] = p[:j] + str(sum([t[max(0, j - 1):min(b, j + 2)].count('#') for t in s[max(0, i - 1):min(a, i + 2)]])) + p[j + 1:]
for i in range(a):
	print(s[i])


(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]
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())
S = ['.' + input() + '.' if i > 0 and i < H + 1 else '.' * (W + 2) for i in range(H + 2)]
for i in range(1, H + 1):
	s = ''
	for j in range(1, W + 1):
		if S[i][j] == '#':
			s += '#'
		else:
			s += str((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]).count('#'))
	print(s)


(h, w) = map(int, input().split())
S = [['.'] + list(input()) + ['.'] for i in range(h)]
S = [['.'] * (w + 2)] + S + [['.'] * (w + 2)]
for i in range(1, h + 1):
	for j in range(1, w + 1):
		if S[i][j] == '.':
			S[i][j] = str([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('#'))
for k in range(len(S[1:h + 1])):
	print(*S[1:h + 1][k][1:w + 1], sep='')


(h, w) = (int(i) for i in input().split())
x = [input() for i in range(h)]
for i in range(h):
	answer = ''
	for j in range(w):
		if x[i][j] == '.':
			(num, y) = (0, [[i - 1, j - 1], [i - 1, j], [i - 1, j + 1], [i, j - 1], [i, j + 1], [i + 1, j - 1], [i + 1, j], [i + 1, j + 1]])
			for k in y:
				if -1 < k[0] and k[0] < h and (-1 < k[1]) and (k[1] < w):
					if x[k[0]][k[1]] == '#':
						num += 1
			answer += str(num)
		else:
			answer += '#'
	print(answer)


(h, w) = map(int, input().split())
T = ['.' + input() + '.' for i in range(h)]
S = '.' * (w + 2)
T.insert(0, S)
T.append(S)
Ans = ['' for i in range(h)]
for i in range(1, h + 1):
	for j in range(1, w + 1):
		count = 0
		if T[i][j] == '#':
			A = '#'
		elif T[i][j] == '.':
			for m in range(3):
				for n in range(3):
					if T[i - 1 + m][j - 1 + n] == '#':
						count += 1
					A = str(count)
		Ans[i - 1] += A
print(*Ans, sep='\n')


(H, W) = map(int, input().split())
S = ['.' * (W + 2)] + ['.' + input() + '.' for i in range(H)] + ['.' * (W + 2)]
for i in range(H):
	for j in range(W):
		c = S[i + 1][j + 1]
		if c == '.':
			c = sum([S[k][j:j + 3].count('#') for k in range(i, i + 3)])
		print(c, sep='', end='')
	print('')


(h, w) = map(int, input().split())
s = ['-' * (w + 2)]
for i in range(h):
	s.append('-' + input() + '-')
s.append('-' * (w + 2))
for j in range(1, h + 1):
	t = []
	for k in range(1, w + 1):
		if s[j][k] == '#':
			t.append('#')
		else:
			c = [s[j - 1][k - 1], s[j][k - 1], s[j + 1][k - 1], s[j - 1][k], s[j + 1][k], s[j - 1][k + 1], s[j][k + 1], s[j + 1][k + 1]]
			t.append(str(c.count('#')))
	t = ''.join(t)
	print(t)


(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) = 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) = (int(i) for i in input().split())
d = {}
nei = [-101, -100, -99, -1, 1, 99, 100, 101]
for i in range(h):
	s = input()
	for j in range(w):
		d[(i + 1) * 100 + j + 1] = s[j]
for k in d:
	if d[k] == '.':
		cnt = 0
		for l in nei:
			if k + l in d:
				if d[k + l] == '#':
					cnt += 1
		d[k] = cnt
for i in range(h):
	ans = ''
	for j in range(w):
		ans += str(d[(i + 1) * 100 + j + 1])
	print(ans)


(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) = 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])


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) = map(int, input().split())
S = [list(input()) for i in range(H)]
ans = [[0] * (W + 2) for i in range(H + 2)]
mine = []
for i in range(H):
	for j in range(W):
		if S[i][j] != '#':
			continue
		mine.append((i, j))
		for v in range(3):
			for h in range(3):
				ans[i + v][j + h] += 1
for (i, j) in mine:
	ans[i + 1][j + 1] = '#'
for l in ans[1:H + 1]:
	print(''.join(map(str, l[1:W + 1])))


import itertools
(H, W) = map(int, input().strip().split(' '))
S = [list('.' * (W + 2))]
S += [list('.' + input().strip() + '.') for _ in range(H)]
S.append(list('.' * (W + 2)))
for (x, y) in itertools.product(range(1, W + 1), range(1, H + 1)):
	if S[y][x] == '#':
		continue
	S[y][x] = sum([S[y + dy][x + dx] == '#' for (dx, dy) in itertools.product([-1, 0, 1], repeat=2)])
for y in range(1, H + 1):
	print(*S[y][1:-1], sep='')


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())
S = ['.'] * (W + 2)
S = [['.'] * (W + 2)] + [list('.' + input() + '.') for _ in range(H)] + [['.'] * (W + 2)]
for i in range(1, H + 1):
	for j in range(1, W + 1):
		if S[i][j] == '#':
			print('#', end='')
		else:
			print((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]).count('#'), end='')
	print()


(h, w) = map(int, input().split())
s = [list(input()) for i in range(h)]
dx = [-1, 0, 1]
dy = [-1, 0, 1]
for i in range(h):
	for j in range(w):
		count = 0
		if s[i][j] == '#':
			continue
		if s[i][j] == '.':
			for l in dx:
				for m in dy:
					if i + l < 0 or i + l > h - 1 or j + m < 0 or (j + m > w - 1):
						continue
					if s[i + l][j + m] == '#':
						count += 1
		s[i][j] = str(count)
for i in s:
	k = ''.join(i)
	print(k)


(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] == '.':
			x = [(i + 1, j + 1), (i, j + 1), (i - 1, j + 1), (i + 1, j), (i - 1, j), (i + 1, j - 1), (i, j - 1), (i - 1, j - 1)]
			x = [(p, q) for (p, q) in x if min(p, q) >= 0 and p < h and (q < w)]
			y = 0
			for (p, q) in x:
				if s[p][q] == '#':
					y += 1
			s[i][j] = str(y)
for i in range(h):
	print(''.join(s[i]))


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


(H, W) = map(int, input().split())
mine_map = [list(input()) for x in range(H)]
for i in range(H):
	for j in range(W):
		count = 0
		if mine_map[i][j] == '.':
			for k in range(-1, 2):
				for l in range(-1, 2):
					if i + k >= 0 and i + k < H and (j + l >= 0) and (j + l < W):
						if mine_map[i + k][j + l] == '#':
							count += 1
			mine_map[i][j] = count
		print(mine_map[i][j], end='')
	print()


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

def __starting_point():
	solve()
__starting_point()


(H, W) = map(int, input().split())
s0 = '.' * (W + 2)
s1 = '.' + input() + '.'
ans = ''
for i in range(H):
	if i == H - 1:
		s2 = '.' * (W + 2)
	else:
		s2 = '.' + input() + '.'
	for j in range(1, W + 1):
		if s1[j] == '.':
			x = s0[j - 1:j + 2] + s1[j - 1] + s1[j + 1] + s2[j - 1:j + 2]
			ans += str(x.count('#'))
		else:
			ans += '#'
	s0 = s1
	s1 = s2
for i in range(H):
	print(ans[i * W:i * W + W])


(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))


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


(H, W) = map(int, input().split(' '))
S = [['x'] * (W + 2)]
for i in range(H):
	S.append(['x'] + list(input()) + ['x'])
S.append(['x'] * (W + 2))
for h in range(H + 2):
	for w in range(W + 2):
		if S[h][w] == '.':
			S[h][w] = str([S[x][y] for x in [h - 1, h, h + 1] for y in [w - 1, w, w + 1]].count('#'))
for i in range(H):
	print(''.join(S[i + 1]).replace('x', ''))


(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())
S = [list(input()) for _ in range(H)]
from itertools import product

def num(i, j):
	c = 0
	for (p, q) in product([-1, 0, 1], repeat=2):
		if 0 <= i + p < H and 0 <= j + q < W and (S[i + p][j + q] == '#'):
			c += 1
	return c
for i in range(H):
	for j in range(W):
		if S[i][j] == '.':
			S[i][j] = str(num(i, j))
	print(''.join(S[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) = map(int, input().split())
S = [input() for i in range(H)]
L = [''] * H
for i in range(H):
	for j in range(W):
		if S[i][j] == '#':
			L[i] += '#'
		else:
			c = sum((t[max(0, j - 1):min(W, j + 2)].count('#') for t in S[max(0, i - 1):min(H, i + 2)]))
			L[i] += str(c)
for i in range(H):
	print(L[i])


(H, W) = map(int, input().split())
L = [[0 for i in range(W)] for j in range(H)]
for i in range(H):
	L[i] = list(input())
for a in range(H):
	for b in range(W):
		if L[a][b] == '.':
			num = 0
			for k in range(-1, 2):
				for l in range(-1, 2):
					if 0 <= a + k <= H - 1 and 0 <= b + l <= W - 1 and (L[a + k][b + l] == '#'):
						num += 1
			L[a][b] = num
	print(*L[a], sep='')


(h, w) = map(int, input().split())
sl = [list(input()) for _ in range(h)]
ans = [[0] * (w + 2) for _ in range(h + 2)]
for i in range(h):
	for j in range(w):
		row = i + 1
		col = j + 1
		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='')


(H, W) = map(int, input().split())
S = [input() for i in range(H)]
for h in range(H):
	print(''.join(['#' if S[h][w] == '#' else str(sum([s[max(0, w - 1):min(W, w + 2)].count('#') for s in S[max(0, h - 1):min(H, h + 2)]])) for w in range(W)]))


(h, w) = map(int, input().split())
s = [input() for _ in range(h)]
dx = [1, 1, 1, -1, -1, -1, 0, 0]
dy = [0, 1, -1, 0, 1, -1, 1, -1]
for i in range(h):
	z = []
	for j in range(w):
		if s[i][j] == '.':
			cnt = 0
			for k in range(8):
				x = i + dx[k]
				y = j + dy[k]
				if x >= 0 and x < h and (y >= 0) and (y < w) and (s[x][y] == '#'):
					cnt += 1
			z.append(cnt)
		else:
			z.append('#')
	print(*z, sep='')


(row, column) = map(int, input().split())
S = [list(input()) for i in range(row)]
for i in range(row):
	for j in range(column):
		if S[i][j] == '#':
			continue
		cnt = 0
		for dx in [-1, 0, 1]:
			for dy in [-1, 0, 1]:
				if not 0 <= i + dy < row or not 0 <= j + dx < column:
					continue
				if S[i + dy][j + dx] == '#':
					cnt += 1
		S[i][j] = str(cnt)
for s in S:
	print(''.join(s))


(h, w) = map(int, input().split())
S = [input() for _ in range(h)]
for r in range(h):
	for c in range(w):
		count = 0
		if S[r][c] == '#':
			print('#', end='')
		else:
			for x in S[max(0, r - 1):min(r + 2, h)]:
				count += x[max(0, c - 1):min(c + 2, w)].count('#')
			print(count, end='')
	print()


(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))