One day Alex decided to remember childhood when computers were not too powerful and lots of people played only default games. Alex enjoyed playing Minesweeper that time. He imagined that he saved world from bombs planted by terrorists, but he rarely won.

Alex has grown up since then, so he easily wins the most difficult levels. This quickly bored him, and he thought: what if the computer gave him invalid fields in the childhood and Alex could not win because of it?

He needs your help to check it.

A Minesweeper field is a rectangle $n \times m$, where each cell is either empty, or contains a digit from $1$ to $8$, or a bomb. The field is valid if for each cell:   if there is a digit $k$ in the cell, then exactly $k$ neighboring cells have bombs.  if the cell is empty, then all neighboring cells have no bombs. 

Two cells are neighbors if they have a common side or a corner (i. e. a cell has at most $8$ neighboring cells).


-----Input-----

The first line contains two integers $n$ and $m$ ($1 \le n, m \le 100$) — the sizes of the field.

The next $n$ lines contain the description of the field. Each line contains $m$ characters, each of them is "." (if this cell is empty), "*" (if there is bomb in this cell), or a digit from $1$ to $8$, inclusive.


-----Output-----

Print "YES", if the field is valid and "NO" otherwise.

You can choose the case (lower or upper) for each letter arbitrarily.


-----Examples-----
Input
3 3
111
1*1
111

Output
YES
Input
2 4
*.*.
1211

Output
NO


-----Note-----

In the second example the answer is "NO" because, if the positions of the bombs are preserved, the first line of the field should be *2*1.

You can read more about Minesweeper in Wikipedia's article.

Difficulty: EASY
Tags: Implementation

Starter code:

Solutions:
def pos(x, num):
	if x < 0:
		return -1
	elif x >= num:
		return -1
	return x

def check(x, y, ai, n, m):
	num = 0
	for i in range(-1, 2):
		for j in range(-1, 2):
			if i == 0 and j == 0:
				continue
			if pos(x + i, n) == -1 or pos(y + j, m) == -1:
				continue
			if ai[pos(x + i, n)][pos(y + j, m)] == '*':
				num += 1
	return num
(n, m) = map(int, input().split())
ai = [input() for i in range(n)]
ans = 1
for i in range(n):
	for j in range(m):
		if ai[i][j] == '*':
			continue
		temp = 0
		if ai[i][j] != '.':
			temp = int(ai[i][j])
		if temp != check(i, j, ai, n, m):
			ans = 0
if ans == 0:
	print('NO')
else:
	print('YES')


(n, m) = list(map(int, input().split()))
arr = [[0 for i in range(m)] for j in range(n)]
fl = 0
for i in range(n):
	mass = input()
	for j in range(m):
		if mass[j] == '.':
			arr[i][j] = 0
		elif mass[j] == '*':
			arr[i][j] = -1
		else:
			arr[i][j] = int(mass[j])
for i in range(n):
	for j in range(m):
		if arr[i][j] != -1:
			c = 0
			for x in range(-1, 2):
				for y in range(-1, 2):
					if i + x >= 0 and i + x < n and (j + y >= 0) and (j + y < m):
						if arr[i + x][j + y] == -1:
							c += 1
			if c != arr[i][j]:
				fl = 1
if fl == 0:
	print('YES')
else:
	print('NO')


import sys
(n, m) = map(int, input().split())
l1 = [list(input()) for i in range(n)]
l2 = [[0] * m for i in range(n)]
dy = [1, 1, 1, 0, -1, -1, -1, 0]
dx = [-1, 0, 1, 1, 1, 0, -1, -1]
for i in range(n):
	for j in range(m):
		for q in range(8):
			if 0 <= i + dy[q] <= n - 1 and 0 <= j + dx[q] <= m - 1 and (l1[i + dy[q]][j + dx[q]] == '*'):
				l2[i][j] += 1
for i in range(n):
	for j in range(m):
		if l1[i][j] != '*' and l1[i][j] != '.' and (int(l1[i][j]) != l2[i][j]):
			print('NO')
			return
for i in range(n):
	for j in range(m):
		if l1[i][j] == '.' and l2[i][j] != 0:
			print('NO')
			return
print('YES')


(n, m) = map(int, input().split())
s = [[' '] * m for i in range(n)]
for i in range(n):
	s[i] = input()
dx = [1, -1, 0, 0, 1, 1, -1, -1]
dy = [0, 0, 1, -1, 1, -1, 1, -1]

def inside(r, c):
	if r >= 0 and c >= 0 and (r < n) and (c < m):
		return True
	else:
		return False
for i in range(n):
	for j in range(m):
		if s[i][j] == '.':
			for k in range(8):
				x = i + dx[k]
				y = j + dy[k]
				if inside(x, y) == True and s[x][y] == '*':
					print('NO')
					exit(0)
		elif s[i][j] != '*':
			cnt = 0
			for k in range(8):
				x = i + dx[k]
				y = j + dy[k]
				if inside(x, y) == True and s[x][y] == '*':
					cnt += 1
			if cnt != ord(s[i][j]) - 48:
				print('NO')
				exit(0)
print('YES')


def check(board):
	(rows, cols) = (len(board), len(board[0]))
	duv = [(0, -1), (-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1)]
	for (i, row) in enumerate(board):
		for (j, x) in enumerate(row):
			if '0' <= x <= '8':
				cnt = 0
				for (du, dv) in duv:
					(u, v) = (i + du, j + dv)
					if 0 <= u < rows and 0 <= v < cols and (board[u][v] == '*'):
						cnt += 1
				if int(x) != cnt:
					return False
	return True

def main():
	(rows, cols) = map(int, input().split())
	board = []
	for _ in range(rows):
		line = input().strip()
		board.append(['0' if c == '.' else c for c in line])
	if check(board):
		print('YES')
	else:
		print('NO')

def __starting_point():
	main()
__starting_point()


(n, m) = map(int, input().split())
a = []
for _ in range(n):
	a.append(list(map(str, input().strip())))
k = 0
for i in range(n):
	for j in range(m):
		p = a[i][j]
		if p == '.':
			if i - 1 >= 0:
				if j - 1 >= 0 and a[i - 1][j - 1] == '*':
					k = 1
				if j + 1 < m and a[i - 1][j + 1] == '*':
					k = 1
				if a[i - 1][j] == '*':
					k = 1
			if j - 1 >= 0 and a[i][j - 1] == '*':
				k = 1
			if j + 1 < m and a[i][j + 1] == '*':
				k = 1
			if i + 1 < n:
				if j - 1 >= 0 and a[i + 1][j - 1] == '*':
					k = 1
				if j + 1 < m and a[i + 1][j + 1] == '*':
					k = 1
				if a[i + 1][j] == '*':
					k = 1
		if ord(p) >= ord('1') and ord(p) <= ord('8'):
			x = ord(p) - ord('0')
			c = 0
			if i - 1 >= 0:
				if j - 1 >= 0 and a[i - 1][j - 1] == '*':
					c += 1
				if j + 1 < m and a[i - 1][j + 1] == '*':
					c += 1
				if a[i - 1][j] == '*':
					c += 1
			if j - 1 >= 0 and a[i][j - 1] == '*':
				c += 1
			if j + 1 < m and a[i][j + 1] == '*':
				c += 1
			if i + 1 < n:
				if j - 1 >= 0 and a[i + 1][j - 1] == '*':
					c += 1
				if j + 1 < m and a[i + 1][j + 1] == '*':
					c += 1
				if a[i + 1][j] == '*':
					c += 1
			if c != x:
				k = 1
		if k == 1:
			break
	if k == 1:
		break
if k == 0:
	print('YES')
else:
	print('NO')


q = [[1, 1], [1, 0], [0, 1], [-1, 1], [1, -1], [-1, -1], [-1, 0], [0, -1]]

def check(chx, chy):
	c = 0
	for i in q:
		(x, y) = (chx + i[0], chy + i[1])
		if x < n and x >= 0 and (y < m) and (y >= 0):
			if a[x][y] == '*':
				c += 1
	return c
(n, m) = map(int, input().split())
a = [input() for i in range(n)]
for i in range(n):
	for j in range(m):
		if a[i][j] == '*':
			continue
		elif a[i][j] == '.':
			if check(i, j) == 0:
				continue
			else:
				print('NO')
				exit()
		elif check(i, j) == int(a[i][j]):
			continue
		else:
			print('NO')
			exit()
print('YES')


l1 = [-1, 0, 1, 1, 1, 0, -1, -1]
l2 = [-1, -1, -1, 0, 1, 1, 1, 0]
(n, m) = input().split()
n = int(n)
m = int(m)
b = (m + 2) * ['.']
a = [b]
for i in range(n):
	a.append(['.'] + list(input()) + ['.'])
a.append(b)
valid = True
for i in range(1, n + 1):
	for j in range(1, m + 1):
		k = 0
		if a[i][j] == '*':
			pass
		else:
			if a[i][j] == '.':
				k = 0
			else:
				k = ord(a[i][j]) - ord('0')
			count = 0
			for ii in range(8):
				if a[i + l1[ii]][j + l2[ii]] == '*':
					count += 1
			if count != k:
				valid = False
print('YES' if valid else 'NO')


(n, m) = [int(s) for s in input().split()]
f = []
for i in range(n):
	f.append(input())

def checkcell(x, y):
	k = 0
	for i in range(-1, 2):
		for j in range(-1, 2):
			if i == 0 and j == 0:
				continue
			if x + i >= 0 and x + i < n and (y + j >= 0) and (y + j < m):
				if f[x + i][y + j] == '*':
					k += 1
	return k

def solve():
	for i in range(n):
		for j in range(m):
			if f[i][j] != '*':
				k = checkcell(i, j)
				num = 0 if f[i][j] == '.' else int(f[i][j])
				if k != num:
					return 'NO'
	return 'YES'
print(solve())


(n, m) = map(int, input().strip().split())
a = [list(input().strip()) for _ in range(n)]
(YES, NO) = ('YES', 'NO')
for i in range(n):
	for j in range(m):
		if a[i][j] == '*':
			a[i][j] = -1
		elif a[i][j] == '.':
			a[i][j] = 0
		else:
			a[i][j] = ord(a[i][j]) - ord('0')

def valid(r, c):
	if r >= 0 and r < n and (c >= 0) and (c < m):
		return 1 if a[r][c] == -1 else 0
	return 0
for i in range(n):
	for j in range(m):
		if a[i][j] == -1:
			continue
		cnt = 0
		for k in range(-1, 2):
			for l in range(-1, 2):
				if k != 0 or l != 0:
					cnt += valid(k + i, l + j)
		if cnt != a[i][j]:
			print(NO)
			quit()
print(YES)


(n, m) = map(int, input().split())
arr = [[0 for _ in range(m + 2)]] + [[] for _ in range(n)] + [[0 for _ in range(m + 2)]]
for i in range(1, n + 1):
	s = input()
	arr[i].append(0)
	for j in s:
		if j == '*':
			arr[i].append(-1)
		elif j == '.':
			arr[i].append(0)
		else:
			arr[i].append(int(j))
	arr[i].append(0)
flag = True
for i in range(1, n + 1):
	for j in range(1, m + 1):
		if arr[i][j] != -1 and (arr[i - 1][j - 1] == -1) + (arr[i - 1][j] == -1) + (arr[i - 1][j + 1] == -1) + (arr[i][j - 1] == -1) + (arr[i][j + 1] == -1) + (arr[i + 1][j - 1] == -1) + (arr[i + 1][j] == -1) + (arr[i + 1][j + 1] == -1) != arr[i][j]:
			flag = False
			break
	if not flag:
		break
if flag:
	print('YES')
else:
	print('NO')


import sys
import io
stream_enable = 0
inpstream = '\n2 4\n*.*.\n1211\n'
if stream_enable:
	sys.stdin = io.StringIO(inpstream)
	input()

def inpmap():
	return list(map(int, input().split()))
(n, m) = inpmap()
arr = []
for i in range(n):
	arr.append(list(input()))
for i in range(n):
	for j in range(m):
		if arr[i][j] == '*':
			continue
		bomb = 0
		for (x, y) in ((-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 1), (1, 0)):
			(ii, jj) = (i + x, j + y)
			if 0 <= ii < n and 0 <= jj < m:
				bomb += arr[ii][jj] == '*'
		if bomb == 0 and arr[i][j] == '.' or (arr[i][j].isdigit() and int(arr[i][j]) == bomb):
			pass
		else:
			print('NO')
			return
print('YES')


(n, m) = map(int, input().split())
f = [[0] * (m + 1) for _ in range(n + 1)]
(l, r) = ([input() for _ in range(n)], 'YES')
for (y, s) in enumerate(l):
	for (x, c) in enumerate(s):
		if c == '*':
			for i in range(y - 1, y + 2):
				for j in range(x - 1, x + 2):
					f[i][j] += 1
for (y, s) in enumerate(l):
	for (x, c) in enumerate(s.replace('.', '0')):
		if c.isdigit() and int(c) != f[y][x]:
			r = 'NO'
print(r)


def bomb(arr, i, j):
	if arr[i][j] == '*':
		return True
	else:
		return False

def count(arr, i, j, n, m):
	c = 0
	if i > 0:
		if bomb(arr, i - 1, j):
			c += 1
		if j > 0 and bomb(arr, i - 1, j - 1):
			c += 1
		if j < m - 1 and bomb(arr, i - 1, j + 1):
			c += 1
	if i < n - 1:
		if bomb(arr, i + 1, j):
			c += 1
		if j > 0 and bomb(arr, i + 1, j - 1):
			c += 1
		if j < m - 1 and bomb(arr, i + 1, j + 1):
			c += 1
	if j > 0 and bomb(arr, i, j - 1):
		c += 1
	if j < m - 1 and bomb(arr, i, j + 1):
		c += 1
	return c
(n, m) = map(int, input().split())
arr = []
for i in range(n):
	arr.append(list(input().strip()))
for i in range(n):
	for j in range(m):
		if arr[i][j] == '.':
			arr[i][j] = '0'
flag = 1
for i in range(n):
	for j in range(m):
		if arr[i][j] != '*' and count(arr, i, j, n, m) != int(arr[i][j]):
			flag = 0
			break
	if flag == 0:
		break
if flag == 1:
	print('YES')
else:
	print('NO')


import sys
import string
from collections import Counter, defaultdict
from math import fsum, sqrt, gcd, ceil, factorial
from itertools import combinations, permutations
flush = lambda : sys.stdout.flush
comb = lambda x, y: factorial(x) // factorial(y) // factorial(x - y)
ip = lambda : input()
ii = lambda : int(input())
r = lambda : map(int, input().split())
rr = lambda : list(r())
(a, b) = r()
arr = [list(ip().replace('.', '0')) for i in range(a)]
for i in range(a):
	for j in range(b):
		if arr[i][j] == '*':
			continue
		c = 0
		for x in range(i - 1, i + 2):
			for y in range(j - 1, j + 2):
				if x >= 0 and x < a and (y >= 0) and (y < b) and (arr[x][y] == '*'):
					c += 1
		val = int(arr[i][j])
		if val != c:
			print('NO')
			exit()
print('YES')


def fun(c, x, y, l, n, m):
	if c == '.':
		for k in range(max(0, y - 1), min(m, y + 2)):
			for b in range(max(0, x - 1), min(x + 2, n)):
				if l[b][k] == '*':
					return False
		return True
	elif c == '*':
		for k in range(max(0, y - 1), min(m, y + 2)):
			for b in range(max(0, x - 1), min(x + 2, n)):
				if l[b][k] == '.':
					return False
		return True
	else:
		s = int(c)
		d = 0
		for k in range(max(0, y - 1), min(m, y + 2)):
			for b in range(max(0, x - 1), min(x + 2, n)):
				if l[b][k] == '*':
					d += 1
		return d == s
(n, m) = map(int, input().split())
l = []
con = True
for i in range(n):
	l.append(input())
for i in range(n):
	for j in range(m):
		con = con and fun(l[i][j], i, j, l, n, m)
		if con == False:
			break
if con:
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().split())
a = []
b = '#' * (1 + m + 1)
a.append(b)
d = ['1', '2', '3', '4', '5', '6', '7', '8']
for i in range(n):
	c = '#' + input().strip() + '#'
	a.append(c)
a.append(b)
ans = 'YES'
for i in range(1, n + 1):
	for j in range(1, m + 1):
		if a[i][j] in d:
			s = 0
			for (dx, dy) in zip([1, 1, 1, 0, 0, -1, -1, -1], [-1, 0, 1, 1, -1, 0, 1, -1]):
				if a[i + dx][j + dy] == '*':
					s += 1
			if s != int(a[i][j]):
				ans = 'NO'
		elif a[i][j] == '.':
			for (dx, dy) in zip([1, 1, 1, 0, 0, -1, -1, -1], [-1, 0, 1, 1, -1, 0, 1, -1]):
				if a[i + dx][j + dy] == '*':
					ans = 'NO'
print(ans)


(n, m) = map(int, input().split())
a = []
for i in range(n):
	a.append(list(input()))
l = 0
b = [(0, 1), (1, 0), (0, -1), (-1, 0), (-1, 1), (1, -1), (1, 1), (-1, -1)]
for i in range(len(a)):
	for j in range(len(a[i])):
		if a[i][j] != '*':
			x = 0
			for k in range(len(b)):
				vx = i + b[k][0]
				vy = j + b[k][1]
				if vx >= 0 and vx <= n - 1 and (vy >= 0) and (vy <= m - 1):
					if a[vx][vy] == '*':
						x += 1
			if a[i][j] != str(x):
				if a[i][j] == '.' and x == 0:
					s = 0
				else:
					l = 1
if l == 1:
	print('no')
else:
	print('yes')


ok = lambda tx, ty: tx >= 0 and tx < n and (ty >= 0) and (ty < m)

def check(mine):
	for i in range(n):
		for j in range(m):
			dot = 0
			star = 0
			for k in range(8):
				tx = i + dx[k]
				ty = j + dy[k]
				if ok(tx, ty):
					if mine[tx][ty] == '.':
						dot += 1
					elif mine[tx][ty] == '*':
						star += 1
			if mine[i][j] == '*':
				if dot > 0:
					print('NO')
					return 0
			elif mine[i][j] == '.':
				if star > 0:
					print('NO')
					return 0
			elif int(mine[i][j]) != star:
				print('NO')
				return 0
	print('YES')
dx = (1, -1, 0, 0, 1, -1, 1, -1)
dy = (0, 0, 1, -1, 1, -1, -1, 1)
(n, m) = map(int, input().split(' '))
mine = []
for i in range(n):
	mine.append(input())
check(mine)


(n, m) = map(int, input().split())
a = []
for i in range(n):
	s = input()
	a.append(s)
f = 1
for i in range(n):
	for j in range(m):
		su = 0
		if i - 1 >= 0 and j - 1 >= 0 and (a[i - 1][j - 1] == '*'):
			su += 1
		if i - 1 >= 0 and a[i - 1][j] == '*':
			su += 1
		if i - 1 >= 0 and j + 1 < m and (a[i - 1][j + 1] == '*'):
			su += 1
		if j - 1 >= 0 and a[i][j - 1] == '*':
			su += 1
		if j + 1 < m and a[i][j + 1] == '*':
			su += 1
		if i + 1 < n and j - 1 >= 0 and (a[i + 1][j - 1] == '*'):
			su += 1
		if i + 1 < n and a[i + 1][j] == '*':
			su += 1
		if i + 1 < n and j + 1 < m and (a[i + 1][j + 1] == '*'):
			su += 1
		if a[i][j] == '.' and su != 0:
			f = 0
			break
		if a[i][j] != '.' and a[i][j] != '*':
			ch = int(a[i][j])
			if su != ch:
				f = 0
				break
	if f == 0:
		break
if f == 0:
	print('NO')
else:
	print('YES')


def bad():
	print('NO')
	exit()

def OK(a, b):
	return 0 <= a < n and 0 <= b < m

def get_neig(a, b):
	return [(a + dx[i], b + dy[i]) for i in range(8) if OK(a + dx[i], b + dy[i])]
(n, m) = map(int, input().split())
field = [input() for i in range(n)]
dx = (+1, +1, 0, -1, -1, -1, 0, +1)
dy = (0, +1, +1, +1, 0, -1, -1, -1)
for x in range(n):
	for y in range(m):
		if field[x][y] == '.':
			for el in get_neig(x, y):
				(x_, y_) = el
				if field[x_][y_] == '*':
					bad()
		elif field[x][y] != '*':
			to_do = int(field[x][y])
			c = 0
			for el in get_neig(x, y):
				(x_, y_) = el
				if field[x_][y_] == '*':
					c += 1
			if c != to_do:
				bad()
print('YES')


(n, m) = [int(x) for x in input().strip().split(' ')]
a = []
for _ in range(n):
	s = input().strip()
	b = []
	for i in range(m):
		if s[i] == '*':
			b.append(-100)
		elif s[i] == '.':
			b.append(0)
		else:
			b.append(int(s[i]))
	a.append(b)
for i in range(n):
	for j in range(m):
		if a[i][j] <= -100:
			if i < n - 1 and j < m - 1:
				a[i + 1][j + 1] -= 1
			if i < n - 1 and j > 0:
				a[i + 1][j - 1] -= 1
			if i > 0 and j < m - 1:
				a[i - 1][j + 1] -= 1
			if i > 0 and j > 0:
				a[i - 1][j - 1] -= 1
			if j < m - 1:
				a[i][j + 1] -= 1
			if i < n - 1:
				a[i + 1][j] -= 1
			if i > 0:
				a[i - 1][j] -= 1
			if j > 0:
				a[i][j - 1] -= 1
ans = True
for i in range(n):
	if ans == False:
		break
	for j in range(m):
		if a[i][j] != 0 and a[i][j] > -100:
			ans = False
			break
if ans:
	print('YES')
else:
	print('NO')


from itertools import product

def check_correct(field, x, y, n, m):
	is_inside_field = lambda x, y: 0 <= x < n and 0 <= y < m
	(dx_var, dy_var) = ((-1, 0, 1), (-1, 0, 1))
	bombs_count = 0
	for (dx, dy) in product(dx_var, dy_var):
		if dx == 0 and dy == 0:
			continue
		if not is_inside_field(x + dx, y + dy):
			continue
		if field[x + dx][y + dy] == '*':
			bombs_count += 1
	if field[x][y] == '.':
		return bombs_count == 0
	elif field[x][y].isdigit():
		return bombs_count == int(field[x][y])
	return True
(n, m) = list(map(int, input().split()))
field = [input() for _ in range(n)]
result = all((check_correct(field, i, j, n, m) for (i, j) in product(range(n), range(m))))
print('YES' if result else 'NO')


import os, sys, math

def main():
	testcase()

def testcase():
	(n, m) = [int(x) for x in input().split()]
	table = []
	for i in range(n):
		table.append(list(input()))
	if solve(table, n, m):
		print('YES')
	else:
		print('NO')

def solve(l, n, m):
	for i in range(n):
		for j in range(m):
			try:
				z = int(l[i][j])
			except ValueError:
				if l[i][j] == '.':
					z = 0
				else:
					continue
			if z != neighbours(l, i, j):
				debug('i,j:', i, j)
				return False
	return True

def neighbours(l, i, j):
	n = [(0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1)]
	s = 0
	for (x, y) in n:
		try:
			if j + y >= 0 and i + x >= 0:
				s += 1 if l[i + x][j + y] == '*' else 0
		except IndexError:
			pass
	return s

def debug(*args):
	if 'DEBUG' in os.environ:
		print(*args)
main()


(n, m) = [int(x) for x in input().split()]
p = []
for _ in range(n):
	p.append(input())
can = True

def valid(i, j):
	return i >= 0 and j >= 0 and (i < n) and (j < m)
for i in range(n):
	for j in range(m):
		if p[i][j] == '*':
			continue
		count = 0
		for di in range(-1, 2):
			for dj in range(-1, 2):
				if di == dj == 0:
					continue
				i1 = i + di
				j1 = j + dj
				if valid(i1, j1) and p[i1][j1] == '*':
					count += 1
		req = 0 if p[i][j] == '.' else int(p[i][j])
		if req != count:
			can = False
			break
	if not can:
		break
print('YES' if can else 'NO')


(n, m) = map(int, input().split())
field = []
for j in range(n):
	field.append([i for i in input()])
