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;

const int MAX_H = 110;
int H, W;
int mp[MAX_H][MAX_H];
int cp[MAX_H][MAX_H];
char S[MAX_H];
int Base[5][5] = {  {6, 3, 1, 4, 6},
                    {2,-1, 2,-1, 2},
                    {1, 3, 6, 4, 1},
                    {5,-1, 5,-1, 5},
                    {6, 3, 1, 4, 6}};

int dx[4] = {0,1,-1, 0};
int dy[4] = {1,0, 0,-1};
bool visited[MAX_H][MAX_H];

void solve() {
    
    stack<pair<int,int> > task;
    task.push(pair<int,int>(0,0));
    while(!task.empty()) {
        pair<int,int> p = task.top(); task.pop();
        int x = p.first, y = p.second;
        if(visited[x][y]) continue;
        visited[x][y] = true;
        if(x == H-1 && y == W-1) { printf("YES\n"); return;}
        for(int i = 0; i < 4; ++i) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if(nx < 0 || nx >= H || ny < 0 || ny >= W) continue;
            if(mp[nx][ny] == -1 || cp[nx][ny] == -1 || mp[nx][ny] != cp[nx][ny]) continue;
            task.push(pair<int,int>(nx,ny));
        }
    }
    printf("NO\n");
}

int main()
{
    scanf("%d %d", &H, &W);
    for(int i = 0; i < H; ++i) {
        scanf("%s", S);
        for(int j = 0; j < W; ++j) {
            if(S[j] == '#') mp[i][j] = -1;
            else mp[i][j] = S[j] - '0';
        }
    }
    for(int i = 0; i < H; ++i) {
        for(int j = 0; j < W; ++j) {
            cp[i][j] = Base[i%4][j%4];
        }
    }
    /*
    printf("cp:\n");
    for(int i = 0; i < H; ++i) {
        for(int j = 0; j < W; ++j) {
            if(cp[i][j] == -1) { printf("#"); }
            else printf("%d",cp[i][j]);
        }
        printf("\n");
    }
    */
    solve();
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using Int = long long;
int PREP = (cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(9), 0);
//int SEGV = getenv("D") || (exit(system("D= SEGFAULT_SIGNALS=all catchsegv ./prog.exe") >> 8), 0);
int dice[7][4] = {
    { -1, -1, -1, -1 },
    {  2,  3,  5,  4 },
    {  1,  4,  6,  3 },
    {  1,  2,  6,  5 },
    {  1,  5,  6,  2 },
    {  1,  3,  6,  4 },
    {  2,  4,  5,  3 },
};
int dp[100][100][7][7];
int f(int k, int l) {
    for (int i = 0; i < 4; i++) {
        if (dice[l][i] == k) {
            return dice[l][(i + 1) % 4];
        }
    }
    return -1;
}
int main() {
    int H, W; cin >> H >> W;
    vector<string> S(H);
    for (auto &s : S) cin >> s;
    stack<tuple<int, int, int, int>> st;
    st.emplace(0, 0, 6, 2);
    while (!st.empty()) {
        int i, j, k, l; tie(i, j, k, l) = st.top(); st.pop();
        if (dp[i][j][k][l]) continue;
        if (S[i][j] == '#' || S[i][j] - '0' != k) continue;
        dp[i][j][k][l] = 1;
        if (i - 1 >= 0) st.emplace(i - 1, j, 7 - l, k);
        if (i + 1 < H) st.emplace(i + 1, j, l, 7 - k);
        if (j - 1 >= 0) st.emplace(i, j - 1, 7 - f(k, l), l);
        if (j + 1 < W) st.emplace(i, j + 1, f(k, l), l);
    }
    bool ans = false;
    for (int k = 1; k <= 6; k++) {
        for (int l = 1; l <= 6; l++) {
            ans |= dp[H - 1][W - 1][k][l];
        }
    }
    cout << (ans ? "YES" : "NO") << endl;
    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <string.h>
using namespace std; using ll = long long; using pll = pair<ll, ll>;
using vl = vector<ll>; using vll = vector<vl>; using vpll = vector<pll>;
int ctoi(char c) {
	switch (c) {
	case '0': return 0; case '1': return 1; case '2': return 2;
	case '3': return 3; case '4': return 4; case '5': return 5;
	case '6': return 6; case '7': return 7; case '8': return 8;
	case '9': return 9;  default: return 0;
	}
}
bool pairCompare(const pll firstElof, pll secondElof)
{
	return firstElof.second < secondElof.second;
}
//**Snippetリスト**//
//rep, vin, all//
//gcdlcm, isPrime, eratos, modinv, bitSearch, dfs, bfs, dijkstra, UnionFind, COM, digitDP//
ll i, j, k, l, m, n; ll N, M, K, H, W, L;
ll MOD = 1000000007; ll INF = 9999999999999; ll ans = 0;
vll flag, color; vll path; vl D;
//***********//
vll R;
void dfs(ll i, ll j) {
	stack<pll> S;
	S.push(make_pair(i, j));
	flag[i][j] = 1;
	while (!S.empty()) {
		pll u = S.top();
		S.pop();
		if (u.first == H - 1 && u.second == W - 1) {
			ans = 1;
		}
		if (i != 0) {
			if (flag[i - 1][j] == 0 && R[i-1][j] == 1)dfs(i - 1, j);
		}
		if (i != H-1) {
			if (flag[i + 1][j] == 0 && R[i+1][j] == 1)dfs(i + 1, j);
		}
		if (j != 0) {
			if (flag[i][j-1] == 0 && R[i][j-1] == 1)dfs(i, j-1);
		}
		if (j != W-1) {
			if (flag[i][j+1] == 0 && R[i][j+1] == 1)dfs(i, j+1);
		}
	}
}
int main() {
	cin >> H >> W;
	R.resize(H, vl(W));
	flag.resize(H, vl(W));
	vector<string> S(H);
	vll P(H, vl(W, 0));
	for (i = 0; i < H; i++) {
		cin >> S[i];
		for (j = 0; j < W; j++) {
			if (S[i][j] == '#') {
				P[i][j] = -1;
			}
			else {
				P[i][j] = ctoi(S[i][j]);
			}
		}
	}
	vll Q(H, vl(W, 0));
	for (i = 0; i < H; i++) {
		for (j = 0; j < W; j++) {
			ll key;
			if (i % 4 == 0) {
				if (j % 4 == 0) key = 6;
				if (j % 4 == 1) key = 3;
				if (j % 4 == 2) key = 1;
				if (j % 4 == 3) key = 4;
			}
			if (i % 4 == 1) {
				if (j % 4 == 0) key = 2;
				if (j % 4 == 1) key = 3;
				if (j % 4 == 2) key = 2;
				if (j % 4 == 3) key = 4;
			}
			if (i % 4 == 2) {
				if (j % 4 == 0) key = 1;
				if (j % 4 == 1) key = 3;
				if (j % 4 == 2) key = 6;
				if (j % 4 == 3) key = 4;
			}
			if (i % 4 == 3) {
				if (j % 4 == 0) key = 5;
				if (j % 4 == 1) key = 3;
				if (j % 4 == 2) key = 5;
				if (j % 4 == 3) key = 4;
			}
			Q[i][j] = key;
		}
	}
	for (i = 0; i < H; i++) {
		for (j = 0; j < W; j++) {
			if (P[i][j] == Q[i][j]) {
				R[i][j] = 1;
			}
			else {
				R[i][j] = 0;
			}
			//cout << R[i][j];
			flag[i][j] = 0;
		}
		//cout << endl;
	}
	dfs(0, 0);
	if (ans)cout << "YES" << endl;
	else cout << "NO" << endl;
}