#include<iostream>
#include<vector>
#include<string>
#include<queue>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template<typename T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return 0;}
template<typename T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return 0;}

struct dice {
    vector<int> side, front;
    dice() {
        side = vector<int>({1, 3, 6, 4});  // top -> right -> bottom -> left
        front = vector<int>({1, 2, 6, 5});  // top -> front -> bottom -> back
    }
    void rotateFront() {
        int tail = front.back();
        rrep(i, 4, 1) {
            front[i] = front[i - 1];
        }
        front[0] = tail;
        side[0] = front[0];
        side[2] = front[2];
    }
    void rotateBack() {
        int head = front.front();
        rep(i, 0, 3) {
            front[i] = front[i + 1];
        }
        front[3] = head;
        side[0] = front[0];
        side[2] = front[2];
    }
    void rotateRight() {
        int tail = side.back();
        rrep(i, 4, 1) {
            side[i] = side[i - 1];
        }
        side[0] = tail;
        front[0] = side[0];
        front[2] = side[2];
    }
    void rotateLeft() {
        int head = side.front();
        rep(i, 0, 3) {
            side[i] = side[i + 1];
        }
        side[3] = head;
        front[0] = side[0];
        front[2] = side[2];
    }
    int getBottom() const {
        return side[2];
    }
};

using P = pair<dice, pair<int, int>>;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // dice D;
    // cout << "First bottom=" << D.getBottom() << endl;
    // int T;
    // cin >> T;
    // rep(_, 0, T) {
    //     cout << "ROTATE -> ";
    //     flush(cout);
    //     char dir;
    //     cin >> dir;
    //     if (dir == 'L') {
    //         D.rotateLeft();
    //     }
    //     else if (dir == 'R') {
    //         D.rotateRight();
    //     }
    //     else if (dir == 'F') {
    //         D.rotateFront();
    //     }
    //     else {
    //         D.rotateBack();
    //     }
    //     cout << "Bottom=" << D.getBottom() << endl;
    // }
    int H, W;
    cin >> H >> W;
    vector<string> grid(H);
    for (auto& g : grid) {
        cin >> g;
    }
    dice D;
    queue<P> que;
    vector<vector<bool>> visited(H, vector<bool>(W, false));
    que.push(P(D, make_pair(0, 0)));
    visited[0][0] = true;
    while (!que.empty()) {
        P now = que.front();
        que.pop();
        dice now_d = now.first;
        int now_h = now.second.first;
        int now_w = now.second.second;
        dice d1(now_d);
        d1.rotateFront();
        if (now_h + 1 < H && grid[now_h + 1][now_w] != '#' && d1.getBottom() == (int)(grid[now_h + 1][now_w] - '0') && !visited[now_h + 1][now_w]) {
            visited[now_h + 1][now_w] = true;
            que.push(P(d1, make_pair(now_h + 1, now_w)));
        }
        dice d2(now_d);
        d2.rotateRight();
        if (now_w + 1 < W && grid[now_h][now_w + 1] != '#' && d2.getBottom() == (int)(grid[now_h][now_w + 1] - '0') && !visited[now_h][now_w + 1]) {
            visited[now_h][now_w + 1] = true;
            que.push(P(d2, make_pair(now_h, now_w + 1)));
        }
        dice d3(now_d);
        d3.rotateBack();
        if (now_h - 1 >= 0 && grid[now_h - 1][now_w] != '#' && d3.getBottom() == (int)(grid[now_h - 1][now_w] - '0') && !visited[now_h - 1][now_w]) {
            visited[now_h - 1][now_w] = true;
            que.push(P(d3, make_pair(now_h - 1, now_w)));
        }
        dice d4(now_d);
        d4.rotateLeft();
        if (now_w - 1 >= 0 && grid[now_h][now_w - 1] != '#' && d4.getBottom() == (int)(grid[now_h][now_w - 1] - '0') && !visited[now_h][now_w - 1]) {
            visited[now_h][now_w - 1] = true;
            que.push(P(d4, make_pair(now_h, now_w - 1)));
        }
    }
    if (visited[H - 1][W - 1]) {
        cout << "YES" << endl;
    }
    else {
        cout << "NO" << endl;
    }
    return 0;
}


Measured memory_bytes: 3280