#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;
const ll inf=1e9+7;
const ll mod=1e9+7;
int main(){
    ll h,w;
    cin>>h>>w;
    vector<vector<char> >s(h+2,vector<char>(w+2));
    REP(i,1,h+1){
        REP(j,1,w+1){
            cin>>s[i][j];
        }
    }
    vector<vector<ll> >visited(h+2,vector<ll>(w+2));
    queue<pll>q;
    visited[1][1]=1;
    q.push(mp(1,1));
    while(!q.empty()){
        pll k=q.front();
        q.pop();
        ll x=k.first,y=k.second;
        if(x%4==1){
            if(y%4==1){
                if(x+2<=h){
                    if(s[x+1][y]=='2'&&s[x+2][y]=='1'&&!visited[x+2][y]){
                        q.push(mp(x+2,y));
                        visited[x+2][y]=1;
                    }
                }
                if(x+1<=h){
                    if(s[x+1][y]=='2'){
                        visited[x+1][y]=1;
                    }
                }
                if(x-2>=0){
                    if(s[x-1][y]=='5'&&s[x-2][y]=='1'&&!visited[x-2][y]){
                        q.push(mp(x-2,y));
                        visited[x-2][y]=1;
                    }
                }
                if(x-1>=0){
                    if(s[x-1][y]=='5'){
                        visited[x-1][y]=1;
                    }
                }
                if(y+2<=w){
                    if(s[x][y+1]=='3'&&s[x][y+2]=='1'&&!visited[x][y+2]){
                        q.push(mp(x,y+2));
                        visited[x][y+2]=1;
                    }
                }
                if(y+1<=w){
                    if(s[x][y+1]=='3'){
                        visited[x][y+1]=1;
                    }
                }
                if(y-2>=0){
                    if(s[x][y-1]=='4'&&s[x][y-2]=='1'&&!visited[x][y-2]){
                        q.push(mp(x,y-2));
                        visited[x][y-2]=1;
                    }
                }
                if(y-1>=0){
                    if(s[x][y-1]=='4'){
                        visited[x][y-1]=1;
                    }
                }
            }else{
                if(x+2<=h){
                    if(s[x+1][y]=='2'&&s[x+2][y]=='6'&&!visited[x+2][y]){
                        q.push(mp(x+2,y));
                        visited[x+2][y]=1;
                    }
                }
                if(x+1<=h){
                    if(s[x+1][y]=='2'){
                        visited[x+1][y]=1;
                    }
                }
                if(x-2>=0){
                    if(s[x-1][y]=='5'&&s[x-2][y]=='6'&&!visited[x-2][y]){
                        q.push(mp(x-2,y));
                        visited[x-2][y]=1;
                    }
                }
                if(x-1>=0){
                    if(s[x-1][y]=='5'){
                        visited[x-1][y]=1;
                    }
                }
                if(y+2<=w){
                    if(s[x][y+1]=='4'&&s[x][y+2]=='6'&&!visited[x][y+2]){
                        q.push(mp(x,y+2));
                        visited[x][y+2]=1;
                    }
                }
                if(y+1<=w){
                    if(s[x][y+1]=='4'){
                        visited[x][y+1]=1;
                    }
                }
                if(y-2>=0){
                    if(s[x][y-1]=='3'&&s[x][y-2]=='6'&&!visited[x][y-2]){
                        q.push(mp(x,y-2));
                        visited[x][y-2]=1;
                    }
                }
                if(y-1>=0){
                    if(s[x][y-1]=='3'){
                        visited[x][y-1]=1;
                    }
                }
            }
        }
        if(x%4==3){
            if(y%4==1){
                if(x+2<=h){
                    if(s[x+1][y]=='5'&&s[x+2][y]=='6'&&!visited[x+2][y]){
                        q.push(mp(x+2,y));
                        visited[x+2][y]=1;
                    }
                }
                if(x+1<=h){
                    if(s[x+1][y]=='5'){
                        visited[x+1][y]=1;
                    }
                }
                if(x-2>=0){
                    if(s[x-1][y]=='2'&&s[x-2][y]=='6'&&!visited[x-2][y]){
                        q.push(mp(x-2,y));
                        visited[x-2][y]=1;
                    }
                }
                if(x-1>=0){
                    if(s[x-1][y]=='2'){
                        visited[x-1][y]=1;
                    }
                }
                if(y+2<=w){
                    if(s[x][y+1]=='3'&&s[x][y+2]=='6'&&!visited[x][y+2]){
                        q.push(mp(x,y+2));
                        visited[x][y+2]=1;
                    }
                }
                if(y+1<=w){
                    if(s[x][y+1]=='3'){
                        visited[x][y+1]=1;
                    }
                }
                if(y-2>=0){
                    if(s[x][y-1]=='4'&&s[x][y-2]=='6'&&!visited[x][y-2]){
                        q.push(mp(x,y-2));
                        visited[x][y-2]=1;
                    }
                }
                if(y-1>=0){
                    if(s[x][y-1]=='4'){
                        visited[x][y-1]=1;
                    }
                }
            }else{
                if(x+2<=h){
                    if(s[x+1][y]=='5'&&s[x+2][y]=='1'&&!visited[x+2][y]){
                        q.push(mp(x+2,y));
                        visited[x+2][y]=1;
                    }
                }
                if(x+1<=h){
                    if(s[x+1][y]=='5'){
                        visited[x+1][y]=1;
                    }
                }
                if(x-2>=0){
                    if(s[x-1][y]=='2'&&s[x-2][y]=='1'&&!visited[x-2][y]){
                        q.push(mp(x-2,y));
                        visited[x-2][y]=1;
                    }
                }
                if(x-1>=0){
                    if(s[x-1][y]=='2'){
                        visited[x-1][y]=1;
                    }
                }
                if(y+2<=w){
                    if(s[x][y+1]=='4'&&s[x][y+2]=='1'&&!visited[x][y+2]){
                        q.push(mp(x,y+2));
                        visited[x][y+2]=1;
                    }
                }
                if(y+1<=w){
                    if(s[x][y+1]=='4'){
                        visited[x][y+1]=1;
                    }
                }
                if(y-2>=0){
                    if(s[x][y-1]=='3'&&s[x][y-2]=='1'&&!visited[x][y-2]){
                        q.push(mp(x,y-2));
                        visited[x][y-2]=1;
                    }
                }
                if(y-1>=0){
                    if(s[x][y-1]=='3'){
                        visited[x][y-1]=1;
                    }
                }
            }
        }
    }
    if(visited[h][w])cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
}


Measured memory_bytes: 3276