bombs = [[0 for i in range(m)] for j in range(n)]
for y in range(n):
	for x in range(m):
		if field[y][x] == '*':
			for i in range(y - 1, y + 2):
				for j in range(x - 1, x + 2):
					if 0 <= i < n and 0 <= j < m:
						bombs[i][j] += 1
right = True
for (row1, row2) in zip(field, bombs):
	for (x1, x2) in zip(row1, row2):
		if x1 != '*':
			if x1 == '.':
				number = 0
			else:
				number = int(x1)
			if number != x2:
				right = False
if right:
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().split())
a = []
for i in range(n):
	a.append(input())
flag = 0
if n == 1:
	if m == 1:
		if a[0][0] == '.' or a[0][0] == '*':
			flag = 0
		else:
			flag = 1
	else:
		for i in range(n):
			for j in range(m):
				cnt = 0
				if i == 0:
					if j == 0:
						cnt = int(bool(a[0][1] == '*'))
						if a[i][j] == '.':
							if cnt != 0:
								flag = 1
								break
						elif '1' <= a[i][j] <= '8':
							if cnt != int(a[i][j]):
								flag = 1
								break
					elif j == m - 1:
						cnt = int(bool(a[i][j - 1] == '*'))
						if a[i][j] == '.':
							if cnt != 0:
								flag = 1
								break
						elif '1' <= a[i][j] <= '8':
							if cnt != int(a[i][j]):
								flag = 1
								break
					else:
						cnt = int(bool(a[i][j - 1] == '*')) + int(bool(a[i][j + 1] == '*'))
						if a[i][j] == '.':
							if cnt != 0:
								flag = 1
								break
						if '1' <= a[i][j] <= '8':
							if cnt != int(a[i][j]):
								flag = 1
								break
elif m == 1:
	for i in range(n):
		for j in range(m):
			cnt = 0
			if i == 0:
				cnt = int(bool(a[1][0] == '*'))
				if a[i][j] == '.':
					if cnt != 0:
						flag = 1
						break
				elif '1' <= a[i][j] <= '8':
					if cnt != int(a[i][j]):
						flag = 1
						break
			elif i == n - 1:
				cnt = int(bool(a[i - 1][j] == '*'))
				if a[i][j] == '.':
					if cnt != 0:
						flag = 1
						break
				elif '1' <= a[i][j] <= '8':
					if cnt != int(a[i][j]):
						flag = 1
						break
			else:
				cnt = int(bool(a[i - 1][j] == '*')) + int(bool(a[i + 1][j] == '*'))
				if a[i][j] == '.':
					if cnt != 0:
						flag = 1
						break
				if '1' <= a[i][j] <= '8':
					if cnt != int(a[i][j]):
						flag = 1
						break
else:
	for i in range(n):
		for j in range(m):
			cnt = 0
			if i == 0:
				if j == 0:
					cnt = int(bool(a[1][0] == '*')) + int(bool(a[0][1] == '*')) + int(bool(a[1][1] == '*'))
					if a[i][j] == '.':
						if cnt != 0:
							flag = 1
							break
					elif '1' <= a[i][j] <= '8':
						if cnt != int(a[i][j]):
							flag = 1
							break
				elif j == m - 1:
					cnt = int(bool(a[i][j - 1] == '*')) + int(bool(a[i + 1][j] == '*')) + int(bool(a[i + 1][j - 1] == '*'))
					if a[i][j] == '.':
						if cnt != 0:
							flag = 1
							break
					elif '1' <= a[i][j] <= '8':
						if cnt != int(a[i][j]):
							flag = 1
							break
				else:
					cnt = int(bool(a[i][j - 1] == '*')) + int(bool(a[i][j + 1] == '*')) + int(bool(a[i + 1][j - 1] == '*')) + int(bool(a[i + 1][j + 1] == '*')) + int(bool(a[i + 1][j] == '*'))
					if a[i][j] == '.':
						if cnt != 0:
							flag = 1
							break
					if '1' <= a[i][j] <= '8':
						if cnt != int(a[i][j]):
							flag = 1
							break
			elif i == n - 1:
				if j == 0:
					cnt = int(bool(a[i][j + 1] == '*')) + int(bool(a[i - 1][j] == '*')) + int(bool(a[i - 1][j + 1] == '*'))
					if a[i][j] == '.':
						if cnt != 0:
							flag = 1
							break
					elif '1' <= a[i][j] <= '8':
						if cnt != int(a[i][j]):
							flag = 1
							break
				elif j == m - 1:
					cnt = int(bool(a[i][j - 1] == '*')) + int(bool(a[i - 1][j] == '*')) + int(bool(a[i - 1][j - 1] == '*'))
					if a[i][j] == '.':
						if cnt != 0:
							flag = 1
							break
					elif '1' <= a[i][j] <= '8':
						if cnt != int(a[i][j]):
							flag = 1
							break
				else:
					cnt = int(bool(a[i][j - 1] == '*')) + int(bool(a[i][j + 1] == '*')) + int(bool(a[i - 1][j - 1] == '*')) + int(bool(a[i - 1][j + 1] == '*')) + int(bool(a[i - 1][j] == '*'))
					if a[i][j] == '.':
						if cnt != 0:
							flag = 1
							break
					elif '1' <= a[i][j] <= '8':
						if cnt != int(a[i][j]):
							flag = 1
							break
			elif j == 0:
				cnt = int(bool(a[i][j + 1] == '*')) + int(bool(a[i + 1][j] == '*')) + int(bool(a[i - 1][j] == '*')) + int(bool(a[i - 1][j + 1] == '*')) + int(bool(a[i + 1][j + 1] == '*'))
				if a[i][j] == '.':
					if cnt != 0:
						flag = 1
						break
				elif '1' <= a[i][j] <= '8':
					if cnt != int(a[i][j]):
						flag = 1
						break
			elif j == m - 1:
				cnt = int(bool(a[i][j - 1] == '*')) + int(bool(a[i + 1][j] == '*')) + int(bool(a[i - 1][j] == '*')) + int(bool(a[i - 1][j - 1] == '*')) + int(bool(a[i + 1][j - 1] == '*'))
				if a[i][j] == '.':
					if cnt != 0:
						flag = 1
						break
				elif '1' <= a[i][j] <= '8':
					if cnt != int(a[i][j]):
						flag = 1
						break
			else:
				cnt = int(bool(a[i][j - 1] == '*')) + int(bool(a[i + 1][j] == '*')) + int(bool(a[i - 1][j] == '*')) + int(bool(a[i - 1][j - 1] == '*')) + int(bool(a[i + 1][j - 1] == '*')) + int(bool(a[i - 1][j + 1] == '*')) + int(bool(a[i + 1][j + 1] == '*')) + int(bool(a[i][j + 1] == '*'))
				if a[i][j] == '.':
					if cnt != 0:
						flag = 1
						break
				elif '1' <= a[i][j] <= '8':
					if cnt != int(a[i][j]):
						flag = 1
						break
		if flag:
			break
if flag:
	print('NO')
else:
	print('YES')


import sys

def main():
	pass

def binary(n):
	return bin(n).replace('0b', '')

def decimal(s):
	return int(s, 2)

def pow2(n):
	p = 0
	while n > 1:
		n //= 2
		p += 1
	return p

def isPrime(n):
	if n == 1:
		return False
	else:
		root = int(n ** 0.5)
		root += 1
		for i in range(2, root):
			if n % i == 0:
				return False
		return True

def lts(l):
	s = ''.join(map(str, l))
	return s

def stl(s):
	l = list(s)
	return l

def sq(a, target, arr=[]):
	s = sum(arr)
	if s == target:
		return arr
	if s >= target:
		return
	for i in range(len(a)):
		n = a[i]
		remaining = a[i + 1:]
		ans = sq(remaining, target, arr + [n])
		if ans:
			return ans

def SieveOfEratosthenes(n):
	cnt = 0
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	for p in range(2, n + 1):
		if prime[p]:
			cnt += 1
	return cnt

def nCr(n, r):
	f = math.factorial
	return f(n) // f(r) // f(n - r)
mod = int(1000000000.0) + 7

def ssinp():
	return sys.stdin.readline().strip()

def iinp():
	return int(input())

def nninp():
	return map(int, sys.stdin.readline().strip().split())

def llinp():
	return list(map(int, sys.stdin.readline().strip().split()))

def p(xyz):
	print(xyz)

def p2(a, b):
	print(a, b)
import math
(n, m) = nninp()
mat = []
for i in range(n):
	s = stl(ssinp())
	mat.append(s)
for i in range(n):
	for j in range(m):
		if mat[i][j] != '*':
			if mat[i][j] == '.':
				nos = 0
			else:
				nos = int(mat[i][j])
			b = 0
			for x in range(max(i - 1, 0), min(n, i + 2)):
				for y in range(max(j - 1, 0), min(m, j + 2)):
					if mat[x][y] == '*':
						b += 1
			if nos != b:
				print('NO')
				exit()
p('YES')


(n, m) = map(int, input().split())
arr = []
b = []
for i in range(n):
	a = list(map(lambda x: 100 if x == '*' else 0 if x == '.' else int(x), list(input())))
	j = -1
	while j < m:
		try:
			j = a.index(100, j + 1)
			b.append([i, j])
		except:
			break
	arr.append(a)
for (i, j) in b:
	(j1, j2) = (max(0, j - 1), min(j + 1, m - 1))
	if i > 0:
		i1 = i - 1
		arr[i1][j1:j2 + 1] = [x - 1 if x != 100 else x for x in arr[i1][j1:j2 + 1]]
	arr[i][j1:j2 + 1] = [x - 1 if x != 100 else x for x in arr[i][j1:j2 + 1]]
	if i < n - 1:
		i1 = i + 1
		arr[i1][j1:j2 + 1] = [x - 1 if x != 100 else x for x in arr[i1][j1:j2 + 1]]
for (i, j) in b:
	arr[i][j] = 0
for i in range(n):
	if any(arr[i]):
		print('NO')
		exit(0)
print('YES')


para = input().split(' ')
para = [int(e) for e in para]
big = []
for i in range(para[0]):
	small = input().strip()
	assert len(small) == para[1]
	l = []
	for c in small:
		if c == '*':
			l.append(100)
		elif c == '.':
			l.append(0)
		else:
			assert c in '12345678'
			l.append(int(c))
	big.append(l)
for i in range(len(big)):
	for j in range(len(big[0])):
		if big[i][j] > 50:
			for ii in [i - 1, i, i + 1]:
				for jj in [j - 1, j, j + 1]:
					if ii >= 0 and ii < len(big) and (jj >= 0) and (jj < len(big[0])):
						big[ii][jj] -= 1
result = True
for i in range(len(big)):
	for j in range(len(big[0])):
		if big[i][j] == 0 or big[i][j] > 50:
			continue
		else:
			result = False
			break
	if not result:
		break
if result:
	print('YES')
else:
	print('NO')


a = list(map(int, input().split()))
n = a[0]
m = a[1]
k = []
for i in range(n):
	l = input()
	k.append(l)
flag = 0
for i in range(n):
	for j in range(m):
		if k[i][j] == '*':
			continue
		count = 0
		if k[i][j] != '.':
			count = int(k[i][j])
		s = 0
		if j + 1 < m:
			if k[i][j + 1] == '*':
				s += 1
		if j - 1 >= 0:
			if k[i][j - 1] == '*':
				s += 1
		if i + 1 < n:
			if k[i + 1][j] == '*':
				s += 1
			if j + 1 < m:
				if k[i + 1][j + 1] == '*':
					s += 1
			if j - 1 >= 0:
				if k[i + 1][j - 1] == '*':
					s += 1
		if i - 1 >= 0:
			if k[i - 1][j] == '*':
				s += 1
			if j + 1 < m:
				if k[i - 1][j + 1] == '*':
					s += 1
			if j - 1 >= 0:
				if k[i - 1][j - 1] == '*':
					s += 1
		if s != count:
			print('NO')
			flag = 1
			break
	if flag == 1:
		break
if flag == 0:
	print('YES')


(n, m) = map(int, input().split())
a = [input().replace('.', '0') for i in range(n)]
for i in range(n):
	for j in range(m):
		if a[i][j] != '*' and int(a[i][j]) != sum(map(lambda b: b[max(0, j - 1):j + 2].count('*'), a[max(0, i - 1):i + 2])):
			print('NO')
			exit()
print('YES')


def isValid(i, j):
	if i >= 0 and i < n:
		if j >= 0 and j < m:
			return True
	return False
(n, m) = map(int, input().split())
l = []
flag = 0
for i in range(n):
	l.append(input())
for i in range(n):
	for j in range(m):
		if l[i][j].isdigit():
			digit = int(l[i][j])
			count = 0
			if isValid(i - 1, j - 1) and l[i - 1][j - 1] == '*':
				count += 1
			if isValid(i, j - 1) and l[i][j - 1] == '*':
				count += 1
			if isValid(i - 1, j + 1) and l[i - 1][j + 1] == '*':
				count += 1
			if isValid(i - 1, j) and l[i - 1][j] == '*':
				count += 1
			if isValid(i + 1, j) and l[i + 1][j] == '*':
				count += 1
			if isValid(i + 1, j - 1) and l[i + 1][j - 1] == '*':
				count += 1
			if isValid(i + 1, j + 1) and l[i + 1][j + 1] == '*':
				count += 1
			if isValid(i, j + 1) and l[i][j + 1] == '*':
				count += 1
			if count != digit:
				flag = 1
				break
		if l[i][j] == '.':
			count = 0
			if isValid(i - 1, j - 1) and l[i - 1][j - 1] == '*':
				count += 1
			if isValid(i, j - 1) and l[i][j - 1] == '*':
				count += 1
			if isValid(i - 1, j + 1) and l[i - 1][j + 1] == '*':
				count += 1
			if isValid(i - 1, j) and l[i - 1][j] == '*':
				count += 1
			if isValid(i + 1, j) and l[i + 1][j] == '*':
				count += 1
			if isValid(i + 1, j - 1) and l[i + 1][j - 1] == '*':
				count += 1
			if isValid(i + 1, j + 1) and l[i + 1][j + 1] == '*':
				count += 1
			if isValid(i, j + 1) and l[i][j + 1] == '*':
				count += 1
			if count > 0:
				flag = 1
				break
if flag == 0:
	print('YES')
else:
	print('NO')


n = list(map(int, input().split()))
l = [input() for i in range(n[0])]
for i in range(n[0]):
	flag = False
	for j in range(n[1]):
		count = 0
		if l[i][j].isdigit() or l[i][j] == '.':
			if i < n[0] and j + 1 < n[1]:
				if l[i][j + 1] == '*':
					count += 1
			if i + 1 < n[0] and j < n[1]:
				if l[i + 1][j] == '*':
					count += 1
			if i - 1 >= 0 and j < n[1]:
				if l[i - 1][j] == '*':
					count += 1
			if i - 1 >= 0 and j - 1 >= 0:
				if l[i - 1][j - 1] == '*':
					count += 1
			if i - 1 >= 0 and j + 1 < n[1]:
				if l[i - 1][j + 1] == '*':
					count += 1
			if i + 1 < n[0] and j - 1 >= 0:
				if l[i + 1][j - 1] == '*':
					count += 1
			if i + 1 < n[0] and j + 1 < n[1]:
				if l[i + 1][j + 1] == '*':
					count += 1
			if i < n[0] and j - 1 >= 0:
				if l[i][j - 1] == '*':
					count += 1
			if l[i][j].isdigit() and count != int(l[i][j]) or (l[i][j] == '.' and count):
				print('NO')
				flag = True
				break
	if flag:
		break
if flag == False:
	print('YES')


(n, m) = map(int, input().split())
g = [input() for _ in range(n)]

def ok():
	for i in range(n):
		for j in range(m):
			if g[i][j] == '*':
				continue
			x = 0 if g[i][j] == '.' else ord(g[i][j]) - ord('0')
			b = 0
			for di in range(-1, 2):
				for dj in range(-1, 2):
					if 0 <= i + di < n and 0 <= j + dj < m:
						if g[i + di][j + dj] == '*':
							b += 1
			if b != x:
				return False
	return True
print('yes' if ok() else 'no')


(n, m) = map(int, input().split())
mass = ['#' * (m + 2)]
for i in range(n):
	mass.append('#' + input() + '#')
mass.append('#' * (m + 2))
ask = 0
for i in range(1, n + 1):
	for j in range(1, m + 1):
		if mass[i][j].isdigit():
			if int(mass[i - 1][j] == '*') + int(mass[i + 1][j] == '*') + int(mass[i][j - 1] == '*') + int(mass[i][j + 1] == '*') + int(mass[i - 1][j - 1] == '*') + int(mass[i - 1][j + 1] == '*') + int(mass[i + 1][j - 1] == '*') + int(mass[i + 1][j + 1] == '*') != int(mass[i][j]):
				ask = 1
				break
		elif mass[i][j] == '.':
			if mass[i - 1][j] == '*' or mass[i + 1][j] == '*' or mass[i][j - 1] == '*' or (mass[i][j + 1] == '*') or (mass[i - 1][j - 1] == '*') or (mass[i - 1][j + 1] == '*') or (mass[i + 1][j - 1] == '*') or (mass[i + 1][j + 1] == '*'):
				ask = 1
				break
	if ask == 1:
		break
print('YES' if ask == 0 else 'NO')


def main():
	(n, m) = input().split()
	n = int(n)
	m = int(m)
	a = [['' for j in range(m)] for i in range(n)]
	for i in range(n):
		a[i] = input()
	for i in range(n):
		for j in range(m):
			if a[i][j] == '*':
				continue
			elif a[i][j] == '.':
				if i - 1 >= 0:
					if a[i - 1][j] == '*':
						print('NO')
						return
					if j - 1 >= 0:
						if a[i - 1][j - 1] == '*':
							print('NO')
							return
					if j + 1 < m:
						if a[i - 1][j + 1] == '*':
							print('NO')
							return
				if i + 1 < n:
					if a[i + 1][j] == '*':
						print('NO')
						return
					if j + 1 < m:
						if a[i + 1][j + 1] == '*':
							print('NO')
							return
					if j - 1 >= 0:
						if a[i + 1][j - 1] == '*':
							print('NO')
							return
				if j + 1 < m:
					if a[i][j + 1] == '*':
						print('NO')
						return
				if j - 1 >= 0:
					if a[i][j - 1] == '*':
						print('NO')
						return
			else:
				k = int(a[i][j])
				count = 0
				if i - 1 >= 0:
					if a[i - 1][j] == '*':
						count += 1
					if j - 1 >= 0:
						if a[i - 1][j - 1] == '*':
							count += 1
					if j + 1 < m:
						if a[i - 1][j + 1] == '*':
							count += 1
				if i + 1 < n:
					if a[i + 1][j] == '*':
						count += 1
					if j + 1 < m:
						if a[i + 1][j + 1] == '*':
							count += 1
					if j - 1 >= 0:
						if a[i + 1][j - 1] == '*':
							count += 1
				if j + 1 < m:
					if a[i][j + 1] == '*':
						count += 1
				if j - 1 >= 0:
					if a[i][j - 1] == '*':
						count += 1
				if count != k:
					print('NO')
					return
	print('YES')
main()


(m, n) = list(map(int, input().split()))
R = ['.'] * m
nR = [[0] * n for i in range(m)]
for i in range(m):
	A = list(input())
	R[i] = A[:]
for i in range(m):
	for j in range(n):
		if R[i][j] == '*':
			for ii in range(-1, 2):
				for jj in range(-1, 2):
					if ii == 0 and jj == 0:
						continue
					if 0 <= i + ii < m and 0 <= j + jj < n:
						nR[i + ii][j + jj] += 1
v = 1
for i in range(m):
	for j in range(n):
		if R[i][j] == '*':
			continue
		if R[i][j] == '.':
			if nR[i][j] != 0:
				v = 0
			continue
		if int(R[i][j]) != nR[i][j]:
			v = 0
if v == 1:
	print('YES')
else:
	print('NO')


def main():
	(r, c) = map(int, input().split())
	a = []
	a.append('.' * (c + 2))
	for i in range(r):
		s = input()
		a.append('.' + s + '.')
	a.append('.' * (c + 2))
	dy = [-1, -1, -1, 0, 0, 1, 1, 1]
	dx = [-1, 0, 1, -1, 1, -1, 0, 1]
	for i in range(1, r + 1):
		for j in range(1, c + 1):
			if a[i][j] == '*':
				continue
			cnt = 0
			for it in range(8):
				y = i + dy[it]
				x = j + dx[it]
				if a[y][x] == '*':
					cnt += 1
			if not cnt and a[i][j] == '.':
				continue
			if ord(a[i][j]) - ord('0') != cnt:
				print('NO')
				return
	print('YES')
main()


from operator import add
(dimensions, field) = (tuple(map(int, input().split(' '))), [])
for i in range(dimensions[0]):
	field.append(list(input()))
relCoords = [(0, -1), (1, -1), (1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1)]
bombCount = 0

def computeBombCount(pos, depth):
	global bombCount
	if 0 > pos[0] or pos[0] >= dimensions[0]:
		return 0
	if 0 > pos[1] or pos[1] >= dimensions[1]:
		return 0
	if depth == 1:
		if field[pos[0]][pos[1]] == '*':
			bombCount = bombCount + 1
	else:
		for i in range(len(relCoords)):
			newCoord = tuple(map(add, pos, relCoords[i]))
			computeBombCount(newCoord, 1)
for i in range(dimensions[0]):
	for j in range(dimensions[1]):
		computeBombCount((i, j), 0)
		if field[i][j] in tuple(map(str, range(9))):
			if not int(field[i][j]) == bombCount:
				print('NO')
				exit()
		elif field[i][j] == '.' and bombCount > 0:
			print('NO')
			exit()
		bombCount = 0
print('YES')


def voz(a, b, c):
	for t in range(a):
		for e in range(b):
			if c[t][e] == '*':
				continue
			elif c[t][e] == '.':
				for k in range(t - 1, t + 2):
					for s in range(e - 1, e + 2):
						if k < 0 or s < 0:
							continue
						try:
							po = c[k][s]
						except IndexError:
							po = '!'
						if po == '*':
							print('NO')
							return
			elif c[t][e].isdigit():
				count = 0
				p = 'as'
				for l in range(t - 1, t + 2):
					for u in range(e - 1, e + 2):
						if l < 0 or u < 0:
							continue
						try:
							p = c[l][u]
						except IndexError:
							p = 'as'
						if p == '*':
							count = count + 1
				if not int(c[t][e]) == count:
					print('NO')
					return
	print('YES')
	return
a = input().split(' ')
qa = int(a[0])
qz = int(a[1])
c = [list(input()) for k in range(qa)]
voz(qa, qz, c)


(n, m) = map(int, input().split())
x1 = []
for i in range(n):
	y1 = []
	s = input()
	for j in s:
		y1.append(j)
	x1.append(y1)
ind = True

def f(x, y):
	a = []
	if x - 1 >= 0:
		if y - 1 >= 0:
			a.append([x - 1, y - 1])
		a.append([x - 1, y])
		if y + 1 < m:
			a.append([x - 1, y + 1])
	if y - 1 >= 0:
		a.append([x, y - 1])
	if y + 1 < m:
		a.append([x, y + 1])
	if x + 1 < n:
		if y - 1 >= 0:
			a.append([x + 1, y - 1])
		a.append([x + 1, y])
		if y + 1 < m:
			a.append([x + 1, y + 1])
	return a
ind = True
for i in range(n):
	for j in range(m):
		r = f(i, j)
		if x1[i][j] == '.':
			count = 0
			for w in r:
				if x1[w[0]][w[1]] == '*':
					count += 1
			if count > 0:
				ind = False
		elif x1[i][j] != '*':
			t = int(x1[i][j])
			count = 0
			for w in r:
				if x1[w[0]][w[1]] == '*':
					count += 1
			if count != t:
				ind = False
if ind:
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().split())
bando = list()
for i in range(n):
	tmp = input()
	bando.append(tmp)

