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 rep1(i,N) for(int i=1;i<int(N);++i)
#define all(a) (a).begin(),(a).end()
#define print(v) { cerr<<#v<<": [ "; for(auto _ : v) cerr<<_<<", "; cerr<<"]"<<endl; }
#define printpair(v) { cerr<<#v<<": [ "; for(auto _ : v) cerr<<"{"<<_.first<<","<<_.second<<"}"<<", "; cerr<<"]"<<endl; }
#define dump(x) cerr<<#x<<": "<<x<<endl;
#define bit(k) (1LL<<(k))
#define Yes "Yes"
#define No "No"
#define YES "YES"
#define NO "NO"
typedef long long ll;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

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; }

const int INF = (ll)1e9;
const ll INFLL = (ll)1e18+1;
const ll MOD = (ll)1e9+7;
const double PI = acos(-1.0);

const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const string sdir = "DRUL";

int H,W;           
int dir_v[4][6] =   {{4, 1, 0, 3, 5, 2},
                     {1, 5, 2, 0, 4, 3},
                     {2, 1, 5, 3, 0, 4},
                     {3, 0, 2, 5, 4, 1}
                  };
int v_idx[] = {4, 1, 2, 3};
bool visited[110][110];
bool IsIn(int x,int y){
    return 0<=x&&x<H&&0<=y&&y<W;
}
vector<string> s, tmp;
void debug(){
    rep(i,H){
        cerr << s[i] << endl;
    }
    cerr << endl;
}
bool can_move(vector<int>& v, int nx, int ny, int dir){
    //cerr <<sdir[dir] << endl;
    //dump(v[v_idx[dir]]);
    //cerr << "can_move: " << nx << " " << ny << " " << v[v_idx[dir]] << " " << s[nx][ny] << endl;
    //print(v);
    return v[v_idx[dir]] == s[nx][ny] - '0';
}
 
vector<int> next_v(vector<int>& v, int dir){
    vector<int> res(6, 0);
    rep(i,6){
        res[i] = v[dir_v[dir][i]];
    }
    return res;
}

void dfs(int sx, int sy, vector<int>& t){
    if(visited[sx][sy])return;
    //cerr << endl << "now: " << sx << " " << sy << endl;
    visited[sx][sy] = true;
    rep(i,4){
        int nx = sx + dx[i];
        int ny = sy + dy[i];
        if(!IsIn(nx, ny))continue;
        if(can_move(t, nx, ny, i)){
            vector<int> nv = next_v(t, i);
            dfs(nx, ny, nv);
        }
    }
    return;
}
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);

    cin >> H >> W;
    s.resize(H);
    rep(i,H)cin >> s[i];
    vector<int> ts = {6, 3, 5, 4, 2, 1};
    rep(i,H)rep(j,W){
        visited[i][j] = false;
    }
    dfs(0, 0, ts);
    if(visited[H-1][W-1]){
        cout << YES << endl;
    }
    else{
        cout << NO << endl;
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long LL;
typedef pair<int,int> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
const LL mod=998244353;
const LL LINF=1LL<<62;
const int INF=1<<30;
int dx[]={1,0,-1,0,1,-1,1,-1};
int dy[]={0,1,0,-1,1,-1,-1,1};

struct Dice
{
    //int x, y;
    int l, r, f, b, d, u;

    void RollN()
    {
        //  --y;
        int buff = d;
        d = f;
        f = u;
        u = b;
        b = buff;
    }

    void RollS()
    {
        // ++y;
        int buff = d;
        d = b;
        b = u;
        u = f;
        f = buff;
    }

    void RollL() // ----->
    {
        int buff = f;
        f = l;
        l = b;
        b = r;
        r = buff;
    }

    void RollR() // <------
    {
        int buff = f;
        f = r;
        r = b;
        b = l;
        l = buff;
    }

    void RollE() // .o -> o.
    {
        // --x;
        int buff = d;
        d = l;
        l = u;
        u = r;
        r = buff;
    }


    void RollW() // o. -> .o
    {
        //  ++x;
        int buff = d;
        d = r;
        r = u;
        u = l;
        l = buff;
    }


    vector< Dice > makeDice()
    {
        vector< Dice > ret;
        for(int i = 0; i < 6; i++) {
            Dice d(*this);
            if(i == 1) d.RollN();
            if(i == 2) d.RollS();
            if(i == 3) d.RollS(), d.RollS();
            if(i == 4) d.RollL();
            if(i == 5) d.RollR();
            for(int j = 0; j < 4; j++) {
                ret.emplace_back(d);
                d.RollE();
            }
        }
        return (ret);
    }
};

int h,w;
vector<string> s(100);
bool vis[100][100];

bool dfs(int y,int x,Dice dice){
    if(y == h - 1 && x == w - 1) return true;
    if(vis[y][x]) return false;
    vis[y][x] = true;
    for (int i = 0; i < 4; i++) {
        int ny = y + dy[i], nx = x + dx[i];
        if(0<=ny&&ny<h&&0<=nx&&nx<w){
            Dice ndice = dice;
            if(i == 0) ndice.RollW();
            if(i == 1) ndice.RollN();
            if(i == 2) ndice.RollE();
            if(i == 3) ndice.RollS();
            if(ndice.d == s[ny][nx] - '0' && dfs(ny, nx, ndice)) return true;
        }
    }
    return false;
}

int main(){
    cin >> h >> w;
    for (int i = 0; i < h; i++) {
        cin >> s[i];
    }
    Dice dice;
    dice.d = 6;
    dice.u = 1;
    dice.r = 3;
    dice.l = 4;
    dice.f = 2;
    dice.b = 5;
    if(dfs(0,0,dice)) puts("YES");
    else puts("NO");
    return 0;
}