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;
using ll=long long;
#define fr(i,n) for(int i=0;i<(n);++i)
#define Fr(i,n) for(int i=1;i<=(n);++i)
#define ifr(i,n) for(int i=(n)-1;i>=0;--i)
#define iFr(i,n) for(int i=(n);i>0;--i)
using P=pair<int,int>;

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

struct PD{
    P p;
    Dice d;
    PD(P p,Dice d):p(p),d(d){}
    bool operator<(const PD &x) const {return this->p<x.p;}
};

map<PD,bool> mp;

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    istream& in(cin);
    ostream& out(cout);
    Dice D;
    D.u=1;D.f=2;D.r=3,D.l=4,D.b=5,D.d=6;
    int h,w;
    in>>h>>w;
    vector<string> s(h);
    for(auto&c:s) in>>c;
    if(s[0][0]!='6') return puts("NO"),0;
    queue<pair<P,Dice>> q;
    q.emplace(make_pair(0,0),D);
    while(!q.empty()){
        P p;
        int i,j;
        Dice d;
        tie(p,d)=q.front();
        tie(i,j)=p;
        q.pop();
        if(i==h-1&&j==w-1) return puts("YES"),0;
        if(i>0&&s[i-1][j]!='#'){
            d.RollS();
            if(d.d==s[i-1][j]-'0'&&!mp[PD(P(i-1,j),d)]){
                mp[PD(P(i-1,j),d)]=true;
                q.emplace(make_pair(i-1,j),d);
            }
            d.RollN();
        }
        if(i<h-1&&s[i+1][j]!='#'){
            d.RollN();
            if(d.d==s[i+1][j]-'0'&&!mp[PD(P(i+1,j),d)]){
                mp[PD(P(i+1,j),d)]=true;
                q.emplace(make_pair(i+1,j),d);
            }
            d.RollS();
        }
        if(j>0&&s[i][j-1]!='#'){
            d.RollE();
            if(d.d==s[i][j-1]-'0'&&!mp[PD(P(i,j-1),d)]){
                mp[PD(P(i,j-1),d)]=true;
                q.emplace(make_pair(i,j-1),d);
            }
            d.RollW();
        }
        if(j<w-1&&s[i][j+1]!='#'){
            d.RollW();
            if(d.d==s[i][j+1]-'0'&&!mp[PD(P(i,j+1),d)]){
                mp[PD(P(i,j+1),d)]=true;
                q.emplace(make_pair(i,j+1),d);
            }
            d.RollE();
        }
    }
    puts("NO");
}