def kiemtra(i, j):
	bom = 0
	toado = [[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 tmp in toado:
		if tmp[0] < 0 or tmp[0] >= n or tmp[1] < 0 or (tmp[1] >= m):
			continue
		if bando[tmp[0]][tmp[1]] == '*':
			bom += 1
	return bom
for i in range(n):
	for j in range(m):
		if bando[i][j] >= '0' and bando[i][j] <= '9':
			tmp = int(bando[i][j])
			if tmp != kiemtra(i, j):
				print('NO')
				exit()
		elif bando[i][j] == '.':
			if kiemtra(i, j) > 0:
				print('NO')
				exit()
print('YES')


def check(i, j, a, n, m):
	if i < 0 or j < 0 or i >= n or (j >= m):
		return 0
	if a[i][j] == '*':
		return 1
	else:
		return 0
(n, m) = input().split()
n = int(n)
m = int(m)
a = []
for i in range(n):
	a += [list(input())]
flag = 1
for i in range(n):
	for j in range(m):
		if a[i][j] != '*':
			count = check(i - 1, j, a, n, m) + check(i, j - 1, a, n, m) + check(i + 1, j, a, n, m) + check(i, j + 1, a, n, m) + check(i - 1, j - 1, a, n, m) + check(i - 1, j + 1, a, n, m) + check(i + 1, j - 1, a, n, m) + check(i + 1, j + 1, a, n, m)
			if a[i][j] == '.':
				if count == 0:
					continue
				else:
					flag = 0
					break
			if count == int(a[i][j]):
				continue
			else:
				flag = 0
				break
if flag:
	print('YES')
else:
	print('NO')


def chk(l, i, j):
	t = 0
	if l[i - 1][j - 1] == '*':
		t += 1
	if l[i][j - 1] == '*':
		t += 1
	if l[i + 1][j - 1] == '*':
		t += 1
	if l[i - 1][j] == '*':
		t += 1
	if l[i + 1][j] == '*':
		t += 1
	if l[i - 1][j + 1] == '*':
		t += 1
	if l[i][j + 1] == '*':
		t += 1
	if l[i + 1][j + 1] == '*':
		t += 1
	return t
(n, m) = map(int, input().split())
a = list()
a.append('.' * (m + 2))
for i in range(n):
	a.append('.' + input().strip() + '.')
a.append('.' * (m + 2))
last = True
for i in range(n):
	for j in range(m):
		if a[i + 1][j + 1] != '*' and a[i + 1][j + 1] != '.':
			if int(a[i + 1][j + 1]) != chk(a, i + 1, j + 1):
				last = False
				print('NO')
				break
		if a[i + 1][j + 1] == '.':
			if chk(a, i + 1, j + 1) != 0:
				last = False
				print('NO')
				break
	if last is False:
		break
if last is True:
	print('YES')


(n, m) = [int(x) for x in input().split()]
v = [input().strip() for _ in range(n)]
ans = True
for i in range(n):
	for j in range(m):
		if v[i][j] == '*':
			continue
		r = 0
		for u in range(i - 1, i + 2):
			for w in range(j - 1, j + 2):
				if u < 0 or u >= n or w < 0 or (w >= m):
					continue
				if v[u][w] == '*':
					r += 1
		if r == 0 and v[i][j] != '.':
			ans = False
		if r > 0 and v[i][j] != chr(ord('0') + r):
			ans = False
print('YES' if ans else 'NO')


(n, m) = str(input()).split()
n = int(n)
m = int(m)
lines = []
for i in range(0, n):
	lines += [str(input())]
valid = 1
for i in range(0, len(lines)):
	for j in range(0, len(lines[i])):
		if lines[i][j] == '*':
			continue
		bombs = 0
		if i > 0 and lines[i - 1][j] == '*':
			bombs += 1
		if i + 1 < n and lines[i + 1][j] == '*':
			bombs += 1
		if j > 0 and lines[i][j - 1] == '*':
			bombs += 1
		if j + 1 < m and lines[i][j + 1] == '*':
			bombs += 1
		if j > 0 and i > 0 and (lines[i - 1][j - 1] == '*'):
			bombs += 1
		if j > 0 and i + 1 < n and (lines[i + 1][j - 1] == '*'):
			bombs += 1
		if i > 0 and j + 1 < m and (lines[i - 1][j + 1] == '*'):
			bombs += 1
		if i + 1 < n and j + 1 < m and (lines[i + 1][j + 1] == '*'):
			bombs += 1
		k = 1
		if lines[i][j] == '.':
			k = 0
		else:
			k = int(lines[i][j])
		if bombs != k:
			valid = 0
			print('NO')
			exit(0)
if valid == 1:
	print('YES')
if valid == 0:
	print('NO')


def sweep():
	inp = input().split()
	n = int(inp[0])
	m = int(inp[1])
	field = ['-' * (m + 2)]
	for row in range(0, n):
		seeds = '-'
		seeds += input()
		seeds += '-'
		field.append(seeds)
	field.append('-' * (m + 2))
	for row in range(1, n + 1):
		for col in range(1, m + 1):
			if field[row][col] not in ['-', '*']:
				bombs = 0
				for i in range(-1, 2):
					for j in range(-1, 2):
						if field[row + i][col + j] == '*':
							bombs += 1
				if field[row][col] == '.':
					if bombs != 0:
						return 'NO'
				elif bombs != int(field[row][col]):
					return 'NO'
	return 'YES'
print(sweep())


__author__ = 'Esfandiar'
(n, m) = map(int, input().split())
s = [input() for j in range(n)]

def check(i, j, n, m, s):
	bom = 0
	if i != 0 and j != 0:
		bom += s[i - 1][j - 1] == '*'
	if i != 0:
		bom += s[i - 1][j] == '*'
	if i != 0 and j + 1 != m:
		bom += s[i - 1][j + 1] == '*'
	if j != 0:
		bom += s[i][j - 1] == '*'
	if j + 1 != m:
		bom += s[i][j + 1] == '*'
	if i + 1 != n and j != 0:
		bom += s[i + 1][j - 1] == '*'
	if i + 1 != n:
		bom += s[i + 1][j] == '*'
	if i + 1 != n and j + 1 != m:
		bom += s[i + 1][j + 1] == '*'
	return bom
for i in range(n):
	for j in range(m):
		if s[i][j] == '*':
			continue
		if s[i][j] == '.':
			if check(i, j, n, m, s) > 0:
				print('NO')
				exit()
		elif check(i, j, n, m, s) != int(s[i][j]):
			print('NO')
			exit()
print('YES')


(n, m) = (int(x) for x in input().split())
a = []
for i in range(n):
	a.append(list(input()))
for iy in range(n):
	for ix in range(m):
		if a[iy][ix] == '*':
			continue
		else:
			c = 0
			for i in range(-1, 2):
				for j in range(-1, 2):
					if i == j == 0:
						continue
					if ix + j < 0 or iy + i < 0 or ix + j >= m or (iy + i >= n):
						continue
					if a[iy + i][ix + j] == '*':
						c += 1
					else:
						continue
			if a[iy][ix] == '.':
				if c == 0:
					continue
				else:
					print('NO')
					exit(0)
			elif c == int(a[iy][ix]):
				continue
			else:
				print('NO')
				exit(0)
print('YES')


(n, m) = map(int, input().split())
mp = []
for i in range(n):
	mp.append(input())
x = 0
while x < n:
	y = 0
	while y < m:
		if mp[x][y] == '*':
			y += 1
			continue
		num = 0
		for dx in range(-1, 2):
			for dy in range(-1, 2):
				tx = x + dx
				ty = y + dy
				if tx < 0 or tx >= n or ty < 0 or (ty >= m):
					continue
				if mp[tx][ty] == '*':
					num += 1
		if mp[x][y] == '.':
			mp[x] = mp[x][:y] + '0' + mp[x][y + 1:]
		if num != int(mp[x][y]):
			print('NO')
			quit(0)
		y += 1
	x += 1
print('YES')


(n, m) = [int(i) for i in input().split()]
arr = [input() for i in range(n)]
t = [[1, 0], [1, 1], [1, -1], [-1, 0], [-1, 1], [-1, -1], [0, 1], [0, -1]]
for i in range(n):
	for j in range(m):
		cnt = 0
		if arr[i][j] == '*':
			continue
		for k in t:
			if k[1] + j >= m or k[1] + j < 0 or k[0] + i >= n or (k[0] + i < 0):
				continue
			if arr[i + k[0]][j + k[1]] == '*':
				cnt += 1
		if cnt == 0:
			if arr[i][j] != '.':
				print('NO')
				exit()
		elif str(cnt) != arr[i][j]:
			print('NO')
			exit()
print('YES')


(n, m) = map(int, input().split())
num = ['.' * (m + 2)]
for i in range(n):
	s = '.'
	s += input()
	s += '.'
	num.append(s)
flag = True
num.append('.' * (m + 2))
ans = 0
for i in range(1, n + 1):
	for j in range(1, m + 1):
		if num[i][j] == '.':
			if num[i - 1][j] != '*' and num[i - 1][j - 1] != '*' and (num[i - 1][j + 1] != '*') and (num[i][j - 1] != '*') and (num[i][j + 1] != '*') and (num[i + 1][j - 1] != '*') and (num[i + 1][j] != '*') and (num[i + 1][j + 1] != '*'):
				ans += 1
			else:
				flag = False
		if num[i][j] != '.' and num[i][j] != '*':
			x = int(num[i][j])
			if num[i - 1][j] == '*':
				x -= 1
			if num[i - 1][j - 1] == '*':
				x -= 1
			if num[i - 1][j + 1] == '*':
				x -= 1
			if num[i][j - 1] == '*':
				x -= 1
			if num[i][j + 1] == '*':
				x -= 1
			if num[i + 1][j - 1] == '*':
				x -= 1
			if num[i + 1][j] == '*':
				x -= 1
			if num[i + 1][j + 1] == '*':
				x -= 1
			if x != 0:
				flag = False
if not flag:
	print('NO')
else:
	print('YES')


(nRows, nColumns) = map(int, input().split())
field = [input()[:nColumns] for y in range(nRows)]
for y in range(nRows):
	for x in range(nColumns):
		if field[y][x] == '*':
			continue
		count = 0
		for dx in range(-1, 2):
			for dy in range(-1, 2):
				nx = x + dx
				ny = y + dy
				if 0 <= nx < nColumns and 0 <= ny < nRows and (field[ny][nx] == '*'):
					count += 1
		if field[y][x] == '.' and count != 0 or (field[y][x] != '.' and int(field[y][x]) != count):
			print('NO')
			exit()
print('YES')


def check_square(grid: [[str]], i: int, j: int):
	return int(grid[i][j]) == [grid[i][j - 1], grid[i][j + 1], grid[i - 1][j - 1], grid[i - 1][j], grid[i - 1][j + 1], grid[i + 1][j - 1], grid[i + 1][j], grid[i + 1][j + 1]].count('*')

def check(grid: [[str]], m: int, n: int):
	for i in range(1, m + 1):
		for j in range(1, n + 1):
			if grid[i][j] in '0123456789':
				if not check_square(grid, i, j):
					return 'NO'
	return 'YES'
(m, n) = [int(x) for x in input().split()]
grid = ['0' * (n + 2)]
for i in range(m):
	row = input().replace('.', '0')
	row = '0' + row + '0'
	grid.append(row)
grid.append('0' * (n + 2))
print(check(grid, m, n))


import sys
(n, m) = list(map(int, input().split()))
a = []
for x in range(n):
	a.append(list(input()))
bomb = 0
for row in range(n):
	for coll in range(m):
		if a[row][coll] == '*':
			continue
		elif '1' <= a[row][coll] <= '8':
			if row - 1 >= 0 and coll - 1 >= 0:
				if a[row - 1][coll - 1] == '*':
					bomb += 1
			if coll - 1 >= 0:
				if a[row][coll - 1] == '*':
					bomb += 1
			if coll - 1 >= 0 and row + 1 < n:
				if a[row + 1][coll - 1] == '*':
					bomb += 1
			if row - 1 >= 0:
				if a[row - 1][coll] == '*':
					bomb += 1
			if row + 1 < n:
				if a[row + 1][coll] == '*':
					bomb += 1
			if row - 1 >= 0 and coll + 1 < m:
				if a[row - 1][coll + 1] == '*':
					bomb += 1
			if coll + 1 < m:
				if a[row][coll + 1] == '*':
					bomb += 1
			if row + 1 < n and coll + 1 < m:
				if a[row + 1][coll + 1] == '*':
					bomb += 1
			if bomb != int(a[row][coll]):
				print('NO')
				sys.exit()
			bomb = 0
		elif a[row][coll] == '.':
			bomb = 0
			if row - 1 >= 0 and coll - 1 >= 0:
				if a[row - 1][coll - 1] == '*':
					bomb += 1
			if coll - 1 >= 0:
				if a[row][coll - 1] == '*':
					bomb += 1
			if coll - 1 >= 0 and row + 1 < n:
				if a[row + 1][coll - 1] == '*':
					bomb += 1
			if row - 1 >= 0:
				if a[row - 1][coll] == '*':
					bomb += 1
			if row + 1 < n:
				if a[row + 1][coll] == '*':
					bomb += 1
			if row - 1 >= 0 and coll + 1 < m:
				if a[row - 1][coll + 1] == '*':
					bomb += 1
			if coll + 1 < m:
				if a[row][coll + 1] == '*':
					bomb += 1
			if row + 1 < n and coll + 1 < m:
				if a[row + 1][coll + 1] == '*':
					bomb += 1
			if bomb != 0:
				print('NO')
				sys.exit()
			bomb = 0
print('YES')


import sys
input = sys.stdin.readline
(n, m) = list(map(int, input().split()))
a = [None] * n
for i in range(n):
	a[i] = input()
flag = False
for i in range(n):
	for j in range(m):
		if a[i][j] != '*':
			cnt = 0
			if i > 0:
				if a[i - 1][j] == '*':
					cnt += 1
				if j < m - 1 and a[i - 1][j + 1] == '*':
					cnt += 1
				if j > 0 and a[i - 1][j - 1] == '*':
					cnt += 1
			if i < n - 1:
				if a[i + 1][j] == '*':
					cnt += 1
				if j < m - 1 and a[i + 1][j + 1] == '*':
					cnt += 1
				if j > 0 and a[i + 1][j - 1] == '*':
					cnt += 1
			if j < m - 1 and a[i][j + 1] == '*':
				cnt += 1
			if j > 0 and a[i][j - 1] == '*':
				cnt += 1
			if cnt == 0 and a[i][j] != '.':
				flag = True
				break
			if cnt != 0 and str(cnt) != a[i][j]:
				flag = True
		if flag:
			break
	if flag:
		break
if flag:
	print('NO')
else:
	print('YES')


def isbomb(x, y, g, n, m):
	c = 0
	if y + 1 < m:
		if g[x][y + 1] == '*':
			c = c + 1
		if x - 1 >= 0:
			if g[x - 1][y + 1] == '*':
				c += 1
	if y - 1 >= 0:
		if g[x][y - 1] == '*':
			c += 1
		if x + 1 < n:
			if g[x + 1][y - 1] == '*':
				c += 1
	if x + 1 < n:
		if g[x + 1][y] == '*':
			c += 1
		if y + 1 < m:
			if g[x + 1][y + 1] == '*':
				c += 1
	if x - 1 >= 0:
		if g[x - 1][y] == '*':
			c += 1
		if y - 1 >= 0:
			if g[x - 1][y - 1] == '*':
				c += 1
	return c
(n, m) = map(int, input().split())
g = []
f = 0
for i in range(n):
	z = list(input())
	g.append(z)
for i in range(n):
	for j in range(m):
		if g[i][j] == '.':
			c = isbomb(i, j, g, n, m)
			r = 0
			if c != r:
				f = 1
		elif g[i][j] != '*':
			r = int(g[i][j])
			c = isbomb(i, j, g, n, m)
			if c != r:
				f = 1
if f == 1:
	print('NO')
else:
	print('YES')


import logging
fmt = '%(levelname)s - %(name)s (line:%(lineno)s) - %(message)s'
formatter = logging.Formatter(fmt)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(formatter)
logger = logging.getLogger('m')
logger.setLevel(logging.DEBUG)
logger.addHandler(ch)

def neighbors(i, j, a):
	(n, m) = (len(a), len(a[0]))
	offsets = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]]
	pos = [(i + x, j + y) for (x, y) in offsets]
	pos = [(x, y) for (x, y) in pos if 0 <= x < n and 0 <= y < m]
	return [a[x][y] for (x, y) in pos]

def solve(a):
	for i in range(len(a)):
		for j in range(len(a[0])):
			nei = neighbors(i, j, a)
			if a[i][j] == '.' and any((x == '*' for x in nei)):
				logger.debug('Bomb next to empty cell %s %s', i, j)
				return False
			elif a[i][j].isnumeric():
				if int(a[i][j]) != len([x for x in nei if x == '*']):
					logger.debug('Number does not match at %s %s', i, j)
					return False
			elif a[i][j] == '*':
				if '.' in nei:
					return False
	return True

def main():
	(n, m) = map(int, input().strip().split())
	a = [list(input().strip()) for _ in range(n)]
	result = solve(a)
	print('YES' if result else 'NO')
main()


(n, m) = map(int, input().split())
A = [input() for i in range(n)]
f = 0

def check(i, j, n1, t):
	if t:
		c = 0
		for (ii, jj) in [[0, 1], [1, 0], [1, 1], [-1, -1], [-1, 0], [0, -1], [-1, 1], [1, -1]]:
			if 0 <= i + ii < n and 0 <= j + jj < m and (A[i + ii][j + jj] == '*'):
				c += 1
		if c == int(n1):
			return False
		return True
	else:
		for (ii, jj) in [[0, 1], [1, 0], [1, 1], [-1, -1], [-1, 0], [0, -1], [-1, 1], [1, -1]]:
			if 0 <= i + ii < n and 0 <= j + jj < m and (A[i + ii][j + jj] == '*'):
				return True
		return False
for i in range(n):
	for j in range(m):
		if A[i][j] >= '0' and A[i][j] <= '9':
			if check(i, j, A[i][j], True):
				f = 1
				break
		elif A[i][j] == '.':
			if check(i, j, A[i][j], False):
				f = 1
				break
	if f:
		break
if f:
	print('NO')
else:
	print('YES')


(n, m) = map(int, input().split())
grid = [input() for i in range(n)]

def num_neighbors(i, j):
	num = 0
	for di in range(-1, 2):
		for dj in range(-1, 2):
			if di != 0 or di != dj:
				(x, y) = (i + di, j + dj)
				if x >= 0 and x < n and (y >= 0) and (y < m) and (grid[x][y] == '*'):
					num += 1
	return num
result = 'YES'
for i in range(n):
	for j in range(m):
		dig = ord(grid[i][j]) - 48
		if grid[i][j] == '.':
			dig = 0
		if dig >= 0 and dig < 9:
			if num_neighbors(i, j) != dig:
				result = 'NO'
print(result)


(n, m) = map(int, input().split())
l = []
d = [['.'] * m for i in range(n)]
(n, m) = (n - 1, m - 1)

def check(l, i):
	if '*' in l[i]:
		a = l[i].index('*')
	else:
		a = -1
	while a >= 0:
		d[i][a] = '*'
		if a + 1 <= m:
			if d[i][a + 1] == '.':
				d[i][a + 1] = 1
			elif d[i][a + 1] != '.' and d[i][a + 1] != '*':
				d[i][a + 1] += 1
			if i - 1 >= 0 and d[i - 1][a + 1] == '.':
				d[i - 1][a + 1] = 1
			elif i - 1 >= 0 and d[i - 1][a + 1] != '.' and (d[i - 1][a + 1] != '*'):
				d[i - 1][a + 1] += 1
			if i + 1 <= n and d[i + 1][a + 1] == '.':
				d[i + 1][a + 1] = 1
			elif i + 1 <= n and d[i + 1][a + 1] != '.' and (d[i + 1][a + 1] != '*'):
				d[i + 1][a + 1] += 1
		if a - 1 >= 0:
			if d[i][a - 1] == '.':
				d[i][a - 1] = 1
			elif d[i][a - 1] != '.' and d[i][a - 1] != '*':
				d[i][a - 1] += 1
			if i - 1 >= 0 and d[i - 1][a - 1] == '.':
				d[i - 1][a - 1] = 1
			elif i - 1 >= 0 and d[i - 1][a - 1] != '.' and (d[i - 1][a - 1] != '*'):
				d[i - 1][a - 1] += 1
			if i + 1 <= n and d[i + 1][a - 1] == '.':
				d[i + 1][a - 1] = 1
			elif i + 1 <= n and d[i + 1][a - 1] != '.' and (d[i + 1][a - 1] != '*'):
				d[i + 1][a - 1] += 1
		if i - 1 >= 0 and d[i - 1][a] == '.':
			d[i - 1][a] = 1
		elif i - 1 >= 0 and d[i - 1][a] != '.' and (d[i - 1][a] != '*'):
			d[i - 1][a] += 1
		if i + 1 <= n and d[i + 1][a] == '.':
			d[i + 1][a] = 1
		elif i + 1 <= n and d[i + 1][a] != '.' and (d[i + 1][a] != '*'):
			d[i + 1][a] += 1
		if '*' in l[i][a + 1:]:
			a = l[i][a + 1:].index('*') + a + 1
		else:
			a = -1
	return d
for i in range(n + 1):
	l.append(list(input()))
for i in range(n + 1):
	li = check(l, i)
for i in range(n + 1):
	for j in range(m + 1):
		if d[i][j] != '.' and d[i][j] != '*':
			d[i][j] = str(d[i][j])
if d == l:
	print('YES')
else:
	print('NO')


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

def no_of_bombs(row, col, field):
	return sum((field[i][j] == '*' for i in range(max(0, row - 1), min(n, row + 2)) for j in range(max(0, col - 1), min(m, col + 2))))

def is_valid_field(row, col, field):
	return no_of_bombs(row, col, field) == int(field[row][col])
field = [input().replace('.', '0') for i in range(n)]
print('YES' if all((field[i][j] == '*' or is_valid_field(i, j, field) for i in range(n) for j in range(m))) else 'NO')


q = '12345678'
(n, m) = map(int, input().split())
s = ''
for i in range(n):
	s += input()
for i in range(n * m):
	k = ''
	if i - m >= 0:
		k += s[i - m]
	if i - m - 1 >= 0 and (i - m - 1) % m != m - 1:
		k += s[i - m - 1]
	if i - m + 1 >= 0 and (i - m + 1) % m != 0:
		k += s[i - m + 1]
	if i - 1 >= 0 and i % m != 0:
		k += s[i - 1]
	if i + 1 < n * m and (i + 1) % m != 0:
		k += s[i + 1]
	if i + m < n * m:
		k += s[i + m]
	if i + m - 1 < n * m and (i + m - 1) % m != m - 1:
		k += s[i + m - 1]
	if i + m + 1 < n * m and (i + m + 1) % m != 0:
		k += s[i + m + 1]
	bombs = k.count('*')
	if s[i] == '.':
		if bombs > 0:
			print('NO')
			exit(0)
	elif s[i] in q:
		if bombs != int(s[i]):
			print('NO')
			exit(0)
print('YES')


(n, m) = map(int, input().split())
c = [input().replace('.', '0') for _ in [0] * n]
b = [[0] * m for _ in [0] * n]
for i in range(n):
	for j in range(m):
		if c[i][j] != '.' and c[i][j] != '*':
			b[i][j] += int(c[i][j])
		elif c[i][j] == '*':
			for k in range(max(i - 1, 0), min(i + 2, n)):
				for l in range(max(j - 1, 0), min(j + 2, m)):
					b[k][l] -= 1
for i in range(n):
	for j in range(m):
		if b[i][j] != 0 and c[i][j] != '*':
			print('NO')
			exit()
print('YES')


from sys import stdin, stdout

def rint():
	return map(int, stdin.readline().split())
(r, c) = rint()

def check(i, j):
	cur_val = m[i][j]
	if cur_val == -1:
		return True
	cnt = 0
	p = [[-1, -1, -1] for _ in range(3)]
	p[1][1] = 0
	if i - 1 < 0:
		p[0][0] = 0
		p[0][1] = 0
		p[0][2] = 0
	if j - 1 < 0:
		p[0][0] = 0
		p[1][0] = 0
		p[2][0] = 0
	if i + 1 >= r:
		p[2][0] = 0
		p[2][1] = 0
		p[2][2] = 0
	if j + 1 >= c:
		p[0][2] = 0
		p[1][2] = 0
		p[2][2] = 0
	for rr in range(3):
		for cc in range(3):
			if p[rr][cc] == 0:
				continue
			if m[i + rr - 1][j + cc - 1] == -1:
				cnt += 1
	return cnt == m[i][j]
