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;
typedef long long ll;
#define all(v) v.begin(), v.end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define fs first
#define sc second
#define _GLIBCXX_DEBUG

int dxy[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
int h, w;
vector<vector<bool>> visited(100, vector<bool>(100, false));
vector<vector<int>> s(100, vector<int>(100, 0));

vector<vector<int>> rotate(int k, vector<vector<int>> dice)
{
    if(k==1){
        dice[1][2] = dice[1][1];
        dice[1][1] = dice[1][0];
        dice[1][0] = 7-dice[1][2];
    }
    if (k == 0)
    {
        dice[1][0] = dice[1][1];
        dice[1][1] = dice[1][2];
        dice[1][2] = 7 - dice[1][0];
    }
    if (k == 3)
    {
        dice[2][1] = dice[1][1];
        dice[1][1] = dice[0][1];
        dice[0][1] = 7 - dice[2][1];
    }
    if (k == 2)
    {
        dice[0][1] = dice[1][1];
        dice[1][1] = dice[2][1];
        dice[2][1] = 7 - dice[0][1];
    }
    return dice;
}

void dfs(int x, int y, vector<vector<int>> dice)
{
    if(x == h-1 && y == w-1){
        cout << "YES\n";
        exit(0);
    }
    visited[x][y] = true;
    rep(i,4){
        int x2 = x+dxy[i][0];
        int y2 = y+dxy[i][1];
        if(0 <= x2 && x2 < h && 0 <= y2 && y2 < w){
            if(s[x2][y2] == dice[1+dxy[i][0]][1+dxy[i][1]] && !visited[x2][y2]){
                dfs(x2,y2,rotate(i,dice));
            }
        }
    }
}

int main(void){
    vector<vector<int>> dice(3, vector<int>(3, 0));
    dice[1][1] = 6;
    dice[1][0] = 4;
    dice[1][2] = 3;
    dice[0][1] = 5;
    dice[2][1] = 2;
    cin >> h >> w;
    rep(i,h){
        string tmp;
        cin >> tmp;
        rep(j,w){
            if(tmp[j] != '#'){
                s[i][j] = tmp[j] - '0';
            }
        }
    }
    dfs(0,0,dice);
    cout << "NO\n";
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;

const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll INF = 1e16;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

void init() {cin.tie(0);ios::sync_with_stdio(false);cout << fixed << setprecision(15);}
//--------------------------------------------------------------------------------//
ll H,W;
bool can=false;
vvc<bool> used;
void dfs(ll h,ll w, ll lpre, ll upre, ll now, vc<string>& S){
    used[h][w] = true;
    if(h==H-1&&w==W-1){
        can = true;
        return;
    }

    if(h+1<H&&!used[h+1][w]&&S[h+1][w]=='0'+7-upre){
        dfs(h + 1, w, lpre, now, 7 - upre, S);
    }
    if(h-1>=0&&!used[h-1][w]&&S[h-1][w]=='0'+upre){
        dfs(h - 1, w, lpre, 7 - now, upre, S);
    }
    if(w+1<W&&!used[h][w+1]&&S[h][w+1]=='0'+7-lpre){
        dfs(h, w + 1, now, upre, 7 - lpre, S);
    }
    if(w-1>=0&&!used[h][w-1]&&S[h][w-1]=='0'+lpre){
        dfs(h, w - 1, 7 - now, upre, lpre, S);
    }

}
int main() {
    init();
    cin >> H >> W;
    vc<string> S(H);
    rep(i, H) cin >> S[i];
    if(H%2==0&&W%2==0){
        cout << "NO" << endl;
        return 0;
    }
    used.resize(H);
    rep(i, H) used[i].resize(W);

    dfs(0, 0, 4, 5, 6, S);
    cout << (can ? "YES" : "NO") << endl;
}