#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cerr.tie(nullptr);
  int n, m;
  cin >> n >> m;
  int a[n][m];
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      char c;
      cin >> c;
      if (c >= '0' && c <= '9') a[i][j] = c - '0';
      if (c == '.') a[i][j] = 0;
      if (c == '*') a[i][j] = -1;
    }
  int d[n][m];
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      d[i][j] = 0;
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (a[i][j] == -1) {
        d[i][j] = -1;
        if (j && d[i][j - 1] != -1) d[i][j - 1] += 1;
        if (j < m - 1 && d[i][j + 1] != -1) d[i][j + 1] += 1;
        if (i && d[i - 1][j] != -1) d[i - 1][j] += 1;
        if (i < n - 1 && d[i + 1][j] != -1) d[i + 1][j] += 1;
        if (i > 0 && j > 0 && d[i - 1][j - 1] != -1) d[i - 1][j - 1] += 1;
        if (i > 0 && j < m - 1 && d[i - 1][j + 1] != -1) d[i - 1][j + 1] += 1;
        if (i < n - 1 && j > 0 && d[i + 1][j - 1] != -1) d[i + 1][j - 1]++;
        if (i < n - 1 && j < m - 1 && d[i + 1][j + 1] != -1) d[i + 1][j + 1]++;
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (d[i][j] != a[i][j]) return cout << "NO\n", 0;
    }
  }
  cout << "YES\n";
}
