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 ld = long double;
using VI = vector<int>;
using VL = vector<ll>;
using loop = int;
template<class T> using PQ = priority_queue<T, vector<T>, greater<T>>;
#define FOR(i,a,n) for(loop (i)=(a);(i)<(n);++(i))
#define eFOR(i,a,n) for(loop (i)=(a);(i)<=(n);++(i))
#define rFOR(i,a,n) for(loop (i)=(n)-1;(i)>=(a);--(i))
#define erFOR(i,a,n) for(loop (i)=(n);(i)>=(a);--(i))
#define each(i, a) for(auto &i : a)
#define SORT(i) sort((i).begin(),(i).end())
#define rSORT(i,a) sort((i).begin(),(i).end(),(a))
#define all(i) (i).begin(),(i).end()
#define out(y,x) ((y) < 0 || h <= (y) || (x) < 0 || w <= (x))
#define line cout << "------------------------\n" 
#define ENDL(i,n) ((i) == (n) - 1 ? "\n" : " ")
//#define stop system("pause") //comment out this on AOJ.
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
constexpr ld eps = 1e-10;
constexpr ld pi = 3.1415926535897932;
template<class T>inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; }return false; }
template<class T>inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; }return false; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }
template<class T>inline istream& operator>>(istream& is, vector<T>& v) { for (auto& elemnt : v)is >> elemnt; return is; }
template<class T, class U>inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template<class T>inline vector<T> vec(size_t a) { return vector<T>(a); }
template<class T>inline vector<T> defvec(T def, size_t a) { return vector<T>(a, def); }
template<class T, class... Ts>inline auto vec(size_t a, Ts... ts) { return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); }
template<class T, class... Ts>inline auto defvec(T def, size_t a, Ts... ts) { return vector<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...)); }

int main() {
    init();

    int yd[] = { 6,2,1,5 }, xd[] = { 6,3,1,4 };
    int yd2[] = { 6,5,1,2 }, xd2[] = { 6,4,1,3 };

    int h, w; cin >> h >> w;
    vector<string> s(h); cin >> s;
    vector<VI> g(h * w);

    for (int y = 0; y < h; y += 2) {
        FOR(x, 0, w - 1) {
            bool ok = false;
            FOR(j, 0, 4) {
                if ((y / 2) % 2 == 0) {
                    ok |= (s[y][x] - '0' == xd[j] && s[y][x + 1] - '0' == xd[(j + 1) % 4]);
                }
                else {
                    ok |= (s[y][x] - '0' == xd2[j] && s[y][x + 1] - '0' == xd2[(j + 1) % 4]);
                }
            }
            if (ok) {
                g[y * w + x].push_back(y * w + x + 1);
                g[y * w + x + 1].push_back(y * w + x);
            }
        }
    }
    for (int x = 0; x < w; x += 2) {
        FOR(y, 0, h - 1) {
            bool ok = false;
            FOR(j, 0, 4) {
                if ((x / 2) % 2 == 0) {
                    ok |= (s[y][x] - '0' == yd[j] && s[y + 1][x] - '0' == yd[(j + 1) % 4]);
                }
                else {
                    ok |= (s[y][x] - '0' == yd2[j] && s[y + 1][x] - '0' == yd2[(j + 1) % 4]);
                }
            }
            if (ok) {
                g[y * w + x].push_back((y + 1) * w + x);
                g[(y + 1) * w + x].push_back(y * w + x);
            }
        }
    }

    auto vis = vec<bool>(h * w);
    vis[0] = true;
    queue<int> bfs;
    bfs.push(0);
    while (!bfs.empty()) {
        int now = bfs.front();
        bfs.pop();
        each(to, g[now]) {
            if (vis[to])continue;
            vis[to] = true;
            bfs.push(to);
        }
    }

    cout << (vis[h * w - 1] ? "YES" : "NO") << "\n";
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const int mod=1000000007,MAX=105,INF=1<<30;

struct data{
    int h;
    int w;
    int d;
    int f;
    int r;
};

bool can[MAX][MAX][7][7][7];
vector<data> G[MAX][MAX][7][7][7];

int S[MAX][MAX];
int H,W;

void DFS(data u,data p){
    for(data to:G[u.h][u.w][u.d][u.f][u.r]){
        if(to.h==p.h&&to.w==p.w&&to.d==p.d&&to.f==p.f&&to.r==p.r) continue;
        
        if(to.h<0||to.h>=H||to.w<0||to.w>=W) continue;
        
        if(to.d!=S[to.h][to.w]) continue;
        
        if(can[to.h][to.w][to.d][to.f][to.r]) continue;
        
        can[to.h][to.w][to.d][to.f][to.r]=1;
        DFS(to,u);
    }
}

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    cin>>H>>W;
    for(int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            char c;cin>>c;
            if(c=='#') S[i][j]=0;
            else S[i][j]=c-'0';
        }
    }
    
    can[0][0][6][2][3]=1;
    
    for(int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            for(int d=1;d<=6;d++){
                for(int f=1;f<=6;f++){
                    for(int r=1;r<=6;r++){
                        if(d==f) continue;
                        if(d==r) continue;
                        if(f==r) continue;
                        
                        G[i][j][d][f][r].push_back({i+1,j,f,7-d,r});
                        G[i][j][d][f][r].push_back({i,j+1,r,f,7-d});
                        G[i][j][d][f][r].push_back({i-1,j,7-f,d,r});
                        G[i][j][d][f][r].push_back({i,j-1,7-r,f,d});
                    }
                }
            }
        }
    }
    
    DFS({0,0,6,2,3},{-1,-1,-1,-1,-1});
    
    bool ok=false;
    
    for(int f=1;f<=6;f++){
        for(int r=1;r<=6;r++){
            if(can[H-1][W-1][S[H-1][W-1]][f][r]) ok=true;
        }
    }
    
    if(ok) cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
}