Go: package main

import "fmt"

var h, w int
var maze [][]byte

var visited [][]bool

type dice struct {
	top, bottom, right, left, up, down int
}

func move(d *dice, dir byte) {
	now := d.top
	switch dir {
	case 'N':
		d.top = d.down
		d.down = d.bottom
		d.bottom = d.up
		d.up = now
	case 'S':
		d.top = d.up
		d.up = d.bottom
		d.bottom = d.down
		d.down = now
	case 'W':
		d.top = d.right
		d.right = d.bottom
		d.bottom = d.left
		d.left = now
	case 'E':
		d.top = d.left
		d.left = d.bottom
		d.bottom = d.right
		d.right = now
	}
}

var dx = [4]int{1, -1, 0, 0}
var dy = [4]int{0, 0, 1, -1}

func dfs(x, y int, d *dice) bool {
	// fmt.Printf("dfs(%d,%d) en: dice.bottom=%d\n", x, y, d.bottom)
	visited[y][x] = true // (x,y)に到達した
	if (y == h-1) && (x == w-1) {
		return true
	}
	// 4方のマス目に進めるかどうかチェック
	for i := 0; i < 4; i++ {
		nx := x + dx[i]
		ny := y + dy[i]
		dir := []byte{'E', 'W', 'S', 'N'}
		dc := *d
		move(&dc, dir[i]) // 動かしてみる
		if 0 <= nx && nx < w && 0 <= ny && ny < h && maze[ny][nx] != '#' && !visited[ny][nx] && dc.bottom == int(maze[ny][nx]-'0') {
			// fmt.Printf("move dir %c, new bottom=%d, move to (nx, ny)=(%d,%d)\n", dir[i], dc.bottom, nx, ny)
			if dfs(nx, ny, &dc) {
				return true
			}
		} else {
			continue
		}
	}
	return false
}

