#include <bits/stdc++.h>
using namespace std;
bool compare(pair<long long, long long> a, pair<long long, long long> b) {
  return (a.second < b.second);
}
long long n, m;
long long a[1000][1000];
long long fun(long long i, long long j, long long mn) {
  long long c = 0;
  if (i == 0 & j == 0) {
    if (a[i][j + 1] == -1) c++;
    if (a[i + 1][j + 1] == -1) c++;
    if (a[i + 1][j] == -1) c++;
  } else if (i == 0 && j == m - 1) {
    if (a[i][j - 1] == -1) c++;
    if (a[i + 1][j - 1] == -1) c++;
    if (a[i + 1][j] == -1) c++;
  } else if (i == n - 1 && j == 0) {
    if (a[i - 1][j] == -1) c++;
    if (a[i - 1][j + 1] == -1) c++;
    if (a[i][j + 1] == -1) c++;
  } else if (i == n - 1 && j == m - 1) {
    if (a[i - 1][j - 1] == -1) c++;
    if (a[i - 1][j] == -1) c++;
    if (a[i][j - 1] == -1) c++;
  } else {
    if (i == 0) {
      if (a[i][j - 1] == -1) c++;
      if (a[i + 1][j - 1] == -1) c++;
      if (a[i + 1][j] == -1) c++;
      if (a[i][j + 1] == -1) c++;
      if (a[i + 1][j + 1] == -1) c++;
    } else if (j == 0) {
      if (a[i][j + 1] == -1) c++;
      if (a[i + 1][j + 1] == -1) c++;
      if (a[i + 1][j] == -1) c++;
      if (a[i - 1][j] == -1) c++;
      if (a[i - 1][j + 1] == -1) c++;
    } else if (i == n - 1) {
      if (a[i - 1][j - 1] == -1) c++;
      if (a[i - 1][j] == -1) c++;
      if (a[i][j - 1] == -1) c++;
      if (a[i - 1][j + 1] == -1) c++;
      if (a[i][j + 1] == -1) c++;
    } else if (j == m - 1) {
      if (a[i - 1][j - 1] == -1) c++;
      if (a[i - 1][j] == -1) c++;
      if (a[i][j - 1] == -1) c++;
      if (a[i + 1][j - 1] == -1) c++;
      if (a[i + 1][j] == -1) c++;
    } else {
      if (a[i - 1][j - 1] == -1) c++;
      if (a[i - 1][j] == -1) c++;
      if (a[i][j - 1] == -1) c++;
      if (a[i + 1][j - 1] == -1) c++;
      if (a[i + 1][j] == -1) c++;
      if (a[i][j + 1] == -1) c++;
      if (a[i + 1][j + 1] == -1) c++;
      if (a[i - 1][j + 1] == -1) c++;
    }
  }
  if (c == mn) return 0;
  return 1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int tests = 1;
  while (tests--) {
    cin >> n >> m;
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < m; j++) {
        char ch;
        cin >> ch;
        if (ch == '*')
          a[i][j] = -1;
        else if (ch == '.')
          a[i][j] = 0;
        else
          a[i][j] = ch - '0';
      }
    }
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < m; j++) {
        if (a[i][j] >= 0) {
          if (fun(i, j, a[i][j]) == 1) {
            cout << "NO\n";
            return 0;
          }
        }
      }
    }
    cout << "YES\n";
  }
  return 0;
}
