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;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define get_unique(x) x.erase(unique(all(x)), x.end());
typedef long long ll;
typedef complex<double> Complex;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;
template <class T>
bool chmax(T& a, const T& b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
vector<T> make_vec(size_t a) {
    return vector<T>(a);
}
template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
struct DICE {
    int L, R, U, D, B, F;
    void right() {
        int x;
        x = R;
        R = U;
        U = L;
        L = D;
        D = x;
    }
    void left() {
        int x;
        x = L;
        L = U;
        U = R;
        R = D;
        D = x;
    }
    void down() {
        int x;
        x = F;
        F = U;
        U = B;
        B = D;
        D = x;
    }
    void up() {
        int x;
        x = B;
        B = U;
        U = F;
        F = D;
        D = x;
    }
};
int main() {
    int h, w;
    cin >> h >> w;
    auto v = make_vec<int>(h, w);
    rep(i, h) rep(j, w) {
        char c;
        cin >> c;
        if (c == '#')
            v[i][j] = -1;
        else
            v[i][j] = c - '0';
    }

    auto d = make_vec<DICE>(h, w);
    auto dp = make_vec<int>(h, w);

    d[0][0] = {4, 3, 1, 6, 5, 2};
    dp[0][0] = 1;
    rep(_, 1000) {
        rep(i, h) {
            rep(j, w) {
                if (j + 1 < w && v[i][j + 1] != -1) {
                    d[i][j].right();
                    if (d[i][j].D == v[i][j + 1]) {
                        d[i][j + 1] = d[i][j];
                        dp[i][j + 1] |= dp[i][j];
                    }
                    d[i][j].left();
                }
                if (i + 1 < h && v[i + 1][j] != -1) {
                    d[i][j].down();
                    if (d[i][j].D == v[i + 1][j]) {
                        d[i + 1][j] = d[i][j];
                        dp[i + 1][j] |= dp[i][j];
                    }
                    d[i][j].up();
                }
                if (j - 1 >= 0 && v[i][j - 1] != -1 && dp[i][j] == 1 &&
                    dp[i][j - 1] == 0) {
                    d[i][j].left();
                    if (d[i][j].D == v[i][j - 1]) {
                        d[i][j - 1] = d[i][j];
                        dp[i][j - 1] |= dp[i][j];
                    }
                    d[i][j].right();
                }
                if (i - 1 >= 0 && v[i - 1][j] != -1 && dp[i][j] == 1 &&
                    dp[i - 1][j] == 0) {
                    d[i][j].up();
                    if (d[i][j].D == v[i - 1][j]) {
                        d[i - 1][j] = d[i][j];
                        dp[i - 1][j] |= dp[i][j];
                    }
                    d[i][j].down();
                }
            }
        }
    }
    /*
    cout << endl;
    rep(i, h) {
        rep(j, w) cout << d[i][j].D;
        cout << endl;
    }
    */
    cout << (dp[h - 1][w - 1] ? "YES" : "NO") << endl;
}

C++: #include <iostream>
#include <algorithm>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <string>
#include <queue>
#include <deque>
#include <list>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <bitset>
#include <random>
#include <complex>
#include <assert.h>

using namespace std;
typedef long long ll;

const ll INF = 1LL << 60;

template<typename T>
inline bool chmin(T& x, T a) {
	if (x >= a) { x = a; return true; }
	return false;
}

template<typename T>
inline bool chmax(T& x, T a) {
	if (x <= a) { x = a; return true; }
	return false;
}

bool f[100][100];
bool reach[100][100];

int main() {
	int H, W; cin >> H >> W;
	const char table[4][4] = {
		{'6', '3', '1', '4'},
		{'2', '0', '2', '0'},
		{'1', '3', '6', '4'},
		{'5', '0', '5', '0'}
	};
	
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			reach[i][j] = false;
			f[i][j] = false;
			char c; cin >> c;
			f[i][j] = (c == table[i % 4][j % 4]);
		}
	}

	queue<pair<int, int>> q;
	q.push({ 0,0 });
	reach[0][0] = true;

	while (q.size() != 0) {
		pair<int, int> p = q.front();
		q.pop();

		int i = p.first, j = p.second;
		if (i < H - 1 && f[i + 1][j] == true) {
			if (reach[i + 1][j] == false) {
				q.push({ i + 1, j });
				reach[i + 1][j] = true;
			}
		}
		if (i > 0 && f[i - 1][j] == true) {
			if (reach[i - 1][j] == false) {
				q.push({ i - 1, j });
				reach[i - 1][j] = true;
			}
		}
		if (j < W - 1 && f[i][j + 1] == true) {
			if (reach[i][j + 1] == false) {
				q.push({ i, j + 1 });
				reach[i][j + 1] = true;
			}
		}
		if (j > 0 && f[i][j - 1] == true) {
			if (reach[i][j - 1] == false) {
				q.push({ i, j - 1 });
				reach[i][j - 1] = true;
			}
		}
	}

	if (reach[H - 1][W - 1] == true) {
		cout << "YES" << endl;
	}
	else {
		cout << "NO" << endl;
	}

	return 0;
}