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

typedef long long ll;
typedef pair<int,int> P;

#define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0)
#define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0)

template<typename T1,typename T2>ostream& operator<<(ostream& os,const pair<T1,T2>& a) {os << "(" << a.first << ", " << a.second << ")";return os;}

const char newl = '\n';
struct Dice
{
  // int x, y;
  int l, r, f, b, d, u;

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

  void RollN()
  {
    // ++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 tapi {
    int x,y;
    Dice d;
    int get() {
        return d.d+d.f*6+d.r*36;
    }
};

int main() {
    int h,w;
    cin >> h >> w;
    string s[100];
    vector<vector<vector<bool>>> used(220,vector<vector<bool>>(h,vector<bool>(w,false)));
    queue<tapi> que;
    for (int i = 0;i < h;++i) cin >> s[i];
    for (int i = 0;i < h;++i) for (int j = 0;j < w;++j) if (s[i][j] == '6') s[i][j] = '0';
    que.push(tapi{0,0,Dice{4,3,2,5,0,1}});
    used[que.front().get()][0][0] = true;
    while (!que.empty()) {
        tapi t = que.front();que.pop();
        if (t.x > 0) {
            t.d.RollN();
            t.x--;
            if (t.d.d == s[t.x][t.y]-'0' && !used[t.get()][t.x][t.y]) {
                que.push(t);
                used[t.get()][t.x][t.y] = true;
            }
            t.d.RollS();
            t.x++;
        }
        if (t.x < h-1) {
            t.d.RollS();
            t.x++;
            if (t.d.d == s[t.x][t.y]-'0' && !used[t.get()][t.x][t.y]) {
                que.push(t);
                used[t.get()][t.x][t.y] = true;
            }
            t.d.RollN();
            t.x--;
        }
        if (t.y > 0) {
            t.d.RollE();
            t.y--;
            if (t.d.d == s[t.x][t.y]-'0' && !used[t.get()][t.x][t.y]) {
                que.push(t);
                used[t.get()][t.x][t.y] = true;
            }
            t.d.RollW();
            t.y++;
        }
        if (t.y < w-1) {
            t.d.RollW();
            t.y++;
            if (t.d.d == s[t.x][t.y]-'0' && !used[t.get()][t.x][t.y]) {
                que.push(t);
                used[t.get()][t.x][t.y] = true;
            }
            t.d.RollE();
            t.y--;
        }
    }
    bool ans = false;
    for (int i = 0;i < 220;++i) if (used[i][h-1][w-1]) ans = true;
    cout << (ans ? "YES" : "NO") << endl;
}


Measured memory_bytes: 4776