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"
#define _overload3(_1,_2,_3,name,...)name
#define _rep(i,n)repi(i,0,n)
#define repi(i,a,b)for(int i=int(a),i##_len=(b);i<i##_len;++i)
#define MSVC_UNKO(x)x
#define rep(...)MSVC_UNKO(_overload3(__VA_ARGS__,repi,_rep,_rep)(__VA_ARGS__))
#define all(c)c.begin(),c.end()
#define write(x)cout<<(x)<<'\n'
using namespace std; typedef long long ll; template<class T>using vv = vector<vector<T>>;
template<class T>auto vvec(int n, int m, T v) { return vv<T>(n, vector<T>(m, v)); }
constexpr int INF = 1 << 29, MOD = int(1e9) + 7; constexpr ll LINF = 1LL << 60;
struct aaa { aaa() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(10); }; }aaaa;

struct Die {
    array<int, 6> s;
    int& top() { return s[0]; }
    int& south() { return s[1]; }
    int& east() { return s[2]; }
    int& west() { return s[3]; }
    int& north() { return s[4]; }
    int& bottom() { return s[5]; }
    void roll(char c) {
        //the view from above
        // N
        //W E
        // S
        string b("EWNSRL");
        int v[6][4] = { {0,3,5,2},
                     {0,2,5,3},
                     {0,1,5,4},
                     {0,4,5,1},
                     {1,2,4,3},
                     {1,3,4,2} };
        for (int k = 0; k < 6; k++) {
            if (b[k] != c) continue;
            int t = s[v[k][0]];
            s[v[k][0]] = s[v[k][1]];
            s[v[k][1]] = s[v[k][2]];
            s[v[k][2]] = s[v[k][3]];
            s[v[k][3]] = t;
        }
    }
    using ll = long long;
    ll hash() {
        ll res = 0;
        for (int i = 0; i < 6; i++) res = res * 256 + s[i];
        return res;
    }
    bool operator==(const Die& d) const {
        for (int i = 0; i < 6; i++) if (s[i] != d.s[i]) return 0;
        return 1;
    }
    bool operator <(const Die& d) const {
        return s < d.s;
    }
};

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

int main() {
    int H, W;
    cin >> H >> W;
    vector<string> S(H);
    rep(i, H) cin >> S[i];

    vv<set<Die>> dist = vvec(H, W, set<Die>());
    Die start;
    start.top() = 1; start.south() = 2; start.east() = 3;
    start.west() = 4; start.north() = 5; start.bottom() = 6;
    dist[0][0].insert(start);
    queue<pair<pair<int, int>, Die>> q;
    q.emplace(make_pair(0, 0), start);
    while (!q.empty()) {
        auto v = q.front().first;
        auto dc = q.front().second;
        q.pop();
        constexpr char dirchar[] = { 'E','S','W','N' };
        rep(d, 4) {
            int nx = v.first + dy[d], ny = v.second + dx[d];
            Die dcn = dc;
            dcn.roll(dirchar[d]);
            if (nx < 0 || nx >= H || ny < 0 || ny >= W || dist[nx][ny].find(dcn) != dist[nx][ny].end() || S[nx][ny] == '#') continue;
            if (dcn.bottom() != S[nx][ny] - '0') continue;
            dist[nx][ny].insert(dcn);
            q.emplace(make_pair(nx, ny), dcn);
        }
    }
    write(dist[H - 1][W - 1].empty() ? "NO" : "YES");
}

C++: #include "bits/stdc++.h"
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;

typedef long long ll;

int main() {
    int H, W;
    cin >> H >> W;
    vector<string> s(H + 2);
    rep(i, W + 2) {
        s[0].push_back('#');
        s[H + 1].push_back('#');
    }
    rep(i, H) {
        cin >> s[i + 1];
        s[i + 1] = "#" + s[i + 1] + "#";
    }
    //rep(i, H + 2) cout << s[i] << "\n";
    vector<vector<vector<bool>>> grid(H + 2, vector<vector<bool>>(W + 2, vector<bool>(6, false)));
    queue<vector<int>> q;
    q.push({1, 1, 4, 0, 5, 4, 1, 3, 2});//xyzUDFBLR
    grid[1][1][5] = true;
    while (!q.empty()) {
        auto p = q.front();
        q.pop();
        int x = p[0], y = p[1], z = p[2];
        if (!grid[x - 1][y][p[3]] && s[x - 1][y] - '1' == p[5]) {
            q.push({x - 1, y, p[3], p[6], p[5], p[3], p[4], p[7], p[8]});
            grid[x - 1][y][p[3]] = true;
        }
        if (!grid[x + 1][y][p[4]] && s[x + 1][y] - '1' == p[6]) {
            q.push({x + 1, y, p[4], p[5], p[6], p[4], p[3], p[7], p[8]});
            grid[x + 1][y][p[4]] = true;
        }
        if (!grid[x][y - 1][p[5]] && s[x][y - 1] - '1' == p[7]) {
            q.push({x, y - 1, p[5], p[8], p[7], p[5], p[6], p[3], p[4]});
            grid[x][y - 1][p[5]] = true;
        }
        if (!grid[x][y + 1][p[5]] && s[x][y + 1] - '1' == p[8]) {
            q.push({x, y + 1, p[5], p[7], p[8], p[5], p[6], p[4], p[3]});
            grid[x][y + 1][p[5]] = true;
        }
    }
    rep(i, 6) {
        if (grid[H][W][i]) {
            cout << "YES\n";
            return 0;
        }
    }
    cout << "NO\n";
}