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>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;




//0:中央 1:南 2:東 3:西 4:北 5:裏側
struct Dice{

	void roll(char dst){
		for(int i = 0; i < 6; i++) work[i] = number[i];
		switch(dst){
		case 'E':
			setNumber(work[3],work[1],work[0],work[5],work[4],work[2]);
			break;
		case 'N':
			setNumber(work[1],work[5],work[2],work[3],work[0],work[4]);
			break;
		case 'S':
			setNumber(work[4],work[0],work[2],work[3],work[5],work[1]);
			break;
		case 'W':
			setNumber(work[2],work[1],work[5],work[0],work[4],work[3]);
			break;
		}
	};

	int getBottom(){
		return number[5];
	}
	int getS(){

		return number[1];
	}
	int getE(){

		return number[2];
	}

	void copy(Dice arg_dice){

		for(int i = 0; i < 6; i++){

			number[i] = arg_dice.number[i];
		}
	}

	void setNumber(int n0,int n1,int n2,int n3,int n4,int n5){
		number[0] = n0;
		number[1] = n1;
		number[2] = n2;
		number[3] = n3;
		number[4] = n4;
		number[5] = n5;
	}

	int number[6],work[6];
};

struct Info{

	Info(int arg_row,int arg_col,Dice arg_dice){
		row = arg_row;
		col = arg_col;
		dice = arg_dice;
	}
	int row,col;
	Dice dice;
};


int H,W;
char table[105][105];
bool visited[105][105][7][7][7];

bool rangeCheck(int row,int col){

	return row >= 0 && row <= H-1 && col >= 0 && col <= W-1;
}


int main(){

	scanf("%d %d",&H,&W);

	for(int row = 0; row < H; row++){

		scanf("%s",table[row]);
	}

	for(int row = 0; row < H; row++){
		for(int col = 0; col < W; col++){
			for(int a = 1; a <= 6; a++){
				for(int b = 1; b <= 6; b++){
					for(int c = 1; c <= 6; c++){

						visited[row][col][a][b][c] = false;
					}
				}
			}
		}
	}

	Dice dice;
	dice.setNumber(1,2,3,4,5,6);

	visited[0][0][dice.getBottom()][dice.getS()][dice.getE()] = true;

	queue<Info> Q;
	Q.push(Info(0,0,dice));

	int diff_row[4] = {-1,0,0,1},diff_col[4] = {0,-1,1,0};
	char DIR[4] = {'N','W','E','S'};

	while(!Q.empty()){

		if(Q.front().row == H-1 && Q.front().col == W-1){

			printf("YES\n");
			return 0;
		}else{

			for(int i = 0; i < 4; i++){

				int adj_row = Q.front().row+diff_row[i];
				int adj_col = Q.front().col+diff_col[i];

				if(!rangeCheck(adj_row,adj_col)||table[adj_row][adj_col] == '#')continue;

				dice.copy(Q.front().dice);
				dice.roll(DIR[i]);

				if(dice.getBottom() != table[adj_row][adj_col]-'0')continue;

				if(visited[adj_row][adj_col][dice.getBottom()][dice.getS()][dice.getE()])continue;
				visited[adj_row][adj_col][dice.getBottom()][dice.getS()][dice.getE()] = true;

				Q.push(Info(adj_row,adj_col,dice));
			}

			Q.pop();
		}
	}

	printf("NO\n");

	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

struct dice {
    int u, d, l, r, f, b;

    void spinl(){ // f -> l
        int tmp = f; f = r; r = b; b = l; l = tmp;
    }
    void spinr(){
        for (int i = 0; i < 3; ++i) spinl();
    }

    void gof(){
        int tmp = f; f = u; u = b; b = d; d = tmp;
    }

    void gob(){
        for (int i = 0; i < 3; ++i) gof();
    }

    void gor(){
        int tmp = u; u = l; l = d; d = r; r = tmp;
    }

    void gol(){
        for (int i = 0; i < 3; ++i) gor();
    }

    dice(int a, int x) {
        u = 1, d = 6, f = 2, b = 5, l = 4, r = 3;
        if(a == 2) gob();
        else if(a == 3) gol();
        else if(a == 4) gor();
        else if(a == 5) gof();
        else if(a == 6) gof(), gof();
        while(f != x) spinl();
    }
    dice(){
        u = 0, d = 0, f = 0, b = 0, l = 0, r = 0;
    }
};

int main() {
    dice D(1, 2);
    int h, w;
    cin >> h >> w;
    vector<vector<int>> v(h+2, vector<int>(w+2, 0));

    for (int i = 0; i < h; ++i) {
        string s;
        cin >> s;
        for (int j = 0; j < w; ++j) {
            if(s[j] != '#') v[i+1][j+1] = s[j] - '0';
        }
    }
    vector<vector<dice>> a(h+2, vector<dice>(w+2));
    a[1][1] = D;
    queue<pair<int, int>> Q;
    Q.emplace(1, 1);
    while(!Q.empty()){
        int y, x; tie(y, x) = Q.front(); Q.pop();
        dice d = a[y][x];
        if(v[y-1][x] && a[y-1][x].r == 0){
            d.gob();
            if(d.d == v[y-1][x]){
                Q.emplace(y-1, x);
                a[y-1][x] = d;
            }
            d.gof();
        }
        if(v[y+1][x] && a[y+1][x].r == 0){
            d.gof();
            if(d.d == v[y+1][x]){
                Q.emplace(y+1, x);
                a[y+1][x] = d;
            }
            d.gob();
        }
        if(v[y][x-1] && a[y][x-1].r == 0){
            d.gol();
            if(d.d == v[y][x-1]){
                Q.emplace(y, x-1);
                a[y][x-1] = d;
            }
            d.gor();
        }
        if(v[y][x+1] && a[y][x+1].r == 0){
            d.gor();
            if(d.d == v[y][x+1]){
                Q.emplace(y, x+1);
                a[y][x+1] = d;
            }
            d.gol();
        }
    }
    if(a[h][w].d){
        puts("YES");
    }else {
        puts("NO");
    }
    return 0;
}