m = []
for i in range(r):
	tmp = input()
	tmp2 = []
	for i in range(c):
		if tmp[i] == '.':
			tmp2.append(0)
		elif tmp[i] == '*':
			tmp2.append(-1)
		else:
			tmp2.append(int(tmp[i]))
	m.append(tmp2)
for i in range(r):
	for j in range(c):
		if check(i, j) == False:
			print('NO')
			exit()
print('YES')


adj = ((1, -1), (1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1))
(n, m) = map(int, input().split())
inp = {}
grid = {}
for y in range(n):
	for (x, c) in enumerate(input()):
		inp[x, y] = c
		if c == '*':
			grid[x, y] = '*'
		else:
			grid[x, y] = '.'
for (key, val) in grid.items():
	(x, y) = key
	if val == '*':
		for (ax, ay) in adj:
			akey = (ax + x, ay + y)
			if akey in grid and grid[akey] != '*':
				if grid[akey] == '.':
					grid[akey] = '1'
				else:
					grid[akey] = str(int(grid[akey]) + 1)
if grid == inp:
	print('YES')
else:
	print('NO')


from sys import stdin, stdout
from collections import Counter, OrderedDict, defaultdict
import math
import re
cin = stdin.readline
cout = stdout.write
mp = lambda : list(map(int, cin().split()))

def chars():
	s = cin()
	return list(s[:len(s) - 1])

def pl(a):
	for val in a:
		cout(str(val) + ' ')
	cout('\n')

def binSearch(l, val, start, end):
	if end < start:
		return False
	mid = (start + end) // 2
	if l[mid] == val:
		return True
	elif l[mid] < val:
		return binSearch(l, val, mid + 1, end)
	elif l[mid] > val:
		return binSearch(l, val, start, mid - 1)

def myLog(x, y):
	z = int(math.log(x, y))
	if y ** (z + 1) > x:
		return z
	elif y ** (z + 1) == x:
		return z + 1

def isPalindrome(s):
	return s == s[::-1]
(n, m) = mp()
s = []
for i in range(n):
	s += [chars()]
for i in range(n):
	for j in range(m):
		temp = 1
		if s[i][j] == '.':
			if i + 1 < n:
				if s[i + 1][j] == '*':
					cout('no\n')
					temp = 0
					break
			if i - 1 >= 0:
				if s[i - 1][j] == '*':
					cout('no\n')
					temp = 0
					break
			if j + 1 < m:
				if s[i][j + 1] == '*':
					cout('no\n')
					temp = 0
					break
			if j - 1 >= 0:
				if s[i][j - 1] == '*':
					cout('no\n')
					temp = 0
					break
			if i + 1 < n and j - 1 >= 0:
				if s[i + 1][j - 1] == '*':
					cout('no\n')
					temp = 0
					break
			if i + 1 < n and j + 1 < m:
				if s[i + 1][j + 1] == '*':
					cout('no\n')
					temp = 0
					break
			if i - 1 >= 0 and j + 1 < m:
				if s[i - 1][j + 1] == '*':
					cout('no\n')
					temp = 0
					break
			if i - 1 >= 0 and j - 1 >= 0:
				if s[i - 1][j - 1] == '*':
					cout('no\n')
					temp = 0
					break
		elif s[i][j] != '*':
			k = int(s[i][j])
			cnt = 0
			if i + 1 < n:
				if s[i + 1][j] == '*':
					cnt += 1
			if i - 1 >= 0:
				if s[i - 1][j] == '*':
					cnt += 1
			if j + 1 < m:
				if s[i][j + 1] == '*':
					cnt += 1
			if j - 1 >= 0:
				if s[i][j - 1] == '*':
					cnt += 1
			if i + 1 < n and j - 1 >= 0:
				if s[i + 1][j - 1] == '*':
					cnt += 1
			if i + 1 < n and j + 1 < m:
				if s[i + 1][j + 1] == '*':
					cnt += 1
			if i - 1 >= 0 and j + 1 < m:
				if s[i - 1][j + 1] == '*':
					cnt += 1
			if i - 1 >= 0 and j - 1 >= 0:
				if s[i - 1][j - 1] == '*':
					cnt += 1
			if cnt != k:
				cout('no\n')
				temp = 0
				break
	if not temp:
		break
else:
	cout('yes\n')


import math
from collections import defaultdict
ml = lambda : map(int, input().split())
ll = lambda : list(map(int, input().split()))
ii = lambda : int(input())
ip = lambda : list(input())
(a, b) = ml()
lol = []
for i in range(a):
	w = list(input())
	lol.append(w)
flag = 0
for i in range(a):
	for j in range(b):
		k = 0
		if i - 1 >= 0:
			if lol[i - 1][j] == '*':
				k += 1
			if j - 1 >= 0:
				if lol[i - 1][j - 1] == '*':
					k += 1
			if j + 1 < b:
				if lol[i - 1][j + 1] == '*':
					k += 1
		if i + 1 < a:
			if lol[i + 1][j] == '*':
				k += 1
			if j - 1 >= 0:
				if lol[i + 1][j - 1] == '*':
					k += 1
			if j + 1 < b:
				if lol[i + 1][j + 1] == '*':
					k += 1
		if j - 1 >= 0:
			if lol[i][j - 1] == '*':
				k += 1
		if j + 1 < b:
			if lol[i][j + 1] == '*':
				k += 1
		if lol[i][j] in {'1', '2', '3', '4', '5', '6', '7', '8'}:
			if int(lol[i][j]) != k:
				flag = 1
		elif lol[i][j] == '.' and k != 0:
			flag = 1
if flag:
	print('NO')
else:
	print('YES')


(R, C) = map(int, input().split())
M = []
for r in range(R):
	row = list(input())
	for i in range(C):
		if row[i] == '.':
			row[i] = '0'
	M.append(row)
f = 1
for r in range(R):
	for c in range(C):
		if M[r][c] != '*':
			b = 0
			if r > 0 and M[r - 1][c] == '*':
				b += 1
			if r < R - 1 and M[r + 1][c] == '*':
				b += 1
			if c > 0 and M[r][c - 1] == '*':
				b += 1
			if c < C - 1 and M[r][c + 1] == '*':
				b += 1
			if r > 0 and c > 0 and (M[r - 1][c - 1] == '*'):
				b += 1
			if r < R - 1 and c < C - 1 and (M[r + 1][c + 1] == '*'):
				b += 1
			if r > 0 and c < C - 1 and (M[r - 1][c + 1] == '*'):
				b += 1
			if r < R - 1 and c > 0 and (M[r + 1][c - 1] == '*'):
				b += 1
			if b != int(M[r][c]):
				f = 0
if f == 0:
	print('NO')
else:
	print('YES')


(n, m) = map(int, input().split())
mat = [[-1 for j in range(m + 2)] for i in range(n + 2)]
for i in range(1, n + 1):
	q = input()
	for (j, x) in enumerate(q):
		mat[i][j + 1] = x
from sys import exit
for i in range(1, n + 1):
	for j in range(1, m + 1):
		if mat[i][j] != '*':
			k = 0
			if mat[i - 1][j - 1] == '*':
				k += 1
			if mat[i - 1][j] == '*':
				k += 1
			if mat[i - 1][j + 1] == '*':
				k += 1
			if mat[i][j - 1] == '*':
				k += 1
			if mat[i][j + 1] == '*':
				k += 1
			if mat[i + 1][j - 1] == '*':
				k += 1
			if mat[i + 1][j] == '*':
				k += 1
			if mat[i + 1][j + 1] == '*':
				k += 1
			if '0' < mat[i][j] < '9':
				if int(mat[i][j]) != k:
					print('NO')
					exit()
			if mat[i][j] == '.':
				if k != 0:
					print('NO')
					exit()
print('YES')


import sys, math
(n, m) = map(int, sys.stdin.readline().split())
arr = [list(sys.stdin.readline().rstrip()) for _ in range(n)]
dx = [0, 1, 0, -1, 1, 1, -1, -1]
dy = [1, 0, -1, 0, 1, -1, 1, -1]
for i in range(n):
	for j in range(m):
		if arr[i][j] == '.':
			arr[i][j] = '0'
for i in range(n):
	for j in range(m):
		if arr[i][j] != '*':
			temp = 0
			for k in range(8):
				(nx, ny) = (i + dx[k], j + dy[k])
				if 0 <= nx < n and 0 <= ny < m:
					if arr[nx][ny] == '*':
						temp += 1
			if temp != int(arr[i][j]):
				print('NO')
				sys.exit(0)
print('YES')


(n, m) = map(int, input().split())
T = [[] for i in range(n)]
for i in range(n):
	a = list(input())
	T[i] = a
flag = True
for i in range(n):
	if not flag:
		break
	for j in range(m):
		if T[i][j].isdigit():
			bom = 0
			for (dx, dy) in [(1, 0), (0, 1), (-1, 0), (0, -1), (1, 1), (-1, 1), (1, -1), (-1, -1)]:
				(nx, ny) = (i + dx, j + dy)
				if 0 <= nx < n and 0 <= ny < m and (T[nx][ny] == '*'):
					bom += 1
			if bom != int(T[i][j]):
				flag = False
				break
		if T[i][j] == '.':
			for (dx, dy) in [(1, 0), (0, 1), (-1, 0), (0, -1), (1, 1), (-1, 1), (1, -1), (-1, -1)]:
				(nx, ny) = (i + dx, j + dy)
				if 0 <= nx < n and 0 <= ny < m and (T[nx][ny] == '*'):
					flag = False
					break
if flag:
	print('YES')
else:
	print('NO')


a = []
(n, N) = list(map(int, input().split()))
for i in range(n):
	a.append(input())
for i in range(n):
	for j in range(N):
		m = (i and a[i - 1][j] == '*') + (j and a[i][j - 1] == '*') + (i < n - 1 and a[i + 1][j] == '*') + (j < N - 1 and a[i][j + 1] == '*') + (i and j and (a[i - 1][j - 1] == '*')) + (i < n - 1 and j < N - 1 and (a[i + 1][j + 1] == '*')) + (i and j < N - 1 and (a[i - 1][j + 1] == '*')) + (i < n - 1 and j and (a[i + 1][j - 1] == '*'))
		if a[i][j] == '.' and m != 0 or ('1' <= a[i][j] <= '8' and int(a[i][j]) != m):
			print('NO')
			return
print('YES')


(n, m) = (int(i) for i in input().split())
inputMatrix = [[0 for i in range(m)] for j in range(n)]
for i in range(n):
	inputMatrix[i] = list(input())
result = 'YES'
needToBreak = False
for i in range(n):
	if needToBreak:
		break
	for j in range(m):
		if needToBreak:
			break
		if inputMatrix[i][j] != '*':
			mineCounter = 0
			for di in range(-1, 2):
				for dj in range(-1, 2):
					neighborI = i + di
					neighborJ = j + dj
					if 0 <= neighborI < n and 0 <= neighborJ < m:
						if inputMatrix[neighborI][neighborJ] == '*':
							mineCounter += 1
			if inputMatrix[i][j] == '.' and mineCounter == 0:
				result = 'YES'
			elif inputMatrix[i][j] == '.' and mineCounter != 0:
				result = 'NO'
				needToBreak = True
			elif int(inputMatrix[i][j]) == mineCounter:
				result = 'YES'
			else:
				result = 'NO'
				needToBreak = True
print(result)


(n, m) = map(int, input().split())
l = [list(map(str, input())) for i in range(n)]
ch = 0
for i in range(n):
	for j in range(m):
		if l[i][j] == '.':
			if j - 1 >= 0:
				if l[i][j - 1] == '*':
					ch = 1
					print('NO')
					break
			if j + 1 <= m - 1:
				if l[i][j + 1] == '*':
					ch = 1
					print('NO')
					break
			if i - 1 >= 0:
				if l[i - 1][j] == '*':
					ch = 1
					print('NO')
					break
			if i + 1 <= n - 1:
				if l[i + 1][j] == '*':
					ch = 1
					print('NO')
					break
			if j - 1 >= 0 and i - 1 >= 0:
				if l[i - 1][j - 1] == '*':
					ch = 1
					print('NO')
					break
			if j - 1 >= 0 and i + 1 <= n - 1:
				if l[i + 1][j - 1] == '*':
					ch = 1
					print('NO')
					break
			if j + 1 <= m - 1 and i - 1 >= 0:
				if l[i - 1][j + 1] == '*':
					ch = 1
					print('NO')
					break
			if j + 1 <= m - 1 and i + 1 <= n - 1:
				if l[i + 1][j + 1] == '*':
					ch = 1
					print('NO')
					break
		elif l[i][j] != '*':
			co = int(l[i][j])
			if j - 1 >= 0:
				if l[i][j - 1] == '*':
					co -= 1
			if j + 1 <= m - 1:
				if l[i][j + 1] == '*':
					co -= 1
			if i - 1 >= 0:
				if l[i - 1][j] == '*':
					co -= 1
			if i + 1 <= n - 1:
				if l[i + 1][j] == '*':
					co -= 1
			if j - 1 >= 0 and i - 1 >= 0:
				if l[i - 1][j - 1] == '*':
					co -= 1
			if j - 1 >= 0 and i + 1 <= n - 1:
				if l[i + 1][j - 1] == '*':
					co -= 1
			if j + 1 <= m - 1 and i - 1 >= 0:
				if l[i - 1][j + 1] == '*':
					co -= 1
			if j + 1 <= m - 1 and i + 1 <= n - 1:
				if l[i + 1][j + 1] == '*':
					co -= 1
			if co != 0:
				ch = 1
				print('NO')
				break
	if ch:
		break
if not ch:
	print('YES')


(n, m) = map(int, input().split())
u = []
u.append([0] * (m + 2))
for i in range(1, n + 1):
	u.append([0] + list(input()) + [0])
	for j in range(1, m + 1):
		if u[i][j] == '.':
			u[i][j] = 0
		elif u[i][j] != '*':
			u[i][j] = int(u[i][j])
u.append([0] * (m + 2))

def bomb(i, j):
	ans = 0
	if u[i + 1][j + 1] == '*':
		ans += 1
	if u[i + 1][j] == '*':
		ans += 1
	if u[i + 1][j - 1] == '*':
		ans += 1
	if u[i][j - 1] == '*':
		ans += 1
	if u[i - 1][j - 1] == '*':
		ans += 1
	if u[i - 1][j] == '*':
		ans += 1
	if u[i - 1][j + 1] == '*':
		ans += 1
	if u[i][j + 1] == '*':
		ans += 1
	return ans
for i in range(1, n + 1):
	for j in range(1, m + 1):
		if u[i][j] != '*':
			if bomb(i, j) != u[i][j]:
				print('NO')
				exit()
print('YES')


(n, m) = list(map(int, input().split()))
f = [list(input()) for _ in range(n)]
ans = 'YES'
cnt = [[0 for _ in range(m)] for _ in range(n)]
for i in range(n):
	for j in range(m):
		if f[i][j] != '*':
			continue
		for di in range(-1, 2):
			for dj in range(-1, 2):
				if 0 < abs(di) + abs(dj) and 0 <= i + di < n and (0 <= j + dj < m):
					cnt[i + di][j + dj] += 1
for i in range(n):
	for j in range(m):
		if f[i][j] == '.':
			if cnt[i][j] != 0:
				ans = 'NO'
		elif f[i][j] != '*':
			if cnt[i][j] != int(f[i][j]):
				ans = 'NO'
print(ans)


(n, m) = map(int, input().split())
dx = [0, 0, -1, 1, -1, -1, 1, 1]
dy = [-1, 1, 0, 0, -1, 1, -1, 1]
g = []
for _ in range(n):
	g.append(input())
for i in range(n):
	for j in range(m):
		c = g[i][j]
		if c == '*':
			continue
		d = 0
		if c >= '1' and c <= '9':
			d = int(c)
		s = 0
		for k in range(8):
			x = i + dx[k]
			y = j + dy[k]
			if x >= 0 and y >= 0 and (x < n) and (y < m):
				if g[x][y] == '*':
					s += 1
		if s != d:
			print('NO')
			exit()
print('YES')


(n, m) = map(int, input().split())
d = (n + 2) * ['']
ans = [(m + 2) * [0] for t in range(n + 2)]
for t in range(1, n + 1):
	d[t] = 'q' + input() + 'q'
for t in range(1, n + 1):
	for i in range(1, m + 1):
		if d[t][i] == '*' and d[t][i] != '.':
			ans[t - 1][i - 1] += 1
			ans[t - 1][i] += 1
			ans[t - 1][i + 1] += 1
			ans[t][i - 1] += 1
			ans[t][i + 1] += 1
			ans[t + 1][i - 1] += 1
			ans[t + 1][i] += 1
			ans[t + 1][i + 1] += 1
flag = 1
for t in range(1, n + 1):
	for i in range(1, m + 1):
		if 49 <= ord(d[t][i]) <= 56:
			if int(d[t][i]) != ans[t][i]:
				print('NO')
				flag = 0
				break
		if d[t][i] == '.' and ans[t][i] > 0:
			print('NO')
			flag = 0
			break
	if flag == 0:
		break
if flag == 1:
	print('YES')


import sys
import math
import bisect

def solve(A):
	n = len(A)
	m = len(A[0])
	for i in range(n):
		for j in range(m):
			hint = -1
			if A[i][j] == '.':
				hint = 0
			elif A[i][j].isdigit():
				hint = ord(A[i][j]) - ord('0')
			if hint >= 0:
				cnt = 0
				for x in range(i - 1, i + 2):
					for y in range(j - 1, j + 2):
						if x >= 0 and x < n and (y >= 0) and (y < m) and (not (x == i and y == j)):
							if A[x][y] == '*':
								cnt += 1
				if cnt != hint:
					return False
	return True

def main():
	(n, m) = map(int, input().split())
	A = []
	for i in range(n):
		A.append(input())
	if solve(A):
		print('YES')
	else:
		print('NO')
main()


(n, m) = map(int, input().split())
s = []
for i in range(n):
	s.append(input())
if m == 1 and n == 1:
	if '1' <= s[0][0] <= '9':
		print('NO')
		exit()
	else:
		print('YES')
		exit()
if m == 1:
	for i in range(len(s)):
		if s[i][0] == '.':
			if i > 0 and i < n - 1 and (s[i + 1][0] == '*') or (i > 0 and i < n - 1 and (s[i - 1][0] == '*')):
				print('NO')
				exit()
			if i == 0 and s[i + 1][0] == '*':
				print('NO')
				exit()
			if i == n - 1 and s[i - 1][0] == '*':
				print('NO')
				exit()
		if s[i][0] == '2':
			if i == 0 or i == n - 1:
				print('NO')
				exit()
			if i > 0 and i < n - 1 and (s[i - 1][0] != '*') and (s[i + 1][0] != '*') or (i > 0 and i < n - 1 and (s[i - 1][0] != '*') and (s[i + 1][0] == '*')) or (i > 0 and i < n - 1 and (s[i - 1][0] == '*') and (s[i + 1][0] != '*')):
				print('NO')
				exit()
		if s[i][0] == '1':
			if i == 0 and s[i + 1][0] != '*':
				print('NO')
				exit()
			if i == n - 1 and s[i - 1][0] != '*':
				print('NO')
				exit()
			if i > 0 and i < n - 1 and (s[i + 1][0] != '*') and (s[i - 1][0] != '*') or (i > 0 and i < n - 1 and (s[i + 1][0] == '*') and (s[i - 1][0] == '*')):
				print('NO')
				exit()
		if '2' < s[i][0] <= '9':
			print('NO')
			exit()
if n == 1:
	for i in range(m):
		if s[0][i] == '.':
			if i > 0 and i < m - 1 and (s[0][i + 1] == '*') or (i > 0 and i < m - 1 and (s[0][i - 1] == '*')):
				print('NO')
				exit()
			if i == 0 and s[0][i + 1] == '*':
				print('NO')
				exit()
			if i == m - 1 and s[0][i - 1] == '*':
				print('NO')
				exit()
		if s[0][i] == '2':
			if i == 0 or i == m - 1:
				print('NO')
				exit()
			if i > 0 and i < m - 1 and (s[0][i - 1] != '*') or (i > 0 and i < m - 1 and (s[0][i + 1] != '*')):
				print('NO')
				exit()
		if s[0][i] == '1':
			if i == 0 and s[0][i + 1] != '*':
				print('NO')
				exit()
			if i == m - 1 and s[0][i - 1] != '*':
				print('NO')
				exit()
			if i > 0 and i < m - 1 and (s[0][i + 1] == '*') and (s[0][i - 1] == '*') or (i > 0 and i < m - 1 and (s[0][i + 1] != '*') and (s[0][i - 1] != '*')):
				print('NO')
				exit()
		if '2' < s[0][i] <= '9':
			print('NO')
			exit()
if m > 1 and n > 1:
	for i in range(n):
		for j in range(m):
			if '1' <= s[i][j] <= '9':
				count = 0
				if i == 0 and j == 0:
					if s[i][j + 1] == '*':
						count = count + 1
					if s[i + 1][j] == '*':
						count = count + 1
					if s[i + 1][j + 1] == '*':
						count = count + 1
				if i == 0 and j == m - 1:
					if s[i][j - 1] == '*':
						count = count + 1
					if s[i + 1][j] == '*':
						count = count + 1
					if s[i + 1][j - 1] == '*':
						count = count + 1
				if i == n - 1 and j == 0:
					if s[i - 1][j] == '*':
						count = count + 1
					if s[i][j + 1] == '*':
						count = count + 1
					if s[i - 1][j + 1] == '*':
						count = count + 1
				if i == n - 1 and j == m - 1:
					if s[i - 1][j] == '*':
						count = count + 1
					if s[i][j - 1] == '*':
						count = count + 1
					if s[i - 1][j - 1] == '*':
						count = count + 1
				if i == 0 and j > 0 and (j < m - 1):
					if s[i + 1][j] == '*':
						count = count + 1
					if s[i][j - 1] == '*':
						count = count + 1
					if s[i][j + 1] == '*':
						count = count + 1
					if s[i + 1][j - 1] == '*':
						count = count + 1
					if s[i + 1][j + 1] == '*':
						count = count + 1
				if i == n - 1 and j > 0 and (j < m - 1):
					if s[i][j - 1] == '*':
						count = count + 1
					if s[i][j + 1] == '*':
						count = count + 1
					if s[i - 1][j - 1] == '*':
						count = count + 1
					if s[i - 1][j + 1] == '*':
						count = count + 1
					if s[i - 1][j] == '*':
						count = count + 1
				if j == 0 and i > 0 and (i < n - 1):
					if s[i - 1][j] == '*':
						count = count + 1
					if s[i + 1][j] == '*':
						count = count + 1
					if s[i][j + 1] == '*':
						count = count + 1
					if s[i - 1][j + 1] == '*':
						count = count + 1
					if s[i + 1][j + 1] == '*':
						count = count + 1
				if j == m - 1 and i > 0 and (i < n - 1):
					if s[i - 1][j] == '*':
						count = count + 1
					if s[i + 1][j] == '*':
						count = count + 1
					if s[i][j - 1] == '*':
						count = count + 1
					if s[i - 1][j - 1] == '*':
						count = count + 1
					if s[i + 1][j - 1] == '*':
						count = count + 1
				if i > 0 and i < n - 1 and (j > 0) and (j < m - 1):
					if s[i - 1][j] == '*':
						count = count + 1
					if s[i + 1][j] == '*':
						count = count + 1
					if s[i][j + 1] == '*':
						count = count + 1
					if s[i][j - 1] == '*':
						count = count + 1
					if s[i - 1][j - 1] == '*':
						count = count + 1
					if s[i - 1][j + 1] == '*':
						count = count + 1
					if s[i + 1][j - 1] == '*':
						count = count + 1
					if s[i + 1][j + 1] == '*':
						count = count + 1
				if count != int(s[i][j]):
					print('NO')
					exit()
			if s[i][j] == '.':
				if i == 0 and j == 0:
					if s[i + 1][j] == '*' or s[i][j + 1] == '*' or s[i + 1][j + 1] == '*':
						print('NO')
						exit()
				if i == 0 and j == m - 1:
					if s[i + 1][j] == '*' or s[i][j - 1] == '*' or s[i + 1][j - 1] == '*':
						print('NO')
						exit()
				if i == n - 1 and j == 0:
					if s[i - 1][j] == '*' or s[i][j + 1] == '*' or s[i - 1][j + 1] == '*':
						print('NO')
						exit()
				if i == n - 1 and j == m - 1:
					if s[i - 1][j] == '*' or s[i][j - 1] == '*' or s[i - 1][j - 1] == '*':
						print('NO')
						exit()
				if i == 0 and j > 0 and (j < m - 1):
					if s[i][j + 1] == '*' or s[i][j - 1] == '*' or s[i + 1][j] == '*' or (s[i + 1][j - 1] == '*') or (s[i + 1][j + 1] == '*'):
						print('NO')
						exit()
				if i == n - 1 and j > 0 and (j < m - 1):
					if s[i][j - 1] == '*' or s[i][j + 1] == '*' or s[i - 1][j] == '*' or (s[i - 1][j - 1] == '*') or (s[i - 1][j + 1] == '*'):
						print('NO')
						exit()
				if j == 0 and i > 0 and (i < n - 1):
					if s[i - 1][j] == '*' or s[i + 1][j] == '*' or s[i][j + 1] == '*' or (s[i - 1][j + 1] == '*') or (s[i + 1][j + 1] == '*'):
						print('NO')
						exit()
				if j == m - 1 and i > 0 and (i < n - 1):
					if s[i - 1][j] == '*' or s[i + 1][j] == '*' or s[i][j - 1] == '*' or (s[i - 1][j - 1] == '*') or (s[i + 1][j - 1] == '*'):
						print('NO')
						exit()
				if i > 0 and i < n - 1 and (j > 0) and (j < m - 1):
					if s[i][j - 1] == '*' or s[i][j + 1] == '*' or s[i - 1][j] == '*' or (s[i + 1][j] == '*') or (s[i - 1][j - 1] == '*') or (s[i - 1][j + 1] == '*') or (s[i + 1][j - 1] == '*') or (s[i + 1][j + 1] == '*'):
						print('NO')
						exit()
