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>

int main(){
    using namespace std;
    unsigned long H, W;
    cin >> H >> W;
    vector<vector<char>> kanou{{'6', '3', '1', '4'}, {'2', '0', '2', '0'}, {'1', '3', '6', '4'}, {'5', '0', '5', '0'}};
    vector<vector<unsigned long>> moveable(H + 2, vector<unsigned long>(W + 2));
    for(unsigned long i{0}; i < H; ++i){
        string S;
        cin >> S;
        for(unsigned long j{0}; j < W; ++j)moveable[i + 1][j + 1] = static_cast<unsigned long>(S[j] == kanou[i & 3][j & 3]);
    }
    vector<pair<unsigned long, unsigned long>> tansaku, dist{{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
    tansaku.emplace_back(1, 1);
    while(!tansaku.empty()){
        auto t = tansaku.back();
        tansaku.pop_back();
        moveable[t.first][t.second] = 0;
        if(t.first == H && t.second == W)return 0 & puts("YES");
        for(const auto& d : dist)if(moveable[t.first + d.first][t.second + d.second]){
            tansaku.emplace_back(t.first + d.first, t.second + d.second);
            moveable[t.first + d.first][t.second + d.second] = 0;
        }
    }
    return 0 & puts("NO");
}

C++: // #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep2(i,a,b) for(ll i=a;i<=b;++i)
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep3(i,a,b) for(ll i=a;i>=b;i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define vec vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define vec2(a,b) vector<vec>(a,vec(b))
#define vec2ll(a,b) vector<vec>(a,vecll(b))
#define vec3(a,b,c) vector<vector<vec>>(a,vec2(b,c))
#define vec3ll(a,b,c) vector<vector<vecll>>(a,vec2ll(b,c))
#define fi first
#define se second
#define endl "\n"
#define all(c) begin(c),end(c)
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))
#define MM " "
using namespace std;
int in() {int x;cin>>x;return x;}
ll lin() {ll x;cin>>x;return x;}
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T> inline void print(pair<T,T> p){cout<<"("<<p.first<<","<<p.second<<") ";}
template<class T> inline void print(vector<pair<T,T>> v){for(auto e:v)print(e); cout<<endl;}
template<class T> inline void print(T v){for(auto e:v)cout<<e<<" ";cout<<endl;}


int s[4][4]={
    {6,3,1,4},
    {2,0,2,0},
    {1,3,6,4},
    {5,0,5,0}
};
int check(int x,int y){
    return s[x%4][y%4];
}

main(){
    int h=in(),w=in();
    int mp[110][110];
    rep(i,h){
        string s;cin>>s;
        rep(j,w){
            mp[i][j]=(s[j]-'0');
        }
    }
    int t[110][110]={};
    t[0][0]=1;
    if(mp[0][0]!=6){
        cout<<"NO\n";return 0;
    }
    rep(i,h){
        rep(j,w){
            if(mp[i][j]==check(i,j))t[i+1][j+1]=1;
        }
    }
    queue<pii> q;
    q.emplace(1,1);
    int visited[110][110]={};
    visited[1][1]=1;
    int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
    while(!q.empty()){
        int x,y;
        tie(x,y)=q.front();
        q.pop();
        rep(i,4){
            if(t[x+dx[i]][y+dy[i]]){
                if(!visited[x+dx[i]][y+dy[i]]) {
                    q.emplace(x+dx[i],y+dy[i]);
                    visited[x+dx[i]][y+dy[i]]=1;
                }
            }
        }
    }
    cout <<(visited[h][w] ? "YES\n" : "NO\n");
}