#include <bits/stdc++.h>
using namespace std;
char mat[1000][1000];
long long n, m;
int32_t main() {
  cin >> n >> m;
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      cin >> mat[i][j];
    }
  }
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      if (mat[i][j] == '*') continue;
      long long cnt = 0;
      if (i + 1 >= 0 and i + 1 < n and j >= 0 and j < m and
          mat[i + 1][j] == '*')
        cnt++;
      if (i + 1 >= 0 and i + 1 < n and j + 1 >= 0 and j + 1 < m and
          mat[i + 1][j + 1] == '*')
        cnt++;
      if (i + 1 >= 0 and i + 1 < n and j - 1 >= 0 and j - 1 < m and
          mat[i + 1][j - 1] == '*')
        cnt++;
      if (i >= 0 and i < n and j + 1 >= 0 and j + 1 < m and
          mat[i][j + 1] == '*')
        cnt++;
      if (i >= 0 and i < n and j - 1 >= 0 and j - 1 < m and
          mat[i][j - 1] == '*')
        cnt++;
      if (i - 1 >= 0 and i - 1 < n and j >= 0 and j < m and
          mat[i - 1][j] == '*')
        cnt++;
      if (i - 1 >= 0 and i - 1 < n and j - 1 >= 0 and j - 1 < m and
          mat[i - 1][j - 1] == '*')
        cnt++;
      if (i - 1 >= 0 and i - 1 < n and j + 1 >= 0 and j + 1 < m and
          mat[i - 1][j + 1] == '*')
        cnt++;
      if (mat[i][j] == '.' and cnt == 0) continue;
      if (mat[i][j] - '0' == cnt) continue;
      cout << "NO\n";
      return 0;
    }
  }
  cout << "YES\n";
  return 0;
}