print('YES')


(n, m) = [int(i) for i in input().split()]
a = []
a.append((m + 2) * '0')
for i in range(n):
	a.append('0' + input().replace('.', '0') + '0')
a.append((m + 2) * '0')

def tu(n, m, a):
	t = 0
	for i in range(n - 1, n + 2):
		for j in range(m - 1, m + 2):
			if a[i][j] == '*':
				t += 1
	return int(a[n][m]) == t
f = 'YES'
for i in range(1, n + 1):
	for j in range(1, m + 1):
		if a[i][j] != '*':
			z = tu(i, j, a)
			if z == False:
				f = 'NO'
				break
	if f == 'NO':
		break
print(f)


arr = []

def valid(i, j):
	bombs = 0
	if arr[i][j] == '*':
		return True
	if i != len(arr) - 1:
		if arr[i + 1][j] == '*':
			bombs += 1
	if i != 0:
		if arr[i - 1][j] == '*':
			bombs += 1
	if j != len(arr[0]) - 1:
		if arr[i][j + 1] == '*':
			bombs += 1
	if j != 0:
		if arr[i][j - 1] == '*':
			bombs += 1
	if i != 0 and j != 0:
		if arr[i - 1][j - 1] == '*':
			bombs += 1
	if i != 0 and j != len(arr[0]) - 1:
		if arr[i - 1][j + 1] == '*':
			bombs += 1
	if i != len(arr) - 1 and j != 0:
		if arr[i + 1][j - 1] == '*':
			bombs += 1
	if i != len(arr) - 1 and j != len(arr[0]) - 1:
		if arr[i + 1][j + 1] == '*':
			bombs += 1
	if bombs == 0 and arr[i][j] == '.':
		return True
	if arr[i][j] == '.':
		return False
	if int(arr[i][j]) == bombs:
		return True
	return False
(n, m) = list(map(int, input().split()))
for i in range(n):
	line = input()
	arr2 = []
	for j in range(m):
		arr2.append(line[j:j + 1])
	arr.append(arr2)
ans = 'YES'
for i in range(n):
	for j in range(m):
		if not valid(i, j):
			ans = 'NO'
print(ans)


def hasAtLeastOneNonBombNeighbor(i, j, field):
	ret = True
	ret &= checkInstance(i - 1, j - 1, field, '*')
	ret &= checkInstance(i, j - 1, field, '*')
	ret &= checkInstance(i - 1, j, field, '*')
	ret &= checkInstance(i - 1, j + 1, field, '*')
	ret &= checkInstance(i + 1, j - 1, field, '*')
	ret &= checkInstance(i, j + 1, field, '*')
	ret &= checkInstance(i + 1, j, field, '*')
	ret &= checkInstance(i + 1, j + 1, field, '*')
	if ret == True:
		return False
	return True

def hasBombNeighbor(i, j, field):
	ret = False
	ret |= checkInstance(i - 1, j - 1, field, '*')
	ret |= checkInstance(i, j - 1, field, '*')
	ret |= checkInstance(i - 1, j, field, '*')
	ret |= checkInstance(i - 1, j + 1, field, '*')
	ret |= checkInstance(i + 1, j - 1, field, '*')
	ret |= checkInstance(i, j + 1, field, '*')
	ret |= checkInstance(i + 1, j, field, '*')
	ret |= checkInstance(i + 1, j + 1, field, '*')
	if ret == True:
		return True
	return False

def hasCorrectNumberOfNeighborBombs(i, j, field, num):
	if checkInstance(i - 1, j - 1, field, '*'):
		num -= 1
	if checkInstance(i, j - 1, field, '*'):
		num -= 1
	if checkInstance(i - 1, j, field, '*'):
		num -= 1
	if checkInstance(i - 1, j + 1, field, '*'):
		num -= 1
	if checkInstance(i + 1, j - 1, field, '*'):
		num -= 1
	if checkInstance(i, j + 1, field, '*'):
		num -= 1
	if checkInstance(i + 1, j, field, '*'):
		num -= 1
	if checkInstance(i + 1, j + 1, field, '*'):
		num -= 1
	if num == 0:
		return True
	return False

def checkInstance(i, j, field, charToCheck):
	if field[i][j] == charToCheck:
		return True
	return False

def doesWork(i, j, field):
	if field[i][j] == '*':
		return True
	if field[i][j] == '.':
		return not hasBombNeighbor(i, j, field)
	if field[i][j] == 'x':
		return True
	else:
		return hasCorrectNumberOfNeighborBombs(i, j, field, int(field[i][j]))

def isCorrect(field):
	for i in range(1, n + 1):
		for j in range(1, m + 1):
			if not doesWork(i, j, field):
				return False
	return True
(n, m) = [int(k) for k in input().split(' ')]
field = []
field.append(['x'] * (m + 2))
for i in range(n):
	temp = ['x']
	temp.extend(list(input()))
	temp.append('x')
	field.append(temp)
field.append(['x'] * (m + 2))
if isCorrect(field):
	print('YES')
else:
	print('NO')


def searchB(arr, x, y):
	nBombs = 0
	k = x
	k2 = y
	j = 0
	while j < 8:
		if j == 0:
			k = x + 1
			k2 = y + 1
		elif j == 1:
			k = x + 1
			k2 = y
		elif j == 2:
			k = x + 1
			k2 = y - 1
		elif j == 2:
			k = x + 1
			k2 = y - 1
		elif j == 3:
			k = x
			k2 = y - 1
		elif j == 4:
			k = x - 1
			k2 = y - 1
		elif j == 5:
			k = x - 1
			k2 = y
		elif j == 6:
			k = x - 1
			k2 = y + 1
		elif j == 7:
			k = x
			k2 = y + 1
		if 0 <= k and k < m and (0 <= k2) and (k2 < n):
			if arr[k2][k] == '*':
				nBombs += 1
		j += 1
	return nBombs
(n, m) = map(int, input().rstrip().split())
k = 0
ret = True
field = []
lis = []
while k < n:
	field.append(input().replace('.', '0'))
	k += 1
i = 0
while i < n:
	t = 0
	while t < m:
		if field[i][t].isdigit():
			a = str(searchB(field, t, i))
			if a != field[i][t]:
				ret = False
				break
		t += 1
	i += 1
if ret == True:
	print('YES')
else:
	print('NO')


Q = [int(x) for x in input().split()]
N = Q[0]
M = Q[1]
B = []
for i in range(N):
	B.append(list(input()))
C = [[0 for _ in range(M)] for i in range(N)]
for i in range(N):
	for j in range(M):
		if B[i][j] != '.' and B[i][j] != '*':
			C[i][j] += int(B[i][j])
		elif B[i][j] == '*':
			for i1 in range(max(0, i - 1), min(i + 2, N)):
				for j1 in range(max(0, j - 1), min(j + 2, M)):
					C[i1][j1] -= 1
ok = 1
for i in range(N):
	for j in range(M):
		if B[i][j] != '*' and C[i][j] != 0:
			ok = 0
			break
if ok == 1:
	print('YES')
else:
	print('NO')


def bad_num(k):
	tb = 0
	for x in [-1, 0, 1]:
		for y in [-1, 0, 1]:
			if pole[i + x][j + y] == '*':
				tb += 1
	return tb == k
(n, m) = list(map(int, input().split()))
pole = []
for i in range(n):
	arr = list(input().strip())
	arr.append('.')
	pole.append(arr)
pole.append('.' * (m + 1))
for i in range(n):
	for j in range(m):
		cur = pole[i][j]
		if cur == '.':
			cur = '0'
		if cur in '0123456789':
			if bad_num(int(cur)):
				continue
			else:
				print('NO')
				exit()
		elif cur != '*':
			print('NO')
			exit()
print('YES')


(n, m) = map(int, input().split())
arr = []
for _ in range(n):
	x = input()
	x = list(x)
	arr.append(x)
flg = 1
X = n
Y = m
neighbors = lambda x, y: [(x2, y2) for x2 in range(x - 1, x + 2) for y2 in range(y - 1, y + 2) if -1 < x < X and -1 < y < Y and (x != x2 or y != y2) and (0 <= x2 < X) and (0 <= y2 < Y)]
for i in range(n):
	for j in range(m):
		if arr[i][j] == '.':
			for k in neighbors(i, j):
				if arr[k[0]][k[1]] == '*':
					print('NO')
					flg = 0
					break
			if flg == 0:
				break
		if arr[i][j].isdigit():
			count = 0
			for k in neighbors(i, j):
				if arr[k[0]][k[1]] == '*':
					count += 1
			if count != int(arr[i][j]):
				print('NO')
				flg = 0
				break
			if flg == 0:
				break
	if flg == 0:
		break
if flg == 1:
	print('YES')


(n, m) = [int(i) for i in input().split()]
a = []
for i in range(n):
	a.append(input())
l = []
for i in range(n):
	b = []
	for j in range(m):
		if a[i][j] == '*':
			b.append('*')
		else:
			b.append(0)
	l.append(b)
for i in range(n):
	for j in range(m):
		if l[i][j] == '*':
			if j + 1 < m:
				if l[i][j + 1] != '*':
					l[i][j + 1] += 1
			if i + 1 < n:
				if l[i + 1][j] != '*':
					l[i + 1][j] += 1
			if j > 0:
				if l[i][j - 1] != '*':
					l[i][j - 1] += 1
			if i > 0:
				if l[i - 1][j] != '*':
					l[i - 1][j] += 1
			if i > 0 and j > 0:
				if l[i - 1][j - 1] != '*':
					l[i - 1][j - 1] += 1
			if i + 1 < n and j + 1 < m:
				if l[i + 1][j + 1] != '*':
					l[i + 1][j + 1] += 1
			if i > 0 and j + 1 < m:
				if l[i - 1][j + 1] != '*':
					l[i - 1][j + 1] += 1
			if i + 1 < n and j > 0:
				if l[i + 1][j - 1] != '*':
					l[i + 1][j - 1] += 1
c = []
for i in range(n):
	k = ''
	for j in range(m):
		if l[i][j] == 0:
			k += '.'
		else:
			k += str(l[i][j])
	c.append(k)
if c == a:
	print('Yes')
else:
	print('No')


(n, m) = [int(i) for i in input().split()]
field = []
bomb = []
for i in range(n):
	field.append([])
	data = input()
	count = 0
	for j in data:
		if j == '.':
			j = '0'
		if j == '*':
			bomb.append([i, count])
			j = '-1'
		field[i].append(int(j))
		count += 1
ans = []
for i in range(n):
	ans.append([])
	for j in range(m):
		ans[i].append(0)
directx = [-1, -1, -1, 0, 0, 0, 1, 1, 1]
directy = [-1, 0, 1, -1, 0, 1, -1, 0, 1]
for i in bomb:
	x = i[0]
	y = i[1]
	ans[x][y] = -1
	for j in range(9):
		x1 = directx[j] + x
		y1 = directy[j] + y
		if 0 <= x1 < n and 0 <= y1 < m:
			if ans[x1][y1] != -1:
				ans[x1][y1] += 1
for i in range(n):
	for j in range(m):
		if ans[i][j] != field[i][j]:
			print('NO')
			quit()
print('YES')


(n, m) = map(int, input().split())
a = [input() for _ in range(n)]
ans = 'YES'
for i in range(n):
	for j in range(m):
		if a[i][j] != '*':
			s = sum((0 <= k < n and 0 <= l < m and (a[k][l] == '*') for k in range(i - 1, i + 2) for l in range(j - 1, j + 2)))
			if str(s or '.') != a[i][j]:
				ans = 'NO'
				break
	else:
		continue
	break
print(ans)


def check_bombs(matrix, i, j):
	counter = 0
	for new_position in [(0, 1), (0, -1), (1, 0), (-1, 0), (-1, -1), (-1, 1), (1, -1), (1, 1)]:
		node_position = (i + new_position[0], j + new_position[1])
		if node_position[0] < 0 or node_position[0] > len(matrix) - 1 or node_position[1] < 0 or (node_position[1] > len(matrix[node_position[0]]) - 1):
			continue
		if matrix[node_position[0]][node_position[1]] == '*':
			counter += 1
	if int(matrix[i][j]) != counter:
		return True

def check_empty(matrix, i, j):
	for new_position in [(0, 1), (0, -1), (1, 0), (-1, 0), (-1, -1), (-1, 1), (1, -1), (1, 1)]:
		node_position = (i + new_position[0], j + new_position[1])
		if node_position[0] < 0 or node_position[0] > len(matrix) - 1 or node_position[1] < 0 or (node_position[1] > len(matrix[node_position[0]]) - 1):
			continue
		if matrix[node_position[0]][node_position[1]] == '*':
			return True

def mine_sweper():
	length = list(map(int, input().split()))
	matrix = [input() for i in range(length[0])]
	for i in range(len(matrix)):
		for j in range(len(matrix[i])):
			if matrix[i][j] == '.':
				if check_empty(matrix, i, j):
					return 'NO'
			elif matrix[i][j] == '*':
				continue
			elif check_bombs(matrix, i, j):
				return 'NO'
	return 'YES'
print(mine_sweper())


def solve(N, M, board):
	for i in range(0, N):
		for j in range(0, M):
			if not analyse(i, j, N, M):
				print('NO')
				return
	print('YES')

def analyse(x, y, N, M):
	value = board[x][y]
	if value == '*':
		return True
	count = value
	if value == '.':
		count = '0'
	count = int(count)
	mines = 0
	neighbours = [-1, 0, 1]
	for xn in neighbours:
		for yn in neighbours:
			if xn == 0 and yn == 0:
				continue
			xNext = x + xn
			yNext = y + yn
			if isValidCell(xNext, yNext, N, M):
				if board[xNext][yNext] == '*':
					mines += 1
	return count == mines

def isValidCell(x, y, N, M):
	if x < 0 or x >= N or y < 0 or (y >= M):
		return False
	return True
(N, M) = [int(x) for x in input().split(' ')]
board = []
for i in range(0, N):
	board.append([c for c in input()])
solve(N, M, board)


(m, n) = list(map(int, input().split()))
field = []
for i in range(m):
	field.append(input())

def calculate(x, y):
	if field[x][y] != '*':
		bombs = 0
		for xx in [-1, 0, 1]:
			for yy in [-1, 0, 1]:
				if x + xx >= 0 and y + yy >= 0:
					try:
						if xx == 0 and yy == 0:
							pass
						else:
							v = field[x + xx][y + yy]
							if v == '*':
								bombs += 1
					except:
						pass
		if field[x][y] == '.' and bombs == 0:
			return True
		elif field[x][y] == '.':
			return False
		else:
			return int(field[x][y]) == bombs
	else:
		return True
status = True
for i in range(m):
	for j in range(n):
		if not calculate(i, j):
			status = False
			break
	if not status:
		break
if status:
	print('YES')
else:
	print('NO')


def bomb(i, j):
	num = 0
	for di in range(-1, 2):
		for dj in range(-1, 2):
			if di != 0 or di != dj:
				(x, y) = (i + di, j + dj)
				if x >= 0 and x < n and (y >= 0) and (y < m) and (matrix[x][y] == '*'):
					num += 1
	return num
(n, m) = map(int, input().split())
matrix = []
for i in range(n):
	matrix.append(list(input()))
ans = 'YES'
for i in range(n):
	for j in range(m):
		if matrix[i][j] == '.':
			count = bomb(i, j)
			if count != 0:
				ans = 'NO'
				break
		elif matrix[i][j] != '.' and matrix[i][j] != '*':
			count = bomb(i, j)
			if count != int(matrix[i][j]):
				ans = 'NO'
				break
	if ans == 'NO':
		break
print(ans)


def count_bomb(a, list1):
	if a[list1[0]][list1[1]] == '*':
		return 1
	return 0

def check_valid(a, n, m):
	for i in range(n):
		for j in range(m):
			count = 0
			left = []
			right = []
			top = []
			bottom = []
			topleft = []
			bottomright = []
			bottomleft = []
			topright = []
			if j - 1 >= 0:
				left = [i, j - 1]
			if j + 1 < m:
				right = [i, j + 1]
			if i - 1 >= 0:
				top = [i - 1, j]
			if i + 1 < n:
				bottom = [i + 1, j]
			if i - 1 >= 0 and j - 1 >= 0:
				topleft = [i - 1, j - 1]
			if i + 1 < n and j + 1 < m:
				bottomright = [i + 1, j + 1]
			if i + 1 < n and j - 1 >= 0:
				bottomleft = [i + 1, j - 1]
			if i - 1 >= 0 and j + 1 < m:
				topright = [i - 1, j + 1]
			for x in [left, right, top, bottom, topleft, bottomright, bottomleft, topright]:
				if x != []:
					count += count_bomb(a, x)
			if a[i][j] == '.' and count != 0:
				return False
			if a[i][j] != '.' and a[i][j] != '*' and (int(a[i][j]) != count):
				return False
	return True
(n, m) = list(map(int, input().split(' ')))
a = []
for i in range(n):
	a.append(input())
if check_valid(a, n, m):
	print('YES')
else:
	print('NO')


def getIntList():
	return list(map(int, input().split()))

def getTransIntList(n):
	first = getIntList()
	m = len(first)
	result = [[0] * n for _ in range(m)]
	for i in range(m):
		result[i][0] = first[i]
	for j in range(1, n):
		curr = getIntList()
		for i in range(m):
			result[i][j] = curr[i]
	return result
(n, m) = getIntList()
a = [[-1 for j in range(m)] for i in range(n)]
b = [[0 for j in range(m)] for i in range(n)]
for i in range(n):
	s = input()
	for (j, l) in enumerate(s):
		if l == '.':
			a[i][j] = 0
		elif l == '*':
			b[i][j] = -1
			for i1 in range(i - 1, i + 2):
				for j1 in range(j - 1, j + 2):
					if i == i1 and j == j1:
						continue
					if 0 <= i1 < n and 0 <= j1 < m:
						b[i1][j1] += 1
		else:
			a[i][j] = int(l)

def check(a, b):
	for i in range(n):
		for j in range(m):
			if a[i][j] != b[i][j] and a[i][j] >= 0:
				return 'NO'
	return 'YES'
print(check(a, b))


(m, n) = map(int, input().split())
arr = []
err = False
for case in range(m):
	arr.append([s for s in input()])

def judge(arr1, i, j):
	res = 0
	temp_arr = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
	for x in temp_arr:
		ii = i + int(x[0])
		jj = j + int(x[1])
		if 0 <= ii < m and 0 <= jj < n and (arr1[ii][jj] == '*'):
			res += 1
	return res
for i in range(m):
	for j in range(n):
		if arr[i][j] == '*':
			continue
		if arr[i][j] == '.':
			if judge(arr, i, j) != 0:
				err = True
				break
		else:
			x = int(arr[i][j])
			if judge(arr, i, j) != x:
				err = True
				break
if err:
	print('NO')
else:
	print('YES')


xy = [int(i) for i in input().split()]
mineMap = [[0] * xy[1] for i in range(xy[0])]
myMineMap = [[0] * xy[1] for i in range(xy[0])]
flag = True
posArr = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]]

def addMine(i, o):
	mineMap[i][o] = -1
	myMineMap[i][o] = -1
	for pos in posArr:
		if i + pos[0] < 0 or i + pos[0] >= xy[0] or o + pos[1] < 0 or (o + pos[1] >= xy[1]):
			continue
		if myMineMap[i + pos[0]][o + pos[1]] != -1:
			myMineMap[i + pos[0]][o + pos[1]] += 1

def addEmpty(i, o):
	if myMineMap[i][o] != 0:
		global flag
		flag = False

def addNum(i, o, value):
	mineMap[i][o] = value
for i in range(xy[0]):
	string = input()
	for o in range(xy[1]):
		if string[o] == '*':
			addMine(i, o)
		elif string[o] == '.':
			addEmpty(i, o)
		else:
			addNum(i, o, int(string[o]))

def check():
	for i in range(xy[0]):
		if str(mineMap[i]) != str(myMineMap[i]):
			global flag
			flag = False
check()
if flag:
	print('YES')
else:
	print('NO')


(r, c) = list(map(int, input().split()))
b = [list(input()) for i in range(r)]
for y in range(r):
	for x in range(c):
		if b[y][x] == '.':
			b[y][x] = '0'
ok = True
for Y in range(r):
	for X in range(c):
		if not b[Y][X].isdigit():
			continue
		p = 0
		for dy in range(-1, 2):
			for dx in range(-1, 2):
				y = Y + dy
				x = X + dx
				if 0 <= y < r and 0 <= x < c:
					p += b[y][x] == '*'
		if p != int(b[Y][X]):
			ok = False
print(['NO', 'YES'][ok])


(n, m) = list(map(int, input().split()))
l = []
for _ in range(n):
	a = list((i if i != '.' else 0 for i in input()))
	l.append(a)
flag = 1
for i in range(n):
	for j in range(m):
		if l[i][j] != '*':
			count = 0
			if i - 1 >= 0:
				for x in range(3):
					if x + j - 1 >= 0 and x + j - 1 < m:
						if l[i - 1][x + j - 1] == '*':
							count += 1
			if j - 1 >= 0:
				if l[i][j - 1] == '*':
					count += 1
			if j + 1 < m:
				if l[i][j + 1] == '*':
					count += 1
			if i + 1 < n:
				for x in range(3):
					if x + j - 1 >= 0 and x + j - 1 < m:
						if l[i + 1][x + j - 1] == '*':
							count += 1
			if count != int(l[i][j]):
				flag = 0
				break
	if flag == 0:
		break
print('YES' if flag == 1 else 'NO')


alongx = [0, 1, -1, 0, 1, -1, -1, 1]
alongy = [-1, 0, 0, 1, -1, -1, 1, 1]
(row, column) = map(int, input().split())
mineSet = [input() for m in range(row)]
for r in range(row):
	for c in range(column):
		if mineSet[r][c] == '*':
			continue
		else:
			count = 0
			for s in range(8):
				(x, y) = (r + alongx[s], c + alongy[s])
				if x < 0 or x >= row or y >= column or (y < 0):
					continue
				elif mineSet[x][y] == '*':
					count += 1
			if count != (0 if mineSet[r][c] == '.' else int(mineSet[r][c])):
				print('No')
				exit(0)
print('Yes')


[n, m] = map(int, input().strip().split())
field = [input().strip() for _ in range(n)]

def count3(s, m):
	res = []
	for j in range(m):
		jl = max(j - 1, 0)
		jr = min(j + 1, m - 1)
		res.append(s[jl:jr + 1].count('*'))
	return res

