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 _for(i,j,N) for(int i = (j);i < (N);i++)
#define _rep(i,j,N) for(int i = (j);i <= (N);i++)
#define _dec(i,N,j) for(int i = (N-1);i >= (j);i--)
#define _rec(i,N,j) for(int i = (N);i >=(j);i--)
#define ALL(x) x.begin(),x.end()
#define MEM(a,n) memset(a,n,sizeof(a))
#define fst first
#define scd second
#define PB push_back
#define MP make_pair

typedef long long LL;
typedef long double LD;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

int get_int(){int x;scanf("%d",&x);return x;}
LL get_ll(){LL x;scanf("%lld",&x);return x;}
double get_db(){double x;scanf("%lf",&x);return x;}

template<typename T> int chmin(T &a,T b){if(a > b){a = b;return 1;}else return 0;}
template<typename T> int chmax(T &a,T b){if(a < b){a = b;return 1;}else return 0;}
template<typename T>
ostream& operator<<(ostream& os,const vector<T>& v) {_for(i,0,v.size()) os << v[i] << " ";return os;}
template<typename T>
ostream& operator<<(ostream& os,const set<T>& v){for(auto a:v) os << a <<" ";return os;}
template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& v){os << v.fst <<" "<<v.scd << " ";return os;}
template<typename T1,typename T2>
ostream& operator<<(ostream& os,const map<T1,T2>& v){for(auto a:v) cout << a << endl;return os;}

static const int dx[8] = { -1,  0, 1, 0,  1, -1,  1, -1};
static const int dy[8] = {0,  1, 0,  -1,  1, -1, -1,  1};
const double pi  = acos(-1.0);
const double eps = 1e-8;
const LL  INF  = 0x3f3f3f3f3f3f3f3f;
const int inf  = 0x3f3f3f3f;

char G[105][105];
int vis[105][105][7][7];
int H,W;

struct dice{
    int a[6];
    int x,y;
    dice(int x = 0,int y = 0):x(x),y(y){
        a[0] = 1;
        a[1] = 6;
        a[2] = 4;
        a[3] = 3;
        a[4] = 2;
        a[5] = 5;
    }
    int rr(){
        int temp = a[0];
        a[0] = a[2];
        a[2] = a[1];
        a[1] = a[3];
        a[3] = temp;
        y += 1;
        return a[1];
    }
    int lr(){
        int temp = a[0];
        a[0] = a[3];
        a[3] = a[1];
        a[1] = a[2];
        a[2] = temp;
        y -= 1;
        return a[1];
    }
    int ur(){
        int temp = a[0];
        a[0] = a[4];
        a[4] = a[1];
        a[1] = a[5];
        a[5] = temp;
        x -= 1;
        return a[1];

    }
    int dr(){
        int temp = a[0];
        a[0] = a[5];
        a[5] = a[1];
        a[1] = a[4];
        a[4] = temp;
        x += 1;
        return a[1];
    }
    bool mark(){
        //cout << x <<" " <<y << endl;
        if(x < 0 || y < 0 || x >= H || y >= W) return false;
        if( (vis[x][y][a[3]][a[4]]) || (a[1] != G[x][y] - '0') ) return false;
        else{
            vis[x][y][a[3]][a[4]] = 1;
            return true;
        }
    }

};




int main()
{
    cin >> H >> W;
    getchar();
    _for(i,0,H){
        _for(j,0,W){
            char c = getchar();
            G[i][j] = c;
        }
        getchar();
    }
    MEM(vis,0);
    dice d;
    d.mark();
    queue<dice> que;
    que.push(d);
    while(!que.empty()){
        dice dd = que.front();que.pop();
        dice k;

        memcpy(&k,&dd,sizeof(dd));
        k.rr();
        if(k.mark()){
            que.push(k);
            //cout << "ok" << endl;
        }

        memcpy(&k,&dd,sizeof(dd));
        k.lr();
        if(k.mark()){
            que.push(k);
        }

       memcpy(&k,&dd,sizeof(dd));
        k.ur();
           if(k.mark()){
            que.push(k);
        }

        memcpy(&k,&dd,sizeof(dd));
        k.dr();
           if(k.mark()){
            que.push(k);
            //cout << "ok" << endl;
        }
    }

    int OK = 0;
    _for(i,0,7){
        _for(j,0,7){
            if(vis[H-1][W-1][i][j]) OK = 1;
        }
    }
    if(OK) cout <<"YES"<< endl;
    else cout << "NO" << endl;
    return 0;
}