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>
#include<unordered_set>
#include<unordered_map>
using namespace std;
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;
const ll inf=1e9+7;
const ll mod=1e9+7;
int main(){
    ll h,w;
    cin>>h>>w;
    vector<vector<char> >s(h+2,vector<char>(w+2));
    REP(i,1,h+1){
        REP(j,1,w+1){
            cin>>s[i][j];
        }
    }
    vector<vector<ll> >visited(h+2,vector<ll>(w+2));
    queue<pll>q;
    visited[1][1]=1;
    q.push(mp(1,1));
    while(!q.empty()){
        pll k=q.front();
        q.pop();
        ll x=k.first,y=k.second;
        if(x%4==1){
            if(y%4==1){
                if(x+2<=h){
                    if(s[x+1][y]=='2'&&s[x+2][y]=='1'&&!visited[x+2][y]){
                        q.push(mp(x+2,y));
                        visited[x+2][y]=1;
                    }
                }
                if(x+1<=h){
                    if(s[x+1][y]=='2'){
                        visited[x+1][y]=1;
                    }
                }
                if(x-2>=0){
                    if(s[x-1][y]=='5'&&s[x-2][y]=='1'&&!visited[x-2][y]){
                        q.push(mp(x-2,y));
                        visited[x-2][y]=1;
                    }
                }
                if(x-1>=0){
                    if(s[x-1][y]=='5'){
                        visited[x-1][y]=1;
                    }
                }
                if(y+2<=w){
                    if(s[x][y+1]=='3'&&s[x][y+2]=='1'&&!visited[x][y+2]){
                        q.push(mp(x,y+2));
                        visited[x][y+2]=1;
                    }
                }
                if(y+1<=w){
                    if(s[x][y+1]=='3'){
                        visited[x][y+1]=1;
                    }
                }
                if(y-2>=0){
                    if(s[x][y-1]=='4'&&s[x][y-2]=='1'&&!visited[x][y-2]){
                        q.push(mp(x,y-2));
                        visited[x][y-2]=1;
                    }
                }
                if(y-1>=0){
                    if(s[x][y-1]=='4'){
                        visited[x][y-1]=1;
                    }
                }
            }else{
                if(x+2<=h){
                    if(s[x+1][y]=='2'&&s[x+2][y]=='6'&&!visited[x+2][y]){
                        q.push(mp(x+2,y));
                        visited[x+2][y]=1;
                    }
                }
                if(x+1<=h){
                    if(s[x+1][y]=='2'){
                        visited[x+1][y]=1;
                    }
                }
                if(x-2>=0){
                    if(s[x-1][y]=='5'&&s[x-2][y]=='6'&&!visited[x-2][y]){
                        q.push(mp(x-2,y));
                        visited[x-2][y]=1;
                    }
                }
                if(x-1>=0){
                    if(s[x-1][y]=='5'){
                        visited[x-1][y]=1;
                    }
                }
                if(y+2<=w){
                    if(s[x][y+1]=='4'&&s[x][y+2]=='6'&&!visited[x][y+2]){
                        q.push(mp(x,y+2));
                        visited[x][y+2]=1;
                    }
                }
                if(y+1<=w){
                    if(s[x][y+1]=='4'){
                        visited[x][y+1]=1;
                    }
                }
                if(y-2>=0){
                    if(s[x][y-1]=='3'&&s[x][y-2]=='6'&&!visited[x][y-2]){
                        q.push(mp(x,y-2));
                        visited[x][y-2]=1;
                    }
                }
                if(y-1>=0){
                    if(s[x][y-1]=='3'){
                        visited[x][y-1]=1;
                    }
                }
            }
        }
        if(x%4==3){
            if(y%4==1){
                if(x+2<=h){
                    if(s[x+1][y]=='5'&&s[x+2][y]=='6'&&!visited[x+2][y]){
                        q.push(mp(x+2,y));
                        visited[x+2][y]=1;
                    }
                }
                if(x+1<=h){
                    if(s[x+1][y]=='5'){
                        visited[x+1][y]=1;
                    }
                }
                if(x-2>=0){
                    if(s[x-1][y]=='2'&&s[x-2][y]=='6'&&!visited[x-2][y]){
                        q.push(mp(x-2,y));
                        visited[x-2][y]=1;
                    }
                }
                if(x-1>=0){
                    if(s[x-1][y]=='2'){
                        visited[x-1][y]=1;
                    }
                }
                if(y+2<=w){
                    if(s[x][y+1]=='3'&&s[x][y+2]=='6'&&!visited[x][y+2]){
                        q.push(mp(x,y+2));
                        visited[x][y+2]=1;
                    }
                }
                if(y+1<=w){
                    if(s[x][y+1]=='3'){
                        visited[x][y+1]=1;
                    }
                }
                if(y-2>=0){
                    if(s[x][y-1]=='4'&&s[x][y-2]=='6'&&!visited[x][y-2]){
                        q.push(mp(x,y-2));
                        visited[x][y-2]=1;
                    }
                }
                if(y-1>=0){
                    if(s[x][y-1]=='4'){
                        visited[x][y-1]=1;
                    }
                }
            }else{
                if(x+2<=h){
                    if(s[x+1][y]=='5'&&s[x+2][y]=='1'&&!visited[x+2][y]){
                        q.push(mp(x+2,y));
                        visited[x+2][y]=1;
                    }
                }
                if(x+1<=h){
                    if(s[x+1][y]=='5'){
                        visited[x+1][y]=1;
                    }
                }
                if(x-2>=0){
                    if(s[x-1][y]=='2'&&s[x-2][y]=='1'&&!visited[x-2][y]){
                        q.push(mp(x-2,y));
                        visited[x-2][y]=1;
                    }
                }
                if(x-1>=0){
                    if(s[x-1][y]=='2'){
                        visited[x-1][y]=1;
                    }
                }
                if(y+2<=w){
                    if(s[x][y+1]=='4'&&s[x][y+2]=='1'&&!visited[x][y+2]){
                        q.push(mp(x,y+2));
                        visited[x][y+2]=1;
                    }
                }
                if(y+1<=w){
                    if(s[x][y+1]=='4'){
                        visited[x][y+1]=1;
                    }
                }
                if(y-2>=0){
                    if(s[x][y-1]=='3'&&s[x][y-2]=='1'&&!visited[x][y-2]){
                        q.push(mp(x,y-2));
                        visited[x][y-2]=1;
                    }
                }
                if(y-1>=0){
                    if(s[x][y-1]=='3'){
                        visited[x][y-1]=1;
                    }
                }
            }
        }
    }
    if(visited[h][w])cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
}