def countr(A, n, m):
	res = [[0 for j in range(m)] for i in range(n)]
	for i in range(n):
		il = max(i - 1, 0)
		ir = min(i + 1, n - 1)
		for j in range(m):
			res[i][j] = sum((A[ii][j] for ii in range(il, ir + 1)))
	return res
cst = [count3(s, m) for s in field]
res = countr(cst, n, m)

def verify(field, A, n, m):
	for i in range(n):
		for j in range(m):
			if field[i][j] == '*':
				continue
			elif field[i][j] == '.':
				if A[i][j] != 0:
					return 'NO'
			elif int(field[i][j]) != A[i][j]:
				return 'NO'
	return 'YES'
ans = verify(field, res, n, m)
print(ans)


from sys import exit
(M, N) = list(map(int, input().strip().split()))
ls = []
for i in range(M):
	ls.append(list(input()))

def neighbours(x, y):
	return [ls[x2][y2] for x2 in range(x - 1, x + 2) for y2 in range(y - 1, y + 2) if -1 < x < M and -1 < y < N and (x != x2 or y != y2) and (-1 < x2 < M) and (-1 < y2 < N)]
for i in range(M):
	for j in range(N):
		if ls[i][j].isnumeric() or ls[i][j] == '.':
			if ls[i][j] == '.':
				ls[i][j] = '0'
			bomb = 0
			for cell in neighbours(i, j):
				if cell == '*':
					bomb += 1
			if bomb != int(ls[i][j]):
				print('NO')
				exit()
print('YES')


import sys
(n, m) = map(int, sys.stdin.readline().split())
matrix = [list(sys.stdin.readline().strip()) for _ in range(n)]
new_matrix = [['*' if i == '*' else 0 for i in x] for x in matrix]

def valid(i, j):
	return 0 <= i < n and 0 <= j < m

def update(i, j):
	for x in range(i - 1, i + 2):
		for y in range(j - 1, j + 2):
			if valid(x, y) and new_matrix[x][y] != '*':
				new_matrix[x][y] += 1
for i in range(n):
	for j in range(m):
		if matrix[i][j] == '*':
			update(i, j)
for i in range(n):
	for j in range(m):
		if new_matrix[i][j] == 0:
			new_matrix[i][j] = '.'
		elif new_matrix != '*':
			new_matrix[i][j] = str(new_matrix[i][j])
g = 0
for i in range(n):
	for j in range(m):
		if new_matrix[i][j] != matrix[i][j]:
			g = 1
			break
if g:
	print('NO')
else:
	print('YES')


(n, m) = map(int, input().split())
a = []
for i in range(n):
	a.append(input())
tmp = []
tmp += ['.' * (m + 2)]
for k in a:
	tmp += ['.' + k + '.']
tmp += ['.' * (m + 2)]
for i in range(1, n + 1):
	for j in range(1, m + 1):
		if tmp[i][j] == '.':
			if tmp[i][j + 1] == '*' or tmp[i][j - 1] == '*' or tmp[i + 1][j] == '*' or (tmp[i - 1][j] == '*') or (tmp[i + 1][j + 1] == '*') or (tmp[i + 1][j - 1] == '*') or (tmp[i - 1][j + 1] == '*') or (tmp[i - 1][j - 1] == '*'):
				print('NO')
				exit()
for i in range(1, n + 1):
	for j in range(1, m + 1):
		cnt = 0
		if tmp[i][j] != '*' and tmp[i][j] != '.':
			if tmp[i][j + 1] == '*':
				cnt += 1
			if tmp[i][j - 1] == '*':
				cnt += 1
			if tmp[i + 1][j] == '*':
				cnt += 1
			if tmp[i - 1][j] == '*':
				cnt += 1
			if tmp[i + 1][j + 1] == '*':
				cnt += 1
			if tmp[i + 1][j - 1] == '*':
				cnt += 1
			if tmp[i - 1][j + 1] == '*':
				cnt += 1
			if tmp[i - 1][j - 1] == '*':
				cnt += 1
			if cnt != int(tmp[i][j]):
				print('NO')
				exit()
print('YES')


(n, m) = input().split(' ')
(n, m) = (int(n), int(m))
a = []
check = [(-1, -1), (-1, 0), (0, -1), (0, 1), (1, 0), (1, 1), (1, -1), (-1, 1)]

def near(i, j):
	cnt = 0
	for x in range(len(check)):
		(A, B) = (i + check[x][0], j + check[x][1])
		if 0 <= A < n and 0 <= B < m and (a[A][B] == '*'):
			cnt += 1
	return cnt
for i in range(n):
	a.append(input().replace('.', '0'))
for i in range(n):
	for j in range(m):
		if a[i][j] and a[i][j].isdigit() and (near(i, j) != int(a[i][j])):
			print('NO')
			exit()
print('YES')


(n, m) = map(int, input().split())
board = [list(input()) for _ in range(n)]
ns = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
valid = True
for (x, row) in enumerate(board):
	for (y, c) in enumerate(row):
		b = 0
		for (dx, dy) in ns:
			(nx, ny) = (x + dx, y + dy)
			if 0 <= nx < len(board) and 0 <= ny < len(row):
				if board[nx][ny] == '*':
					b += 1
		xx = '.' if b == 0 else str(b)
		if c != '*' and xx != c:
			valid = False
print('YES' if valid else 'NO')


(n, m) = map(int, input().split(' '))
field = []
field.append(list('.' * (m + 2)))
for _ in range(n):
	field.append(list('.' + input() + '.'))
field.append(list('.' * (m + 2)))

def check(i, j):
	global field
	ctr = 0
	if field[i - 1][j - 1] == '*':
		ctr += 1
	if field[i - 1][j] == '*':
		ctr += 1
	if field[i - 1][j + 1] == '*':
		ctr += 1
	if field[i][j - 1] == '*':
		ctr += 1
	if field[i][j + 1] == '*':
		ctr += 1
	if field[i + 1][j - 1] == '*':
		ctr += 1
	if field[i + 1][j] == '*':
		ctr += 1
	if field[i + 1][j + 1] == '*':
		ctr += 1
	return ctr
flag = True
flagoo = True
for i in range(1, n + 1):
	if flagoo == False:
		break
	for j in range(1, m + 1):
		if field[i][j] == '.':
			x = check(i, j)
			if x != 0:
				print('NO')
				flag = False
				flagoo = False
				break
		elif field[i][j] == '*':
			ll = 1
		elif check(i, j) != int(field[i][j]):
			print('NO')
			flag = False
			flagoo = False
			break
if flag:
	print('YES')


(n, m) = [int(x) for x in input().split()]
a = ['.' for i in range(n + 2) for j in range(m + 2)]

def f(i, j, d):
	c = 0
	if a[i * (m + 2) + j - 1] == '*':
		c += 1
	if a[i * (m + 2) + j + 1] == '*':
		c += 1
	if a[(i + 1) * (m + 2) + j] == '*':
		c += 1
	if a[(i - 1) * (m + 2) + j] == '*':
		c += 1
	if a[(i + 1) * (m + 2) + j - 1] == '*':
		c += 1
	if a[(i - 1) * (m + 2) + j + 1] == '*':
		c += 1
	if a[(i + 1) * (m + 2) + j + 1] == '*':
		c += 1
	if a[(i - 1) * (m + 2) + j - 1] == '*':
		c += 1
	if c == d:
		return True
	else:
		return False

def fs(n, m, a):
	for i in range(1, n + 1):
		a[(m + 2) * i + 1:(m + 2) * (i + 1) - 1] = input()
	for i in range(1, n + 1):
		for j in range(1, m + 1):
			if a[i * (m + 2) + j].isdigit():
				d = int(a[i * (m + 2) + j])
			elif a[i * (m + 2) + j] == '.':
				d = 0
			if not a[i * (m + 2) + j] == '*' and (not f(i, j, d)):
				print('NO')
				return
	print('YES')
fs(n, m, a)


from sys import stdin
(n, m) = map(int, stdin.readline().strip().split())
field = []
for i in range(n):
	field.append(stdin.readline().strip())

def neighboring_bombs(field, n, m, i, j):
	count = 0
	for ii in range(max(i - 1, 0), min(i + 2, n)):
		for jj in range(max(j - 1, 0), min(j + 2, m)):
			if ii == i and jj == j:
				continue
			if field[ii][jj] == '*':
				count += 1
	return count

def valid(field, n, m):
	for i in range(n):
		for j in range(m):
			if field[i][j] == '*':
				continue
			if field[i][j] == '.':
				expect_bombs = 0
			else:
				expect_bombs = ord(field[i][j]) - ord('0')
			actual_bombs = neighboring_bombs(field, n, m, i, j)
			if actual_bombs != expect_bombs:
				return False
	return True
if valid(field, n, m):
	print('YES')
else:
	print('NO')


(n, m) = list(map(int, input().strip().split()))
nums = []
for i in range(n):
	row = input().strip()
	nums.append(row)
ok = True
for i in range(n):
	for j in range(m):
		if nums[i][j] == '*':
			continue
		cur = 0
		for x in range(-1, 2):
			for y in range(-1, 2):
				if x == 0 and y == 0:
					continue
				(new_i, new_j) = (i + x, j + y)
				if new_i < 0 or new_i >= n or new_j < 0 or (new_j >= m):
					continue
				if nums[new_i][new_j] == '*':
					cur += 1
		gt = 0
		if nums[i][j] != '.':
			gt = int(nums[i][j])
		if cur != gt:
			ok = False
			break
if ok:
	print('YES')
else:
	print('NO')


(a, b) = [int(x) for x in input().split()]
f = [0] * a
t = f * b
alpha = []
beta = []
for i in range(a):
	p = input()
	for s in range(b):
		if p[s] == '.':
			t[i * b + s] = 0
			beta = beta + [i * b + s]
		elif p[s] == '*':
			t[i * b + s] = 0
			alpha = alpha + [i * b + s]
		else:
			t[i * b + s] = int(p[s])

def pre(s, m):
	q = 1
	if a == 1:
		ll = [1]
	else:
		ll = [0, 1, 2]
	if b == 1:
		lll = [1]
	elif (s + 1) % b == 0:
		lll = [0, 1]
	elif s % b == 0:
		lll = [1, 2]
	else:
		lll = [0, 1, 2]
	for i1 in ll:
		for i2 in lll:
			ww = s - b - 1 + b * i1 + i2
			if m == 100 and ww >= 0 and (ww < a * b) and (not ww in alpha):
				t[ww] = t[ww] - q
				if ww in beta:
					t[ww] = -1
	return t
if len(alpha) + len(beta) == a * b:
	if len(alpha) == 0:
		print('YES')
	elif len(beta) == 0:
		print('YES')
	else:
		print('NO')
else:
	for i in alpha:
		t = pre(i, 100)
	for i in range(a * b):
		if t[i] != 0 and t[i] != -10:
			print('NO')
			break
	else:
		print('YES')


(n, m) = map(int, input().split())
k = [input() for _ in range(n)]
k1 = [[0] * (m + 2) for __ in range(n + 2)]
for (s, x) in zip(k, range(1, n + 1)):
	for (c, y) in zip(s, range(1, m + 1)):
		if c == '*':
			for x1 in range(x - 1, x + 2):
				for y1 in range(y - 1, y + 2):
					k1[x1][y1] += 1
			k1[x][y] += 8
for (s, s1) in zip(k, k1[1:-1]):
	for (c, c1) in zip(s, s1[1:-1]):
		if c1 > 8:
			c1 = '*'
		elif c1:
			c1 = str(c1)
		else:
			c1 = '.'
		if c != c1:
			print('NO')
			break
	else:
		continue
	break
else:
	print('YES')


def valid(M, i, j):
	if M[i][j] == '*':
		return True
	count = int(M[i][j])
	for t1 in (-1, 0, 1):
		for t2 in (-1, 0, 1):
			if (t1, t2) != (0, 0):
				c = M[i + t1][j + t2]
				if c == '*':
					count -= 1
	if count != 0:
		return False
	else:
		return True
(n, m) = map(int, input().split())
M = []
M.append(['0' for _ in range(m + 2)])
for _ in range(n):
	l = ''.join(['0', input().replace('.', '0'), '0'])
	M.append(list(l))
M.append(['0' for _ in range(m + 2)])
try:
	for i in range(1, n + 1):
		for j in range(1, m + 1):
			if not valid(M, i, j):
				raise Exception('Invalid')
except:
	res = 'NO'
else:
	res = 'YES'
print(res)


(n, m) = map(int, input().split())
a = []
a.append((m + 2) * '.')
for i in range(n):
	a.append('.')
	b = input()
	a[i + 1] = a[i + 1] + b + '.'
a.append((m + 2) * '.')
for i in range(1, n + 1):
	for j in range(1, m + 1):
		if a[i][j] == '*':
			continue
		else:
			if a[i][j] == '.':
				c = 0
			else:
				c = int(a[i][j])
			d = 0
			for x in [-1, 0, 1]:
				for y in [-1, 0, 1]:
					if a[i + x][j + y] == '*':
						d += 1
			if c != d:
				print('NO')
				exit()
print('YES')


(n, m) = map(int, input().split())
a = []
b = [[0 for i in range(m)] for j in range(n)]
for i in range(n):
	a.append(input())
for i in range(n):
	for j in range(m):
		if a[i][j] == '*':
			b[i][j] = 9
			for x in range(-1, 2):
				for y in range(-1, 2):
					if i + x >= 0 and i + x < n and (j + y >= 0) and (j + y < m) and (a[i + x][j + y] != '*'):
						b[i + x][j + y] += 1
c = []
for i in range(n):
	cur = ''.join(map(str, b[i]))
	cur = cur.replace('9', '*')
	cur = cur.replace('0', '.')
	c.append(cur)
print('YES' if a == c else 'NO')


(r, c) = map(int, input().split())
given = []
correct = [[0 for i in range(c)] for i in range(r)]
for i in range(r):
	given.append(list(input()))
	for j in range(c):
		if given[i][j] == '.':
			given[i][j] = 0
		elif given[i][j] != '*':
			given[i][j] = int(given[i][j])
for i in range(r):
	for j in range(c):
		if given[i][j] == '*':
			correct[i][j] = '*'
			if i - 1 >= 0:
				if given[i - 1][j] != '*':
					correct[i - 1][j] += 1
				if j - 1 >= 0 and given[i - 1][j - 1] != '*':
					correct[i - 1][j - 1] += 1
				if j + 1 < c and given[i - 1][j + 1] != '*':
					correct[i - 1][j + 1] += 1
			if j - 1 >= 0 and given[i][j - 1] != '*':
				correct[i][j - 1] += 1
			if j + 1 < c and given[i][j + 1] != '*':
				correct[i][j + 1] += 1
			if i + 1 < r:
				if given[i + 1][j] != '*':
					correct[i + 1][j] += 1
				if j - 1 >= 0 and given[i + 1][j - 1] != '*':
					correct[i + 1][j - 1] += 1
				if j + 1 < c and given[i + 1][j + 1] != '*':
					correct[i + 1][j + 1] += 1
if correct == given:
	print('YES')
else:
	print('NO')


R = lambda : map(int, input().split())
(n, m) = R()
b0 = []
for i in range(110):
	b0.append([0] * 110)
b1 = []
for i in range(1, n + 1):
	s = input()
	b1.append(s.replace('.', '0'))
	for j in range(1, m + 1):
		if s[j - 1] == '*':
			b0[i - 1][j - 1] += 1
			b0[i - 1][j] += 1
			b0[i - 1][j + 1] += 1
			b0[i][j - 1] += 1
			b0[i][j + 1] += 1
			b0[i + 1][j - 1] += 1
			b0[i + 1][j] += 1
			b0[i + 1][j + 1] += 1
ans = 'YES'
for i in range(n):
	for j in range(m):
		if '0' <= b1[i][j] <= '8':
			if int(b1[i][j]) != b0[i + 1][j + 1]:
				ans = 'NO'
print(ans)


def main():

	def f(elem):
		if elem == '*':
			return 1
		else:
			return 0
	(n, m) = map(int, input().split())
	a = list()
	for i in range(n):
		a.append(list(input()))
	for i in range(n):
		for j in range(m):
			if a[i][j] == '.':
				a[i][j] = '0'
	for i in range(n):
		for j in range(m):
			if f(a[i][j]):
				continue
			tec = 0
			if i > 0 and j > 0:
				tec += f(a[i - 1][j - 1])
			if i > 0:
				tec += f(a[i - 1][j])
			if i > 0 and j < m - 1:
				tec += f(a[i - 1][j + 1])
			if j > 0:
				tec += f(a[i][j - 1])
			if j < m - 1:
				tec += f(a[i][j + 1])
			if i < n - 1 and j > 0:
				tec += f(a[i + 1][j - 1])
			if i < n - 1:
				tec += f(a[i + 1][j])
			if i < n - 1 and j < m - 1:
				tec += f(a[i + 1][j + 1])
			if tec != int(a[i][j]):
				print('NO')
				return
	print('YES')
main()


(n, m) = map(int, input().split())
a = []
a.append(['.' for _ in range(m + 2)])
for _ in range(n):
	row = []
	row.append('.')
	for x in list(input()):
		row.append(x)
	row.append('.')
	a.append(row)
a.append(['.' for _ in range(m + 2)])
f = True
for y in range(1, n + 1):
	for x in range(1, m + 1):
		if a[y][x] != '*':
			k = 0
			if a[y - 1][x - 1] == '*':
				k += 1
			if a[y][x - 1] == '*':
				k += 1
			if a[y + 1][x - 1] == '*':
				k += 1
			if a[y - 1][x] == '*':
				k += 1
			if a[y + 1][x] == '*':
				k += 1
			if a[y - 1][x + 1] == '*':
				k += 1
			if a[y][x + 1] == '*':
				k += 1
			if a[y + 1][x + 1] == '*':
				k += 1
			if a[y][x] == '.':
				c = 0
			else:
				c = int(a[y][x])
			if k != c:
				f = False
if f:
	print('YES')
else:
	print('NO')


(n, m) = input().split(' ')
n = int(n)
m = int(m)
list = []
comp = 'YES'
for i in range(n):
	list.append(str(input()))
for i in range(0, n):
	for j in range(0, m):
		a = list[i][j]
		if ord(a) >= 48 and ord(a) <= 57:
			a = int(a)
			count = 0
			for b in range(-1, 2):
				for c in range(-1, 2):
					if i + b >= 0 and i + b < n and (j + c >= 0) and (j + c < m):
						if list[i + b][j + c] == '*':
							count = count + 1
							if count > a:
								comp = 'NO'
								break
				if comp == 'NO':
					break
			if a != count:
				comp = 'NO'
		elif a == '.':
			for b in range(-1, 2):
				for c in range(-1, 2):
					if i + b >= 0 and i + b < n and (j + c >= 0) and (j + c < m):
						if list[i + b][j + c] == '*':
							comp = 'NO'
							break
				if comp == 'NO':
					break
print(comp)


(n, m) = map(int, input().split())
mat = []
dx = [1, 1, 0, 0, -1, -1, 1, -1]
dy = [-1, 0, 1, -1, -1, 0, 1, 1]
for i in range(n):
	l = list(input())
	mat.append(l)

def isvalid(i, j):
	return i >= 0 and i < n and (j >= 0) and (j < m) and (mat[i][j] == '*')
f = 1
for i in range(n):
	for j in range(m):
		dot = 0
		if mat[i][j] == '*':
			continue
		if mat[i][j] == '.':
			dot = 1
			curr = 0
		if not dot:
			curr = mat[i][j]
			curr = int(curr)
		cnt = 0
		for a in range(8):
			if isvalid(i + dx[a], j + dy[a]):
				cnt += 1
		if cnt != curr:
			f = 0
if f:
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().split())
arr = []
for i in range(n):
	arr += [input()]
ok = True
for i in range(n):
	for j in range(m):
		if arr[i][j] != '*':
			cnt = 0
			l = max(0, j - 1)
			r = min(m - 1, j + 1)
			u = max(0, i - 1)
			d = min(n - 1, i + 1)
			for row in range(u, d + 1):
				for col in range(l, r + 1):
					if not (row == i and col == j):
						if arr[row][col] == '*':
							cnt += 1
			if arr[i][j] == '.' and cnt != 0:
				ok = False
				break
			try:
				if int(arr[i][j]) != cnt:
					ok = False
					break
			except:
				pass
if ok:
	print('YES')
else:
	print('NO')


(n, m) = [int(x) for x in input().split()]
inp = []
for i in range(n):
	a = []
	b = input()
	for ii in range(m):
		try:
			a.append(int(b[ii]))
		except:
			a.append(b[ii])
	inp.append(a)
d = True
for i in range(n):
	for ii in range(m):
		count = 0
		try:
			if inp[i][ii - 1] == '*' and ii >= 1:
				count += 1
		except:
			pass
		try:
			if inp[i][ii + 1] == '*':
				count += 1
		except:
			pass
		try:
			if inp[i + 1][ii] == '*':
				count += 1
		except:
			pass
		try:
			if inp[i - 1][ii] == '*' and i >= 1:
				count += 1
		except:
			pass
		try:
			if inp[i + 1][ii + 1] == '*':
				count += 1
		except:
			pass
		try:
			if inp[i + 1][ii - 1] == '*' and ii >= 1:
				count += 1
		except:
			pass
		try:
			if inp[i - 1][ii - 1] == '*' and i >= 1 and (ii >= 1):
				count += 1
		except:
			pass
		try:
			if inp[i - 1][ii + 1] == '*' and i >= 1:
				count += 1
		except:
			pass
		if inp[i][ii] == '.':
			if count == 0:
				pass
			elif count > 0:
				d = False
				break
		elif inp[i][ii] == '*':
			pass
		elif inp[i][ii] == count:
			pass
		elif inp[i][ii] != count:
			d = False
			break
if d == True:
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().split())
l = []
for i in range(n):
	a = input()
	l.append(a)
d = 1
for i in range(n):
	for j in range(m):
		t = 0
		if i > 0 and l[i - 1][j] == '*':
			t += 1
		if i < n - 1 and l[i + 1][j] == '*':
			t += 1
		if j > 0 and l[i][j - 1] == '*':
			t += 1
		if j < m - 1 and l[i][j + 1] == '*':
			t += 1
		if i > 0 and j > 0 and (l[i - 1][j - 1] == '*'):
			t += 1
		if i > 0 and j < m - 1 and (l[i - 1][j + 1] == '*'):
			t += 1
		if i < n - 1 and j > 0 and (l[i + 1][j - 1] == '*'):
			t += 1
		if i < n - 1 and j < m - 1 and (l[i + 1][j + 1] == '*'):
			t += 1
		if l[i][j] == '.' and t != 0:
			d = 0
			break
		elif l[i][j] != '*' and l[i][j] != '.' and (int(l[i][j]) != t):
			d = 0
			break
if d == 1:
	print('YES')
else:
	print('NO')


def go():
	(n, m) = (int(i) for i in input().split(' '))
	board = []
	for i in range(n):
		board.append([i for i in input()])

	def get_neighbour_bombs(i, j):
		total = 0
		for k in [-1, 0, 1]:
			for l in [-1, 0, 1]:
				if i + k >= n:
					continue
				if i + k < 0:
					continue
				if j + l >= m:
					continue
				if j + l < 0:
					continue
				if board[i + k][j + l] == '*':
					total += 1
		return total
	for i in range(n):
		for j in range(m):
			if board[i][j] != '*':
				if board[i][j] == '.':
					value = 0
				else:
					value = int(board[i][j])
				if value != get_neighbour_bombs(i, j):
					return 'NO'
	return 'YES'
print(go())


(h, w) = map(int, input().split())
arr = []
for i in range(h):
	s = str(input())
	arr.append(s)
flag = 0
if h == 1 and w == 1:
	if arr[0][0] != '.' and arr[0][0] != '*':
		flag = 1
elif h == 1 and w > 1:
	if arr[0][0] == '.':
		if arr[0][1] == '*':
			flag = 1
	elif arr[0][0] in '12345678':
		if arr[0][0] == '1':
			if arr[0][1] != '*':
				flag = 1
		else:
			flag = 1
	if arr[0][w - 1] == '.':
		if arr[0][w - 2] == '*':
			flag = 1
	elif arr[0][w - 1] in '12345678':
		k = 0
		if arr[0][w - 2] == '*':
			k += 1
		if str(k) != arr[0][w - 1]:
			flag = 1
	for j in range(1, w - 1):
		if arr[0][j] in '12345678':
			k = 0
			if arr[0][j - 1] == '*':
				k += 1
			if arr[0][j + 1] == '*':
				k += 1
			if str(k) != arr[0][j]:
				flag = 1
		elif arr[0][j] == '.':
			if arr[0][j - 1] == '*' or arr[0][j + 1] == '*':
				flag = 1
