#include <bits/stdc++.h>
using namespace std;

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
	return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
	for(auto &e:u) fill_v<T>(e,v...);
}

int64 d[4][4] = {{6, 3, 1, 4}, {2, 3, 5, 4}, {1, 3, 6, 4}, {5, 3, 2, 4}};

int64 dx[4] = {-1, 0, 1, 0};
int64 dy[4] = {0, -1, 0, 1};
string dir = "WNES";

struct Die{
  int s[6];
  int &top()   {return s[0];}
  int &south() {return s[1];}
  int &east()  {return s[2];}
  int &west()  {return s[3];}
  int &north() {return s[4];}
  int &bottom(){return s[5];}
  void roll(char c){
    //the view from above
    // N
    //W E
    // S
    string b("EWNSRL");
    int v[6][4]={{0,3,5,2},
                 {0,2,5,3},
                 {0,1,5,4},
                 {0,4,5,1},
                 {1,2,4,3},
                 {1,3,4,2}};
    for(int k=0;k<6;k++){
      if(b[k]!=c) continue;
      int t=s[v[k][0]];
      s[v[k][0]]=s[v[k][1]];
      s[v[k][1]]=s[v[k][2]];
      s[v[k][2]]=s[v[k][3]];
      s[v[k][3]]=t;
    }
  }
  using ll = long long;
  ll hash(){
    ll res=0;
    for(int i=0;i<6;i++) res=res*256+s[i];
    return res;
  }
  bool operator==(const Die &d) const{
    for(int i=0;i<6;i++) if(s[i]!=d.s[i]) return 0;
    return 1;
  }

  bool operator<(const Die &d) const {
    return s[0] < d.s[0];
  }
};

int main(void){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int64 H, W;
	cin >> H >> W;
	vector<string> f(H);
	REP(i, H) {
	  cin >> f[i];
	}
	auto d = make_v<int64>(H, W);
	fill_v<int64>(d, INF_LL);
	auto in = [&](int64 y, int64 x) {
	  return 0<=y&&y<H&&0<=x&&x<W&&(f[y][x]!='#');
	};
	using T = tuple<int64, int64, int64, Die>;
	priority_queue<T, vector<T>, greater<T>> pq;
	Die init;
	init.top() = 1; init.south() = 2; init.bottom() = 6; init.north() = 5; init.west() = 4; init.east() = 3;
	pq.emplace(0, 0, 0, init);
	d[0][0] = 0;
	while (pq.size()) {
	  int64 dd, y, x;
	  Die dice;
	  tie(dd, y, x, dice) = pq.top(); pq.pop();
	  if (d[y][x] < dd) continue;
	  REP(i, 4) {
	    int64 yy = y + dy[i], xx = x + dx[i];
	    Die nxt = dice;
	    nxt.roll(dir[i]);
	    if (!in(yy, xx) || nxt.bottom() != (f[yy][xx] - '0')) continue;
	    if (d[yy][xx] > dd+1) {
	      d[yy][xx] = dd+1;
	      pq.emplace(dd+1, yy, xx, nxt);
	    }
	  }
	}
	if (d[H-1][W-1] != INF_LL) {
    cout << "YES" << endl;
	} else {
	  cout << "NO" << endl;
	}
}



Measured memory_bytes: 3348