#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define fr(i,n) for(int i=0;i<(n);++i)
#define Fr(i,n) for(int i=1;i<=(n);++i)
#define ifr(i,n) for(int i=(n)-1;i>=0;--i)
#define iFr(i,n) for(int i=(n);i>0;--i)
using P=pair<int,int>;

struct Dice
{
    // int x, y;
    int l, r, f, b, d, u;

    void RollN()
    {
        //  --y;
        int buff = d;
        d = f;
        f = u;
        u = b;
        b = buff;
    }

    void RollS()
    {
        // ++y;
        int buff = d;
        d = b;
        b = u;
        u = f;
        f = buff;
    }

    void RollL() // ----->
    {
        int buff = f;
        f = l;
        l = b;
        b = r;
        r = buff;
    }

    void RollR() // <------
    {
        int buff = f;
        f = r;
        r = b;
        b = l;
        l = buff;
    }

    void RollE() // .o -> o.
    {
        // --x;
        int buff = d;
        d = l;
        l = u;
        u = r;
        r = buff;
    }


    void RollW() // o. -> .o
    {
        //  ++x;
        int buff = d;
        d = r;
        r = u;
        u = l;
        l = buff;
    }


    vector< Dice > makeDice()
    {
        vector< Dice > ret;
        for(int i = 0; i < 6; i++) {
            Dice d(*this);
            if(i == 1) d.RollN();
            if(i == 2) d.RollS();
            if(i == 3) d.RollS(), d.RollS();
            if(i == 4) d.RollL();
            if(i == 5) d.RollR();
            for(int j = 0; j < 4; j++) {
                ret.emplace_back(d);
                d.RollE();
            }
        }
        return (ret);
    }
};

struct PD{
    P p;
    Dice d;
    PD(P p,Dice d):p(p),d(d){}
    bool operator<(const PD &x) const {return this->p<x.p;}
};

map<PD,bool> mp;

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    istream& in(cin);
    ostream& out(cout);
    Dice D;
    D.u=1;D.f=2;D.r=3,D.l=4,D.b=5,D.d=6;
    int h,w;
    in>>h>>w;
    vector<string> s(h);
    for(auto&c:s) in>>c;
    if(s[0][0]!='6') return puts("NO"),0;
    queue<pair<P,Dice>> q;
    q.emplace(make_pair(0,0),D);
    while(!q.empty()){
        P p;
        int i,j;
        Dice d;
        tie(p,d)=q.front();
        tie(i,j)=p;
        q.pop();
        if(i==h-1&&j==w-1) return puts("YES"),0;
        if(i>0&&s[i-1][j]!='#'){
            d.RollS();
            if(d.d==s[i-1][j]-'0'&&!mp[PD(P(i-1,j),d)]){
                mp[PD(P(i-1,j),d)]=true;
                q.emplace(make_pair(i-1,j),d);
            }
            d.RollN();
        }
        if(i<h-1&&s[i+1][j]!='#'){
            d.RollN();
            if(d.d==s[i+1][j]-'0'&&!mp[PD(P(i+1,j),d)]){
                mp[PD(P(i+1,j),d)]=true;
                q.emplace(make_pair(i+1,j),d);
            }
            d.RollS();
        }
        if(j>0&&s[i][j-1]!='#'){
            d.RollE();
            if(d.d==s[i][j-1]-'0'&&!mp[PD(P(i,j-1),d)]){
                mp[PD(P(i,j-1),d)]=true;
                q.emplace(make_pair(i,j-1),d);
            }
            d.RollW();
        }
        if(j<w-1&&s[i][j+1]!='#'){
            d.RollW();
            if(d.d==s[i][j+1]-'0'&&!mp[PD(P(i,j+1),d)]){
                mp[PD(P(i,j+1),d)]=true;
                q.emplace(make_pair(i,j+1),d);
            }
            d.RollE();
        }
    }
    puts("NO");
}


Measured memory_bytes: 3828