elif h > 1 and w == 1:
	if arr[0][0] == '.':
		if arr[1][0] == '*':
			flag = 1
	elif arr[0][0] in '12345678':
		if arr[0][0] == '1':
			if arr[1][0] != '*':
				flag = 1
		else:
			flag = 1
	if arr[h - 1][0] == '.':
		if arr[h - 2][0] == '*':
			flag = 1
	elif arr[h - 1][0] in '12345678':
		k = 0
		if arr[h - 2][0] == '*':
			k += 1
		if str(k) != arr[h - 1][0]:
			flag = 1
	for j in range(1, h - 1):
		if arr[j][0] in '12345678':
			k = 0
			if arr[j - 1][0] == '*':
				k += 1
			if arr[j + 1][0] == '*':
				k += 1
			if k != int(arr[j][0]):
				flag = 1
				break
		elif arr[j][0] == '.':
			if arr[j - 1][0] == '*' or arr[j + 1][0] == '*':
				flag = 1
else:
	if arr[0][0] == '.':
		if arr[1][0] == '*' or arr[1][1] == '*' or arr[0][1] == '*':
			flag = 1
	elif arr[0][0] in '12345678':
		k = 0
		if arr[0][1] == '*':
			k += 1
		if arr[1][0] == '*':
			k += 1
		if arr[1][1] == '*':
			k += 1
		if str(k) != arr[0][0]:
			flag = 1
	if arr[h - 1][0] == '.':
		if arr[h - 2][0] == '*' or arr[h - 2][1] == '*' or arr[h - 1][1] == '*':
			flag = 1
	elif arr[h - 1][0] in '12345678':
		k = 0
		if arr[h - 1][1] == '*':
			k += 1
		if arr[h - 2][0] == '*':
			k += 1
		if arr[h - 2][1] == '*':
			k += 1
		if str(k) != arr[h - 1][0]:
			flag = 1
	if arr[h - 1][w - 1] == '.':
		if arr[h - 2][w - 1] == '*' or arr[h - 2][w - 2] == '*' or arr[h - 1][w - 2] == '*':
			flag = 1
	elif arr[h - 1][w - 1] in '12345678':
		k = 0
		if arr[h - 1][w - 2] == '*':
			k += 1
		if arr[h - 2][w - 2] == '*':
			k += 1
		if arr[h - 2][w - 1] == '*':
			k += 1
		if str(k) != arr[h - 1][w - 1]:
			flag = 1
	if arr[0][w - 1] == '.':
		if arr[1][w - 1] == '*' or arr[1][w - 2] == '*' or arr[0][w - 2] == '*':
			flag = 1
	elif arr[0][w - 1] in '12345678':
		k = 0
		if arr[0][w - 2] == '*':
			k += 1
		if arr[1][w - 2] == '*':
			k += 1
		if arr[1][w - 1] == '*':
			k += 1
		if str(k) != arr[0][w - 1]:
			flag = 1
	for j in range(1, w - 1):
		if arr[0][j] in '12345678':
			k = 0
			if arr[0][j - 1] == '*':
				k += 1
			if arr[0][j + 1] == '*':
				k += 1
			if arr[1][j + 1] == '*':
				k += 1
			if arr[1][j - 1] == '*':
				k += 1
			if arr[1][j] == '*':
				k += 1
			if str(k) != arr[0][j]:
				flag = 1
		elif arr[0][j] == '.':
			if arr[0][j - 1] == '*' or arr[0][j + 1] == '*' or arr[1][j - 1] == '*' or (arr[1][j] == '*') or (arr[1][j + 1] == '*'):
				flag = 1
	for j in range(1, w - 1):
		if arr[h - 1][j] in '12345678':
			k = 0
			if arr[h - 1][j - 1] == '*':
				k += 1
			if arr[h - 1][j + 1] == '*':
				k += 1
			if arr[h - 2][j + 1] == '*':
				k += 1
			if arr[h - 2][j - 1] == '*':
				k += 1
			if arr[h - 2][j] == '*':
				k += 1
			if str(k) != arr[h - 1][j]:
				flag = 1
		elif arr[h - 1][j] == '.':
			if arr[h - 1][j - 1] == '*' or arr[h - 1][j + 1] == '*' or arr[h - 2][j - 1] == '*' or (arr[h - 2][j] == '*') or (arr[h - 2][j + 1] == '*'):
				flag = 1
	for j in range(1, h - 1):
		if arr[j][0] in '12345678':
			k = 0
			if arr[j - 1][0] == '*':
				k += 1
			if arr[j + 1][0] == '*':
				k += 1
			if arr[j + 1][1] == '*':
				k += 1
			if arr[j - 1][1] == '*':
				k += 1
			if arr[j][1] == '*':
				k += 1
			if str(k) != arr[j][0]:
				flag = 1
		elif arr[j][0] == '.':
			if arr[j - 1][0] == '*' or arr[j + 1][0] == '*' or arr[j - 1][1] == '*' or (arr[j][1] == '*') or (arr[j + 1][1] == '*'):
				flag = 1
	for j in range(1, h - 1):
		if arr[j][w - 1] in '12345678':
			k = 0
			if arr[j - 1][w - 1] == '*':
				k += 1
			if arr[j + 1][w - 1] == '*':
				k += 1
			if arr[j + 1][w - 2] == '*':
				k += 1
			if arr[j - 1][w - 2] == '*':
				k += 1
			if arr[j][w - 2] == '*':
				k += 1
			if str(k) != arr[j][w - 1]:
				flag = 1
		elif arr[j][w - 1] == '.':
			if arr[j - 1][w - 1] == '*' or arr[j + 1][w - 1] == '*' or arr[j - 1][w - 2] == '*' or (arr[j][w - 2] == '*') or (arr[j + 1][w - 2] == '*'):
				flag = 1
	for j in range(1, h - 1):
		for k in range(1, w - 1):
			if arr[j][k] in '12345678':
				x = 0
				if arr[j - 1][k] == '*':
					x += 1
				if arr[j + 1][k] == '*':
					x += 1
				if arr[j + 1][k + 1] == '*':
					x += 1
				if arr[j + 1][k - 1] == '*':
					x += 1
				if arr[j - 1][k + 1] == '*':
					x += 1
				if arr[j - 1][k - 1] == '*':
					x += 1
				if arr[j][k + 1] == '*':
					x += 1
				if arr[j][k - 1] == '*':
					x += 1
				if str(x) != arr[j][k]:
					flag = 1
			elif arr[j][k] == '.':
				if arr[j - 1][k] == '*' or arr[j + 1][k] == '*' or arr[j + 1][k - 1] == '*' or (arr[j + 1][k + 1] == '*') or (arr[j - 1][k - 1] == '*') or (arr[j - 1][k + 1] == '*') or (arr[j][k - 1] == '*') or (arr[j][k + 1] == '*'):
					flag = 1
if flag == 0:
	print('YES')
else:
	print('NO')


import sys
(n, m) = map(int, input().split())
l = []
l.append('.' * (m + 2))
for x in range(n):
	s = input()
	s = '.' + s + '.'
	l.append(s)
l.append('.' * (m + 2))
for i in range(1, n + 1):
	for j in range(1, m + 1):
		if l[i][j] != '*':
			counter = 0
			if l[i - 1][j - 1] == '*':
				counter += 1
			if l[i][j - 1] == '*':
				counter += 1
			if l[i + 1][j - 1] == '*':
				counter += 1
			if l[i - 1][j] == '*':
				counter += 1
			if l[i + 1][j] == '*':
				counter += 1
			if l[i - 1][j + 1] == '*':
				counter += 1
			if l[i][j + 1] == '*':
				counter += 1
			if l[i + 1][j + 1] == '*':
				counter += 1
			if l[i][j] == '.':
				if counter != 0:
					print('NO')
					sys.exit(0)
			elif counter != int(l[i][j]):
				print('NO')
				sys.exit(0)
print('YES')


from sys import stdin, stdout
from math import factorial
from math import log10
from math import sqrt
(n, m) = map(int, stdin.readline().split())
steps = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]]
maps = []
for i in range(n):
	maps.append(stdin.readline().strip())
label = 1
for i in range(n):
	for j in range(m):
		if maps[i][j] == '*':
			continue
		cnt = 0
		for v in steps:
			if n > i + v[0] >= 0 and m > j + v[1] >= 0 and (maps[i + v[0]][j + v[1]] == '*'):
				cnt += 1
		if maps[i][j] in '12345678' and cnt != int(maps[i][j]):
			label = 0
		if maps[i][j] == '.' and cnt:
			label = 0
if label:
	stdout.write('YES')
else:
	stdout.write('NO')


l = input()
l = l.split()
n = int(l[0])
m = int(l[1])
array = []
s = ''
for i in range(0, m + 2):
	s = s + '0'
array.append(s)
for i in range(0, n):
	x = input()
	x = '0' + x + '0'
	array.append(x)
array.append(s)
flag = 0
for i in range(1, n + 1):
	for j in range(1, m + 1):
		if array[i][j] != '*':
			val = 0
			if array[i][j] == '.':
				val = 0
			else:
				val = int(array[i][j])
			cnt = 0
			for k in range(i - 1, i + 2):
				for l in range(j - 1, j + 2):
					if array[k][l] == '*':
						cnt = cnt + 1
			if cnt != val:
				print('NO')
				flag = 1
				break
	if flag == 1:
		break
if flag == 0:
	print('YES')


[n, m] = [int(i) for i in input().split()]
A = []
for i in range(n):
	A.append(input())

def lets_inc(B, i, j):
	if i - 1 >= 0 and j - 1 >= 0:
		if B[i - 1][j - 1] == '.':
			B[i - 1][j - 1] = 1
		elif B[i - 1][j - 1] != '*':
			B[i - 1][j - 1] += 1
	if i - 1 >= 0:
		if B[i - 1][j] == '.':
			B[i - 1][j] = 1
		elif B[i - 1][j] != '*':
			B[i - 1][j] += 1
	if i - 1 >= 0 and j + 1 <= m - 1:
		if B[i - 1][j + 1] == '.':
			B[i - 1][j + 1] = 1
		elif B[i - 1][j + 1] != '*':
			B[i - 1][j + 1] += 1
	if j - 1 >= 0:
		if B[i][j - 1] == '.':
			B[i][j - 1] = 1
		elif B[i][j - 1] != '*':
			B[i][j - 1] += 1
	if j + 1 <= m - 1:
		if B[i][j + 1] == '.':
			B[i][j + 1] = 1
		elif B[i][j + 1] != '*':
			B[i][j + 1] += 1
	if i + 1 <= n - 1 and j - 1 >= 0:
		if B[i + 1][j - 1] == '.':
			B[i + 1][j - 1] = 1
		elif B[i + 1][j - 1] != '*':
			B[i + 1][j - 1] += 1
	if i + 1 <= n - 1:
		if B[i + 1][j] == '.':
			B[i + 1][j] = 1
		elif B[i + 1][j] != '*':
			B[i + 1][j] += 1
	if i + 1 <= n - 1 and j + 1 <= m - 1:
		if B[i + 1][j + 1] == '.':
			B[i + 1][j + 1] = 1
		elif B[i + 1][j + 1] != '*':
			B[i + 1][j + 1] += 1
B = [['.' for _ in range(m)] for _ in range(n)]
for i in range(n):
	for j in range(m):
		if A[i][j] == '*':
			B[i][j] = '*'
			lets_inc(B, i, j)
for i in range(n):
	for j in range(m):
		B[i][j] = str(B[i][j])
for i in range(n):
	B[i] = ''.join(B[i])
OK = 1
for i in range(n):
	if A[i] != B[i]:
		OK = 0
if OK:
	print('YES')
else:
	print('NO')


def setbomb(x, y, m, n, b):
	if x > 0:
		if y > 0:
			b[y - 1][x - 1] += 1
		b[y][x - 1] += 1
		if y < n - 1:
			b[y + 1][x - 1] += 1
	if x < m - 1:
		if y > 0:
			b[y - 1][x + 1] += 1
		b[y][x + 1] += 1
		if y < n - 1:
			b[y + 1][x + 1] += 1
	if y > 0:
		b[y - 1][x] += 1
	if y < n - 1:
		b[y + 1][x] += 1
	return
(n, m) = map(int, input().split())
a = []
for i in range(n):
	a.append(input())
b = []
for i in range(n):
	b.append([])
	for j in range(m):
		b[i].append(0)
for i in range(n):
	for j in range(m):
		if a[i][j] == '*':
			setbomb(j, i, m, n, b)
for i in range(n):
	for j in range(m):
		if a[i][j] == '.':
			if b[i][j] > 0:
				print('NO')
				exit()
		elif a[i][j] != '*':
			if a[i][j] != str(b[i][j]):
				print('NO')
				exit()
print('YES')


(n, m) = map(int, input().split())
a = [['0'] * (m + 2)]
for i in range(n):
	a.append(list('0' + input() + '0'))
a.append(['0'] * (m + 2))
flag = True
for i in range(1, n + 1):
	for j in range(1, m + 1):
		d = a[i][j]
		if d != '*':
			if d == '.':
				d = 0
			else:
				d = int(d)
			if d != int(a[i - 1][j - 1] == '*') + int(a[i - 1][j] == '*') + int(a[i - 1][j + 1] == '*') + int(a[i][j - 1] == '*') + int(a[i][j + 1] == '*') + int(a[i + 1][j - 1] == '*') + int(a[i + 1][j] == '*') + int(a[i + 1][j + 1] == '*'):
				flag = False
print('YES' if flag else 'NO')


def cntBomb(i, j):
	global n, m, x
	cnt = 0
	if x[i - 1][j] == '*':
		cnt += 1
	if x[i + 1][j] == '*':
		cnt += 1
	if x[i][j - 1] == '*':
		cnt += 1
	if x[i][j + 1] == '*':
		cnt += 1
	if x[i - 1][j - 1] == '*':
		cnt += 1
	if x[i - 1][j + 1] == '*':
		cnt += 1
	if x[i + 1][j - 1] == '*':
		cnt += 1
	if x[i + 1][j + 1] == '*':
		cnt += 1
	return cnt
(n, m) = map(int, input().split())
x = [0] * (n + 2)
x[0] = ['.'] * (m + 2)
x[-1] = ['.'] * (m + 2)
for i in range(1, n + 1):
	x[i] = ['.']
	x[i].extend(list(input()))
	x[i].append('.')
for i in range(1, n + 1):
	for j in range(1, m + 1):
		c = x[i][j]
		if ord('1') <= ord(c) <= ord('8') or c == '.':
			if c == '.':
				c = 0
			if cntBomb(i, j) != int(c):
				print('NO')
				exit()
print('YES')


(n, m) = map(int, input().split())
s = [input() for i in range(n)]
t = [[1, 0], [1, 1], [1, -1], [-1, 0], [-1, 1], [-1, -1], [0, 1], [0, -1]]
for i in range(n):
	for j in range(m):
		if s[i][j] == '*':
			continue
		cnt = 0
		for x in t:
			if x[0] + i >= n or x[0] + i < 0 or x[1] + j >= m or (x[1] + j < 0):
				continue
			if s[i + x[0]][j + x[1]] == '*':
				cnt += 1
		if s[i][j] == '.' and cnt != 0:
			print('NO')
			exit()
		if s[i][j] != str(cnt) and s[i][j] != '.':
			print('NO')
			exit()
print('YES')


import sys
from collections import deque
from typing import Union
input = sys.stdin.readline

def print(x, end='\n'):
	sys.stdout.write(str(x) + end)

def Validneighbours(x, y, grid):
	z = 0
	if grid[x][y] == '.':
		z = 0
	else:
		z = grid[x][y]
	count = 0
	for i in range(x - 1, x + 2, 1):
		for j in range(y - 1, y + 2, 1):
			if grid[i][j] == '*':
				count += 1
	return count == z

def main():
	T = 1

	def solve():
		arr = list(map(int, input().split(' ')))
		(n, m) = (arr[0], arr[1])
		grid = [[0] * (m + 2) for i in range(n + 2)]
		for x in range(1, n + 1):
			s = input()
			for y in range(1, m + 1):
				if s[y - 1] == '*':
					grid[x][y] = s[y - 1]
				elif s[y - 1] == '.':
					grid[x][y] = s[y - 1]
				else:
					grid[x][y] = int(s[y - 1])
		valid = 1
		for i in range(1, n + 1, 1):
			if valid == 0:
				break
			for j in range(1, m + 1, 1):
				if grid[i][j] == '*':
					continue
				else:
					z = Validneighbours(i, j, grid)
					if z:
						continue
					else:
						valid = 0
						break
		if valid:
			print('YES')
		else:
			print('NO')
	for f in range(T):
		solve()
	pass
main()


X = list(map(int, input().split()))
AidArray = [[0] * (X[1] + 2) for i in range(X[0] + 2)]
MineSweeper = []
for i in range(X[0]):
	Temp = input()
	MineSweeper.append(list(Temp))
	for j in range(X[1]):
		if Temp[j] == '*':
			AidArray[i][j] += 1
			AidArray[i][j + 1] += 1
			AidArray[i][j + 2] += 1
			AidArray[i + 1][j] += 1
			AidArray[i + 1][j + 2] += 1
			AidArray[i + 2][j] += 1
			AidArray[i + 2][j + 1] += 1
			AidArray[i + 2][j + 2] += 1
for i in range(X[0]):
	for j in range(X[1]):
		if MineSweeper[i][j] != '*':
			if MineSweeper[i][j] == '.' and AidArray[i + 1][j + 1] == 0 or MineSweeper[i][j] == str(AidArray[i + 1][j + 1]):
				continue
			print('NO')
			exit()
print('YES')


R = lambda : list(map(int, input().split()))
(n, m) = R()
field = []
for i in range(n):
	t = input()
	l = []
	for ele in t:
		l.append(ele)
	field.append(l)
i_arr = [+0, +1, +1, +1, +0, -1, -1, -1]
j_arr = [-1, -1, +0, +1, +1, +1, +0, -1]

def check():
	for i in range(n):
		for j in range(m):
			ele = field[i][j]
			bomb = 0
			if ele == '*':
				continue
			for k in range(8):
				i_ = i + i_arr[k]
				j_ = j + j_arr[k]
				if i_ >= 0 and i_ < n and (j_ >= 0) and (j_ < m) and (field[i_][j_] == '*'):
					bomb += 1
			if ele == '.' and bomb > 0:
				return False
			if ele.isdigit() and bomb != int(ele):
				return False
	return True
if check():
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().split())
a = ['.' * (m + 2)] + ['.%s.' % input() for _ in range(n)] + ['.' * (m + 2)]
b = [['.'] * (m + 2) for i in range(n + 2)]
convert = lambda c: 1 if str(c) == '.' else c if str(c) == '*' else c + 1
for i in range(1, n + 1):
	if '*' in set(a[i]):
		for d in range(m + 2):
			if a[i][d] == '*':
				b[i][d] = '*'
				b[i][d - 1] = convert(b[i][d - 1])
				b[i][d + 1] = convert(b[i][d + 1])
				b[i - 1][d - 1] = convert(b[i - 1][d - 1])
				b[i - 1][d + 1] = convert(b[i - 1][d + 1])
				b[i - 1][d] = convert(b[i - 1][d])
				b[i + 1][d] = convert(b[i + 1][d])
				b[i + 1][d - 1] = convert(b[i + 1][d - 1])
				b[i + 1][d + 1] = convert(b[i + 1][d + 1])
for i in range(1, n + 1):
	if a[i][1:m + 1] != ''.join(['%s' % i for i in b[i]])[1:m + 1]:
		print('NO')
		break
else:
	print('YES')


(n, m) = map(int, input().split())
S = [input() for i in range(n)]
res = [[0] * m for i in range(n)]
d = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
for i in range(n):
	for j in range(m):
		if S[i][j] == '*':
			for (di, dj) in d:
				(ni, nj) = (i + di, j + dj)
				if 0 <= ni < n and 0 <= nj < m:
					if S[ni][nj] != '*':
						res[ni][nj] += 1
for i in range(n):
	for j in range(m):
		if S[i][j] == '*':
			continue
		elif S[i][j] == '.':
			if res[i][j] != 0:
				print('NO')
				exit()
		elif int(S[i][j]) != res[i][j]:
			print('NO')
			exit()
else:
	print('YES')


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

def isValid(n, m, i, j, l):
	c = 0
	if j - 1 >= 0 and l[i][j - 1] == '*':
		c += 1
	if j - 1 >= 0 and i - 1 >= 0 and (l[i - 1][j - 1] == '*'):
		c += 1
	if i - 1 >= 0 and l[i - 1][j] == '*':
		c += 1
	if j + 1 < m and i - 1 >= 0 and (l[i - 1][j + 1] == '*'):
		c += 1
	if j + 1 < m and l[i][j + 1] == '*':
		c += 1
	if j + 1 < m and i + 1 < n and (l[i + 1][j + 1] == '*'):
		c += 1
	if i + 1 < n and l[i + 1][j] == '*':
		c += 1
	if j - 1 >= 0 and i + 1 < n and (l[i + 1][j - 1] == '*'):
		c += 1
	return c == (int(l[i][j]) if l[i][j] != '.' else 0)
ok = True
for i in range(n):
	for j in range(m):
		if l[i][j] != '*' and (not isValid(n, m, i, j, l)):
			ok = False
print('YES' if ok else 'NO')


def val(arr, i, j):
	try:
		if i < 0:
			return 0
		if j < 0:
			return 0
		x = arr[i][j]
		if x == '*':
			return 1
		else:
			return 0
	except:
		return 0
[n, m] = list(map(int, input().split()))
arr = []
for i in range(n):
	arr.append(input(''))
flag = 0
sign = 0
for i in range(n):
	flag = 0
	for j in range(m):
		flag = 0
		if arr[i][j] != '*' and arr[i][j] != '.':
			if val(arr, i, j - 1) + val(arr, i, j + 1) + val(arr, i - 1, j) + val(arr, i + 1, j) + val(arr, i + 1, j + 1) + val(arr, i + 1, j - 1) + val(arr, i - 1, j + 1) + val(arr, i - 1, j - 1) != int(arr[i][j]):
				flag = 1
				sign = 1
				break
		elif arr[i][j] == '.':
			if val(arr, i, j - 1) + val(arr, i, j + 1) + val(arr, i - 1, j) + val(arr, i + 1, j) + val(arr, i + 1, j + 1) + val(arr, i + 1, j - 1) + val(arr, i - 1, j + 1) + val(arr, i - 1, j - 1) != 0:
				flag = 1
				sign = 1
				break
	if flag:
		break
if sign:
	print('NO')
else:
	print('YES')


T = input().split(' ')
n = int(T[0])
m = int(T[1])
L = []
M = [0] * (m + 2)
L.append(M)
for i in range(n):
	M = [0] * (m + 2)
	t = input()
	for j in range(m):
		if t[j] == '*':
			M[j + 1] = -1
		elif t[j] != '.':
			M[j + 1] = int(t[j])
	L.append(M)
M = [0] * (m + 2)
L.append(M)
c = True
for i in range(1, n + 1):
	for j in range(1, m + 1):
		t = 0
		for a in range(-1, 2):
			for b in range(-1, 2):
				if L[i + a][j + b] == -1:
					t += 1
		if L[i][j] != -1 and L[i][j] != t:
			c = False
			break
if c:
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().split())
g = [input().replace('.', '0') for i in range(n)]

def f(r, c):
	v = sum((g[i][j] == '*' for i in range(max(0, r - 1), min(n, r + 2)) for j in range(max(0, c - 1), min(m, c + 2))))
	return v == int(g[r][c])
print('YES' if all((g[i][j] == '*' or f(i, j) for i in range(n) for j in range(m))) else 'NO')


digits = ['1', '2', '3', '4', '5', '6', '7', '8']
bomb = '*'
empty = '.'

