#include <bits/stdc++.h>
using namespace std;
class Minesweeper {
 public:
  int countAdj(int nr, int nc, vector<string> rows, int r, int c) {
    int rStart = r - 1;
    int rEnd = r + 1;
    int cStart = c - 1;
    int cEnd = c + 1;
    if (r == 0) {
      rStart = r;
    }
    if (r == (nr - 1)) {
      rEnd = r;
    }
    if (c == 0) {
      cStart = c;
    }
    if (c == (nc - 1)) {
      cEnd = c;
    }
    int cnt = 0;
    for (int i = rStart; i <= rEnd; i++) {
      for (int j = cStart; j <= cEnd; j++) {
        if ((i != r) || (j != c)) {
          char cell = rows[i][j];
          if (cell == '*') {
            cnt++;
          }
        }
      }
    }
    return cnt;
  }
  bool cellValid(int nRows, int nCols, vector<string> rows, int r, int c) {
    bool retV = true;
    char cell = rows[r][c];
    if (cell != '*') {
      int nAdj = -1;
      if (cell == '.') {
        nAdj = 0;
      } else {
        nAdj = cell - '0';
      }
      int actAdj = countAdj(nRows, nCols, rows, r, c);
      if (actAdj != nAdj) {
        retV = false;
      }
    }
    return retV;
  }
  string checkValid(int nRows, int nCols, vector<string> rows) {
    string retS = "YES";
    for (int i = 0; i < nRows; i++) {
      for (int j = 0; j < nCols; j++) {
        if (!cellValid(nRows, nCols, rows, i, j)) {
          retS = "NO";
          break;
        }
      }
      if (retS == "NO") {
        break;
      }
    }
    return retS;
  }
};
int main(int argc, const char* argv[]) {
  int n, m;
  cin >> n;
  cin >> m;
  string testLn;
  getline(cin, testLn);
  string rowLn;
  vector<string> rows;
  for (int i = 0; i < n; i++) {
    getline(cin, rowLn);
    rows.push_back(rowLn);
  }
  Minesweeper ms;
  string retS = ms.checkValid(n, m, rows);
  cout << retS << endl;
  return 0;
}