func main() {
	fmt.Scan(&h, &w)
	maze = make([][]byte, h)
	visited = make([][]bool, h)
	for i := 0; i < h; i++ {
		visited[i] = make([]bool, w)
		maze[i] = make([]byte, w)
		fmt.Scan(&maze[i])
	}
	// fmt.Println("h", h, "w", w, "maze", maze)
	// visited[0][0] = true
	var d dice
	d.bottom = 6
	d.top = 1
	d.right = 3
	d.left = 4
	d.down = 2
	d.up = 5
	if dfs(0, 0, &d) {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using grid = vector<vector<char>>;

//constexpr ll MOD = 1000000007;

constexpr ll MOD = 998244353;

constexpr ll INF = 1050000000;
constexpr ll LONGINF = 1050000000000000000;
struct all_init {
    all_init() {
        cout.tie(nullptr);
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(11);
    };
} ALL_INIT;
struct edge {
 
    int from, to;
    ll cost;
    ll capa;
 
    edge(int s, int d) : from(s), to(d) {
        cost = 0;
        capa = 0;
    }
    edge(int s, int d, ll w) : from(s), to(d), cost(w) { capa = 0; }
    edge(int s, int d, ll x, ll y) : from(s), to(d), cost(x), capa(y) {}
 
    bool operator<(const edge& x) const { return cost < x.cost; }
};
using graph = vector<vector<edge>>;
 
#define CIN(vector_array_etc, n)         \
  for (int loop = 0; loop < n; loop++) { \
    cin >> vector_array_etc[loop];       \
  }
#define COUT(vector_array_etc, n)                                   \
  for (int LOOP = 0; LOOP < n; LOOP++) {                            \
    cout << vector_array_etc[LOOP] << (LOOP == n - 1 ? '\n' : ' '); \
  }
#define VC(Type_name) vector<Type_name>
#define SORT(vector_etc) sort(vector_etc.begin(), vector_etc.end())
#define ALL(vec_etc) vec_etc.begin(), vec_etc.end()
#define VCVC(Type_name) vector<vector<Type_name>>  
#define WARSHALL vector<vector<ll>> g(n, vector<ll>(n, LONGINF))
#define endl '\n'

 
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;
}
template <typename T>
istream& operator>>(istream& is, vector<T>& Vec) {
    for (T& x : Vec) {
        is >> x;
    }
    return is;
}
template <typename V, typename H>
void resize(vector<V>& vec, const H head) {
    vec.resize(head);
}
template <typename V, typename H, typename... T>
void resize(vector<V>& vec, const H& head, const T... tail) {
    vec.resize(head);
    for (auto& v : vec) {
        resize(v, tail...);
    }
}
template <ll mod>
struct ModInt {
    long long val;
    constexpr ModInt(long long v = 0) noexcept : val(v% mod) {
        if (val < 0) val += mod;
    }
    constexpr int getmod() { return mod; }
    constexpr ModInt operator-() const noexcept { return val ? mod - val : 0; }
    constexpr ModInt operator+(const ModInt& r) const noexcept {
        return ModInt(*this) += r;
    }
    constexpr ModInt operator-(const ModInt& r) const noexcept {
        return ModInt(*this) -= r;
    }
    constexpr ModInt operator*(const ModInt& r) const noexcept {
        return ModInt(*this) *= r;
    }
    constexpr ModInt operator/(const ModInt& r) const noexcept {
        return ModInt(*this) /= r;
    }
    constexpr ModInt& operator+=(const ModInt& r) noexcept {
        val += r.val;
        if (val >= mod) val -= mod;
        return *this;
    }
    constexpr ModInt& operator-=(const ModInt& r) noexcept {
        val -= r.val;
        if (val < 0) val += mod;
        return *this;
    }
    constexpr ModInt& operator*=(const ModInt& r) noexcept {
        val = val * r.val % mod;
        return *this;
    }
    constexpr ModInt& operator/=(const ModInt& r) noexcept {
        long long a = r.val, b = mod, u = 1, v = 0;
        while (b) {
            long long t = a / b;
            a -= t * b;
            swap(a, b);
            u -= t * v;
            swap(u, v);
        }
        val = val * u % mod;
        if (val < 0) val += mod;
        return *this;
    }
    constexpr bool operator==(const ModInt& r) const noexcept {
        return this->val == r.val;
    }
    constexpr bool operator!=(const ModInt& r) const noexcept {
        return this->val != r.val;
    }
    friend ostream& operator<<(ostream& os, const ModInt<mod>& x) noexcept {
        return os << x.val;
    }
    friend istream& operator>>(istream& is, ModInt<mod>& x) noexcept {
        return is >> x.val;
    }
    friend constexpr ModInt<mod> modpow(const ModInt<mod>& a,
        long long n) noexcept {
        if (n == 0) return 1;
        auto t = modpow(a, n / 2);
        t = t * t;
        if (n & 1) t = t * a;
        return t;
    }
};
template <class T>
struct nCk {
    vector<T> fact_, inv_, finv_;
    constexpr nCk() {}
    constexpr nCk(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) {
        init(n);
    }
    constexpr void init(int n) noexcept {
        fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1);
        ll MOD = 1000000007;
        for (ll i = 2; i < n; i++) {
            fact_[i] = fact_[i - 1] * i;
            inv_[i] = -inv_[MOD % i] * (MOD / i);
            finv_[i] = finv_[i - 1] * inv_[i];
        }
    }
    constexpr T com(ll n, ll k) const noexcept {
        if (n < k || n < 0 || k < 0) return 0;
        return fact_[n] * finv_[k] * finv_[n - k];
    }
    constexpr T fact(int n) const noexcept {
        if (n < 0) return 0;
        return fact_[n];
    }
    constexpr T inv(int n) const noexcept {
        if (n < 0) return 0;
        return inv_[n];
    }
    constexpr T finv(int n) const noexcept {
        if (n < 0) return 0;
        return finv_[n];
    }
};
 
int dx[] = { 0, 1, -1, 0, 1, -1, 1, -1 };  // i<4:4way i<8:8way
int dy[] = { 1, 0, 0, -1, 1, -1, -1, 1 };
 


int main() {
    vector<string> s={
        "6314",
        "2!2!",
        "1364",
        "5!5!",
    };

    int h,w;cin>>h>>w;

    vector<vector<char>> a(h,vector<char>(w));
    vector<vector<char>> x(h,vector<char>(w,'.'));
    vector<vector<int>> f(h,vector<int>(w,INF));
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cin>>a[i][j];
            if(a[i][j]!=s[i%4][j%4]){
                x[i][j]='#';
            }
        }
    }
    queue<pair<int, int>> q;
    q.push(make_pair(0, 0));
    f[0][0]=0;
 
	while (!q.empty()) {
		pair<int, int> check = q.front(); q.pop();
		for (int i = 0; i < 4; i++) {
			int ny = check.first + dy[i];
			int nx = check.second + dx[i];
            if(nx<0||nx>=w||ny<0||ny>=h){
                continue;
            }
			if (x[ny][nx] == '.'&&f[ny][nx]==INF) {
				f[ny][nx] = f[check.first][check.second] + 1;
				q.push(make_pair(ny, nx));
			}
		}
	}
    /*
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            cout<<x[i][j];
        }
        cout<<endl;
    }
    */

    if(f[h-1][w-1]!=INF){
        cout<<"YES"<<endl;
    }
    else{
        cout<<"NO"<<endl;
    }





    ;
    return 0;
}