def sweep(ch, i, j):
	neighboors = [ch[i - 1][j - 1], ch[i - 1][j], ch[i - 1][j + 1], ch[i][j - 1], ch[i][j + 1], ch[i + 1][j - 1], ch[i + 1][j], ch[i + 1][j + 1]]
	return neighboors
(n, m) = [int(x) for x in input().split(' ')]
Mines = [['0' for x in range(m + 2)] for y in range(n + 2)]
for i in range(1, n + 1):
	x = input()
	for j in range(1, m + 1):
		Mines[i][j] = x[j - 1]
mistake = 0
for i in range(1, n + 1):
	for j in range(1, m + 1):
		neighboors = sweep(Mines, i, j)
		if Mines[i][j] in digits:
			if neighboors.count(bomb) != int(Mines[i][j]):
				mistake += 1
		elif Mines[i][j] == empty:
			if bomb in neighboors:
				mistake += 1
if mistake == 0:
	print('YES')
else:
	print('NO')


from bisect import bisect_left, bisect_right

def tuple_input(type):
	return map(type, input().strip().split())
(n, m) = tuple_input(int)
F = list()
for _ in range(n):
	F.append(list(input().strip()))
di = [-1, -1, 0, 1, 1, 1, 0, -1]
dj = [0, 1, 1, 1, 0, -1, -1, -1]
for i in range(n):
	for j in range(m):
		if F[i][j] == '*':
			continue
		b = 0
		for k in range(8):
			i2 = i + di[k]
			j2 = j + dj[k]
			if 0 <= i2 and i2 < n and (0 <= j2) and (j2 < m) and (F[i2][j2] == '*'):
				b += 1
		if F[i][j] == '.':
			if b > 0:
				print('NO')
				exit(0)
		elif ord(F[i][j]) - ord('0') != b:
			print('NO')
			exit(0)
print('YES')


(n, m) = map(int, input().split())
A = [''] * (n + 2)
A[0] = '0' * (m + 2)
A[n + 1] = '0' * (m + 2)
for i in range(1, n + 1):
	k = str(input())
	A[i] = '0' + k + '0'
C = True
for i in range(1, len(A) - 1):
	for k in range(1, len(A[i]) - 1):
		if A[i][k] != '*':
			j = str(A[i - 1][k - 1]) + str(A[i - 1][k]) + str(A[i - 1][k + 1]) + str(A[i][k + 1]) + str(A[i][k - 1]) + str(A[i + 1][k - 1]) + str(A[i + 1][k]) + str(A[i + 1][k + 1])
			if '*' in j:
				if str(j.count('*')) != A[i][k]:
					C = False
			elif str(A[i][k]) != '.':
				C = False
if C:
	print('YES')
else:
	print('NO')


(n, m) = map(int, input().split())
maps = []
for i in range(n):
	s = [i for i in input()]
	maps.append(s)
import sys
for i in range(n):
	for j in range(m):
		ans = 0
		if i - 1 >= 0:
			if maps[i - 1][j] == '*':
				ans += 1
			if j - 1 >= 0 and maps[i - 1][j - 1] == '*':
				ans += 1
			if j + 1 < m and maps[i - 1][j + 1] == '*':
				ans += 1
		if i + 1 < n:
			if maps[i + 1][j] == '*':
				ans += 1
			if j - 1 >= 0 and maps[i + 1][j - 1] == '*':
				ans += 1
			if j + 1 < m and maps[i + 1][j + 1] == '*':
				ans += 1
		if j - 1 >= 0 and maps[i][j - 1] == '*':
			ans += 1
		if j + 1 < m and maps[i][j + 1] == '*':
			ans += 1
		if maps[i][j] in '12345678':
			if ans != int(maps[i][j]):
				print('NO')
				sys.exit(0)
		elif maps[i][j] == '.' and ans != 0:
			print('NO')
			sys.exit(0)
print('YES')


(n, m) = [int(x) for x in input().split()[:2]]
fd = [[-2 for i in range(m)] for i in range(n)]

def inbd(i, j):
	if i >= 0 and i < n and (j >= 0) and (j < m):
		return (i, j)
	else:
		return None

def neis(i, j):
	ans = [inbd(i - 1, j - 1), inbd(i - 1, j), inbd(i - 1, j + 1), inbd(i, j - 1), inbd(i, j + 1), inbd(i + 1, j - 1), inbd(i + 1, j), inbd(i + 1, j + 1)]
	ans = [x for x in ans if x != None]
	return ans
for i in range(n):
	s = input()
	for j in range(m):
		c = s[j]
		if c == '*':
			fd[i][j] = '*'
		elif c == '.':
			fd[i][j] = 0
		else:
			fd[i][j] = int(c)
for i in range(n):
	for j in range(m):
		if fd[i][j] == '*':
			for (x, y) in neis(i, j):
				if fd[x][y] == '*':
					continue
				fd[x][y] -= 1
fg = True
for i in range(n):
	for j in range(m):
		if fd[i][j] == '*' or fd[i][j] == 0:
			continue
		else:
			fg = False
if fg:
	print('YES')
else:
	print('NO')


(n, m) = list(map(int, input().split()))
a = []
for i in range(n):
	a.append(input())
f = 1
for i in range(n):
	for j in range(m):
		c = 0
		if a[i][j] == '.':
			t = 0
		elif a[i][j] == '*':
			t = -1
		else:
			t = int(a[i][j])
		if t != -1:
			for k1 in range(-1, 2):
				for k2 in range(-1, 2):
					if 0 <= i + k1 < n and 0 <= j + k2 < m:
						if a[i + k1][j + k2] == '*':
							c += 1
			if c != t:
				f = 0
				break
if f:
	print('YES')
else:
	print('NO')


import sys

def check(i, j):
	cnt = 0
	for x in range(i - 1, i + 2):
		for y in range(j - 1, j + 2):
			cnt += et[x][y] == '*'
	return cnt
(n, m) = map(int, input().split())
et = [['.'] * (m + 2)]
for i in range(n):
	x = list(input())
	et.append(['.'] + x + ['.'])
et.append(['.'] * (m + 2))
for i in range(1, n + 1):
	for j in range(1, m + 1):
		if et[i][j].isdigit():
			if int(et[i][j]) != check(i, j):
				print('NO')
				sys.exit(0)
		elif et[i][j] == '.' and check(i, j) != 0:
			print('NO')
			sys.exit(0)
print('YES')


(n, m) = [int(i) for i in input().split()]
a = [[] for i in range(n)]
ans = 'YES'
for i in range(n):
	for item in input():
		if item == '*':
			a[i].append('*')
		elif item == '.':
			a[i].append(0)
		else:
			a[i].append(int(item))
i = 0
j = 0
while i != n and ans == 'YES':
	if j == m:
		j = 0
		i += 1
	if i != n and a[i][j] != '*':
		c = 0
		if j != m - 1 and a[i][j + 1] == '*':
			c += 1
		if j != m - 1 and i != n - 1 and (a[i + 1][j + 1] == '*'):
			c += 1
		if j != m - 1 and i != 0 and (a[i - 1][j + 1] == '*'):
			c += 1
		if i != n - 1 and a[i + 1][j] == '*':
			c += 1
		if i != 0 and a[i - 1][j] == '*':
			c += 1
		if j != 0 and a[i][j - 1] == '*':
			c += 1
		if j != 0 and i != n - 1 and (a[i + 1][j - 1] == '*'):
			c += 1
		if i != 0 and j != 0 and (a[i - 1][j - 1] == '*'):
			c += 1
		if c != a[i][j]:
			ans = 'NO'
	j += 1
print(ans)


(n, m) = [int(s) for s in input().split(' ')]
a = []
a.append('x' * (m + 2))
for i in range(n):
	a.append(('x' + input() + 'x').replace('.', '0'))
a.append('x' * (m + 2))
n = n + 2
m = m + 2
all_ok = True
for i in range(1, n - 1):
	if not all_ok:
		break
	for j in range(1, m - 1):
		if not all_ok:
			break
		if a[i][j] == 'x' or a[i][j] == '*':
			continue
		ssum = 0
		for p in range(-1, 2):
			for q in range(-1, 2):
				if p == 0 and q == 0:
					continue
				if a[i + p][j + q] == '*':
					ssum = ssum + 1
		if str(ssum) != a[i][j]:
			all_ok = False
			break
if all_ok:
	print('YES')
else:
	print('NO')


import os
import sys
debug = True
if debug and os.path.exists('input.in'):
	input = open('input.in', 'r').readline
else:
	debug = False
	input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())
test_count = 1
if debug:
	test_count = inp()
for t in range(test_count):
	if debug:
		print('Test Case #', t + 1)
	(n, m) = invr()
	a = [[0] * (m + 2)]
	for i in range(n):
		d_row = list(insr())
		row = [0]
		for j in range(0, m):
			if d_row[j] == '.':
				row.append(0)
			elif d_row[j] == '*':
				row.append(-1)
			else:
				row.append(int(d_row[j]))
		row.append(0)
		a.append(row)
	a.append([0] * (m + 2))
	ans = True
	for i in range(1, n + 1):
		for j in range(1, m + 1):
			if a[i][j] == -1:
				continue
			cnt = 0
			for k in (-1, 0, 1):
				for l in (-1, 0, 1):
					if k == 0 and l == 0:
						continue
					if a[i + k][j + l] == -1:
						cnt += 1
			if cnt != a[i][j]:
				ans = False
				break
		if ans == False:
			break
	print('YES' if ans else 'NO')


def main():
	(n, m) = list(map(int, input().split()))
	field = [[0] * (m + 2)]
	for i in range(n):
		field.append(list('0' + input().replace('.', '0') + '0'))
	field.append([0] * (m + 2))
	for i in range(1, n + 1):
		for j in range(1, m + 1):
			if field[i][j] != '*':
				if int(field[i][j]) == [field[i - 1][j - 1], field[i][j - 1], field[i + 1][j - 1], field[i - 1][j], field[i + 1][j], field[i - 1][j + 1], field[i][j + 1], field[i + 1][j + 1]].count('*'):
					pass
				else:
					print('NO')
					return
			else:
				continue
	print('YES')
main()


import sys
getnum = lambda cell: ord(cell) - ord('0') if cell >= '1' and cell <= '9' else 0 if cell == '.' else None
b = lambda row, col: row >= 0 and col >= 0 and (row < len(field)) and (col < len(field[0])) and (field[row][col] == '*')
input()
field = []
for line in sys.stdin:
	field.append(line)
for row in range(len(field)):
	for col in range(len(field[0])):
		num = getnum(field[row][col])
		if num != None and num != b(row - 1, col - 1) + b(row - 1, col) + b(row - 1, col + 1) + b(row, col - 1) + b(row, col + 1) + b(row + 1, col - 1) + b(row + 1, col) + b(row + 1, col + 1):
			print('NO')
			break
	else:
		continue
	break
else:
	print('YES')


(n, m) = map(int, input().split())
l = []
z = '.' * (m + 2)
l.append(z)
for i in range(n):
	x = input()
	x = '.' + x + '.'
	l.append(x)
l.append(z)
f = 1
for i in range(1, n + 1):
	for j in range(1, m + 1):
		if l[i][j] == '.':
			if l[i - 1][j - 1] == '*' or l[i - 1][j] == '*' or l[i - 1][j + 1] == '*' or (l[i][j - 1] == '*') or (l[i][j + 1] == '*') or (l[i + 1][j - 1] == '*') or (l[i + 1][j] == '*') or (l[i + 1][j + 1] == '*'):
				print('NO')
				exit(0)
		elif l[i][j] != '*':
			c = 0
			if l[i - 1][j - 1] == '*':
				c += 1
			if l[i - 1][j] == '*':
				c += 1
			if l[i - 1][j + 1] == '*':
				c += 1
			if l[i][j - 1] == '*':
				c += 1
			if l[i][j + 1] == '*':
				c += 1
			if l[i + 1][j - 1] == '*':
				c += 1
			if l[i + 1][j] == '*':
				c += 1
			if l[i + 1][j + 1] == '*':
				c += 1
			if c != int(l[i][j]):
				f = 0
if f:
	print('YES')
else:
	print('NO')


import sys

def main():
	(n, m) = map(int, sys.stdin.readline().split())
	grid = []
	for _ in range(n):
		grid.append(list(map(lambda x: x if x == '*' else 0 if x == '.' else int(x), sys.stdin.readline().strip())))
	res = [[0 for _ in range(m + 2)] for _ in range(n + 2)]
	for i in range(1, n + 1):
		for j in range(1, m + 1):
			cur = grid[i - 1][j - 1]
			if cur == '*':
				res[i][j + 1] += 1
				res[i][j - 1] += 1
				res[i + 1][j] += 1
				res[i - 1][j] += 1
				res[i + 1][j + 1] += 1
				res[i + 1][j - 1] += 1
				res[i - 1][j + 1] += 1
				res[i - 1][j - 1] += 1
	for i in range(n):
		for j in range(m):
			if res[i + 1][j + 1] != grid[i][j] and grid[i][j] != '*':
				print('NO')
				return
	print('YES')
main()


(n, m) = map(int, input().strip().split())
a = [[0 for _ in range(m + 2)] for _ in range(n + 2)]
for i in range(1, n + 1):
	a[i] = [0] + list(input())
	a[i].append(0)
for i in range(1, n + 1):
	for j in range(1, m + 1):
		if a[i][j] == '*':
			pass
		else:
			if a[i][j] == '.':
				a[i][j] = 0
			count = 0
			count = (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] == '*')
			if count != int(a[i][j]):
				print('NO')
				exit()
print('YES')


import sys
import math

def is_mine(c):
	if c == '*':
		return 1
	return 0

def fun():
	(n, m) = map(int, input().split())
	map_ = []
	map_.append([0] * (m + 2))
	for i in range(n):
		row = [0] + list(input()) + [0]
		map_.append(row)
	map_.append([0] * (m + 2))
	for i in range(1, n + 1):
		for j in range(1, m + 1):
			if map_[i][j] != '*':
				num = int(map_[i][j]) if map_[i][j] != '.' else 0
				count = 0
				for k in [-1, 0, 1]:
					for p in [-1, 0, 1]:
						count += is_mine(map_[i + k][j + p])
				if num != count:
					print('NO')
					return
	print('YES')
	return
fun()


from itertools import product
I = lambda : map(int, input().split())
(n, m) = I()
A = [input().replace('.', '0') for _ in range(n)]
for (x, y) in product(range(n), range(m)):
	if A[x][y] != '*' and sum((A[x + dx][y + dy] == '*' for dx in range(-1, 2) for dy in range(-1, 2) if 0 <= x + dx < n and 0 <= y + dy < m)) != int(A[x][y]):
		print('NO')
		break
else:
	print('YES')


def checkBomb(ch):
	if ch == '*':
		return True
	else:
		return False
(n, m) = map(int, input().split())
Field = []
Bomb = 0
for i in range(n):
	Field.append(input())
for i in range(n):
	TMP_ROW = ''
	for j in range(len(Field[i])):
		Bomb = 0
		if Field[i][j] != '*':
			if j - 1 >= 0:
				if checkBomb(Field[i][j - 1]):
					Bomb += 1
			if j + 1 < m:
				if checkBomb(Field[i][j + 1]):
					Bomb += 1
			if i - 1 >= 0:
				if checkBomb(Field[i - 1][j]):
					Bomb += 1
				if j + 1 < m:
					if checkBomb(Field[i - 1][j + 1]):
						Bomb += 1
				if j - 1 >= 0:
					if checkBomb(Field[i - 1][j - 1]):
						Bomb += 1
			if i + 1 < n:
				if checkBomb(Field[i + 1][j]):
					Bomb += 1
				if j + 1 < m:
					if checkBomb(Field[i + 1][j + 1]):
						Bomb += 1
				if j - 1 >= 0:
					if checkBomb(Field[i + 1][j - 1]):
						Bomb += 1
			if Bomb == 0:
				TMP_ROW += '.'
			else:
				TMP_ROW += str(Bomb)
		else:
			TMP_ROW += '*'
	if TMP_ROW != Field[i]:
		print('NO')
		exit(0)
print('YES')


(n, m) = map(int, input().split())
ar = [[i for i in input()] for j in range(n)]

def bombva(arr):
	if n >= 2 and m >= 2:
		if arr[0][0] != '*':
			c = 0
			if arr[0][1] == '*':
				c += 1
			if arr[1][0] == '*':
				c += 1
			if arr[1][1] == '*':
				c += 1
			if c == 0 and arr[0][0] != '.' or (c != 0 and arr[0][0] != str(c)):
				return 'NO'
		if arr[0][m - 1] != '*':
			c = 0
			if arr[0][m - 2] == '*':
				c += 1
			if arr[1][m - 1] == '*':
				c += 1
			if arr[1][m - 2] == '*':
				c += 1
			if c == 0 and arr[0][m - 1] != '.' or (c != 0 and arr[0][m - 1] != str(c)):
				return 'NO'
		if arr[n - 1][0] != '*':
			c = 0
			if arr[n - 1][1] == '*':
				c += 1
			if arr[n - 2][0] == '*':
				c += 1
			if arr[n - 2][1] == '*':
				c += 1
			if c == 0 and arr[n - 1][0] != '.' or (c != 0 and arr[n - 1][0] != str(c)):
				return 'NO'
		if arr[n - 1][m - 1] != '*':
			c = 0
			if arr[n - 1][m - 2] == '*':
				c += 1
			if arr[n - 2][m - 1] == '*':
				c += 1
			if arr[n - 2][m - 2] == '*':
				c += 1
			if c == 0 and arr[n - 1][m - 1] != '.' or (c != 0 and arr[n - 1][m - 1] != str(c)):
				return 'NO'
		for i in range(1, n - 1):
			if arr[i][0] != '*':
				c = 0
				if arr[i][1] == '*':
					c += 1
				if arr[i - 1][0] == '*':
					c += 1
				if arr[i - 1][1] == '*':
					c += 1
				if arr[i + 1][0] == '*':
					c += 1
				if arr[i + 1][1] == '*':
					c += 1
				if c == 0 and arr[i][0] != '.' or (c != 0 and arr[i][0] != str(c)):
					return 'NO'
			if arr[i][m - 1] != '*':
				c = 0
				if arr[i][m - 2] == '*':
					c += 1
				if arr[i - 1][m - 2] == '*':
					c += 1
				if arr[i - 1][m - 1] == '*':
					c += 1
				if arr[i + 1][m - 2] == '*':
					c += 1
				if arr[i + 1][m - 1] == '*':
					c += 1
				if c == 0 and arr[i][m - 1] != '.' or (c != 0 and arr[i][m - 1] != str(c)):
					return 'NO'
		for j in range(1, m - 1):
			if arr[0][j] != '*':
				c = 0
				if arr[1][j] == '*':
					c += 1
				if arr[0][j + 1] == '*':
					c += 1
				if arr[1][j + 1] == '*':
					c += 1
				if arr[0][j - 1] == '*':
					c += 1
				if arr[1][j - 1] == '*':
					c += 1
				if c == 0 and arr[0][j] != '.' or (c != 0 and arr[0][j] != str(c)):
					return 'NO'
			if arr[n - 1][j] != '*':
				c = 0
				if arr[n - 2][j] == '*':
					c += 1
				if arr[n - 1][j + 1] == '*':
					c += 1
				if arr[n - 2][j + 1] == '*':
					c += 1
				if arr[n - 1][j - 1] == '*':
					c += 1
				if arr[n - 2][j - 1] == '*':
					c += 1
				if c == 0 and arr[n - 1][j] != '.' or (c != 0 and arr[n - 1][j] != str(c)):
					return 'NO'
		for i in range(1, n - 1):
			for j in range(1, m - 1):
				if arr[i][j] != '*':
					c = 0
					if arr[i][j + 1] == '*':
						c += 1
					if arr[i][j - 1] == '*':
						c += 1
					if arr[i - 1][j] == '*':
						c += 1
					if arr[i - 1][j + 1] == '*':
						c += 1
					if arr[i - 1][j - 1] == '*':
						c += 1
					if arr[i + 1][j] == '*':
						c += 1
					if arr[i + 1][j + 1] == '*':
						c += 1
					if arr[i + 1][j - 1] == '*':
						c += 1
					if c == 0 and arr[i][j] != '.' or (c != 0 and arr[i][j] != str(c)):
						return 'NO'
		return 'YES'
	elif n == 1 and m >= 2:
		if arr[0][0] != '*':
			if arr[0][1] == '*' and arr[0][0] != '1' or (arr[0][1] != '*' and arr[0][0] != '.'):
				return 'NO'
		if arr[0][m - 1] != '*':
			if arr[0][m - 2] == '*' and arr[0][m - 1] != '1' or (arr[0][m - 2] != '*' and arr[0][m - 1] != '.'):
				return 'NO'
		for j in range(1, m - 1):
			if arr[0][j] != '*':
				c = 0
				if arr[0][j - 1] == '*':
					c += 1
				if arr[0][j + 1] == '*':
					c += 1
				if c == 0 and arr[0][j] != '.' or (c != 0 and arr[0][j] != str(c)):
					return 'NO'
		return 'YES'
	elif m == 1 and n >= 2:
		if arr[0][0] != '*':
			if arr[1][0] == '*' and arr[0][0] != '1' or (arr[1][0] != '*' and arr[0][0] != '.'):
				return 'NO'
		if arr[n - 1][0] != '*':
			if arr[n - 2][0] == '*' and arr[n - 1][0] != '1' or (arr[n - 2][0] != '*' and arr[n - 1][0] != '.'):
				return 'NO'
		for i in range(1, n - 1):
			if arr[i][0] != '*':
				c = 0
				if arr[i - 1][0] == '*':
					c += 1
				if arr[i + 1][0] == '*':
					c += 1
				if c == 0 and arr[i][0] != '.' or (c != 0 and arr[i][0] != str(c)):
					return 'NO'
		return 'YES'
	if arr[0][0] in '.*':
		return 'YES'
	return 'NO'
print(bombva(ar))


def readline():
	return map(int, input().split())

def yes():
	(h, w) = readline()

	def empty():
		return '.' * w
	prev = empty() + '..'
	curr = '.' + input() + '.'
	for i in range(h):
		next = (input() if i != h - 1 else empty()).join('..')
		for j in range(1, w + 1):
			if curr[j] != '*':
				count = sum((c == '*' for c in prev[j - 1:j + 2] + curr[j - 1:j + 2] + next[j - 1:j + 2]))
				if curr[j] == '.':
					if count:
						return False
				elif count != int(curr[j]):
					return False
		(prev, curr) = (curr, next)
	return True
print('YES' if yes() else 'NO')


(n, m) = [int(el) for el in input().split()]
field = []
for i in range(n):
	field.append(input())
correct = True
for y in range(n):
	for x in range(m):
		if field[y][x] == '*':
			continue
		count = 0
		for dy in range(-1, 2):
			for dx in range(-1, 2):
				if x + dx < 0 or x + dx > m - 1 or y + dy < 0 or (y + dy > n - 1) or (dy == 0 and dx == 0):
					continue
				if field[y + dy][x + dx] == '*':
					count += 1
		if field[y][x] == '.':
			input_count = 0
		else:
			input_count = int(field[y][x])
		if input_count != count:
			correct = False
			break
	if not correct:
		break
if correct:
	print('YES')
else:
	print('NO')


(n, m) = input().split()
m = int(m)
n = int(n)
l = ['#' * (m + 2)]
for i in range(n):
	l.append('#' + input() + '#')
l.append('#' * (m + 2))
h = 0
for i in range(1, n + 1):
	for j in range(1, m + 1):
		if l[i][j].isdigit():
			bombs = 0
			if int(l[i - 1][j] == '*') + int(l[i + 1][j] == '*') + int(l[i][j - 1] == '*') + int(l[i][j + 1] == '*') + int(l[i - 1][j - 1] == '*') + int(l[i - 1][j + 1] == '*') + int(l[i + 1][j - 1] == '*') + int(l[i + 1][j + 1] == '*') != int(l[i][j]):
				h = 1
				break
		elif l[i][j] == '.':
			if l[i - 1][j] == '*' or l[i + 1][j] == '*' or l[i][j - 1] == '*' or (l[i][j + 1] == '*') or (l[i - 1][j - 1] == '*') or (l[i - 1][j + 1] == '*') or (l[i + 1][j - 1] == '*') or (l[i + 1][j + 1] == '*'):
				h = 1
				break
	if h == 1:
		break
print('YES') if h == 0 else print('NO')