#include<bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<n;i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl; 
#define INF 1000000000
#define mod 1000000007
using ll=long long;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

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;
  }

    void o(){
        cout<<d<<endl;
    }

  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);
  }
};

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(0);

    int h,w;cin>>h>>w;
    string s[h];
    rep(i,h) cin>>s[i];
    bool check[h][w]={};
    
    queue<pair<Dice,pair<int,int>>> que;
    Dice d[h][w];
    d[0][0].r=3;d[0][0].l=4;d[0][0].f=2;
    d[0][0].b=5;d[0][0].d=6;d[0][0].u=1;
    check[0][0]=true;
    que.push(make_pair(d[0][0],make_pair(0,0)));
    
    while(!que.empty()){
        auto p=que.front();que.pop();
        auto pos=p.second;auto dice=p.first;
        int i=pos.first,j=pos.second;
        // cout<<"ima ";dice.o();
        if(i-1>=0 and !check[i-1][j]){
            dice.RollS();
            // dice.o();
            if(dice.d+'0'==s[i-1][j]){
                check[i-1][j]=true;
                que.push(make_pair(dice,make_pair(i-1,j)));
            }
            dice.RollN();
        }
        if(i+1<h and !check[i+1][j]){
            dice.RollN();
            // dice.o();
            if(dice.d+'0'==s[i+1][j]){
                check[i+1][j]=true;
                que.push(make_pair(dice,make_pair(i+1,j)));
            }
            dice.RollS();
        }
        if(j-1>=0 and !check[i][j-1]){
            dice.RollE();
            // dice.o();
            if(dice.d+'0'==s[i][j-1]){
                check[i][j-1]=true;
                que.push(make_pair(dice,make_pair(i,j-1)));
            }
            dice.RollW();
        }
        if(j+1<w and !check[i][j+1]){
            dice.RollW();
            // dice.o();
            if(dice.d+'0'==s[i][j+1]){
                check[i][j+1]=true;
                que.push(make_pair(dice,make_pair(i,j+1)));
            }
            dice.RollE();
        }
    }
    
    if(check[h-1][w-1])     cout<<"YES"<<endl;
    else                    cout<<"NO"<<endl;
    return 0;
}



Measured memory_bytes: 3272