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

Starter code:

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


import sys
import io

stream_enable = 0

inpstream = """
2 4
*.*.
1211
"""

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()))
# print(arr)
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] == "*"
        # print(i, j, bomb)
        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 = 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')


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


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


n, m = list(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')


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)




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

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




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 = list(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):
        #print(i, j, ((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)))
        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')


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)


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


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')
                return
        elif cur != '*':
            print('NO')
            return

print('YES')


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())
    #A.replace('.','0')
    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
#print(nR)
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')


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

n,m=list(map(int,input().split()))
a=[]
for _ in range(n):
	a.append(list(map(str,input().strip())))
#print(a)
k=0
for i in range(n):
	for j in range(m):
		p=a[i][j]
		#print(p)
		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
			#print("c=%d x=%d" %(c,x))
		if k==1:
			break
	if k==1:
		break
if k==0:
	print("YES")
else:
	print("NO")
				
	


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


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

import os, sys, math

def main():
	#T = int(input())
	#for _ in range(T):
	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)

def __starting_point():
	main()
__starting_point()

# n,m=int(input().split()[:2])
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
# print(fd)
if fg:
    print('YES')
else:
    print('NO')


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)
#print(a)
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
#print(a)
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")

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