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(x) (x).begin(),(x).end()
const int mod=1000000007,MAX=105,INF=1<<30;

struct data{
    int h;
    int w;
    int d;
    int f;
    int r;
};

bool can[MAX][MAX][7][7][7];
vector<data> G[MAX][MAX][7][7][7];

int S[MAX][MAX];
int H,W;

void DFS(data u,data p){
    for(data to:G[u.h][u.w][u.d][u.f][u.r]){
        if(to.h==p.h&&to.w==p.w&&to.d==p.d&&to.f==p.f&&to.r==p.r) continue;
        
        if(to.h<0||to.h>=H||to.w<0||to.w>=W) continue;
        
        if(to.d!=S[to.h][to.w]) continue;
        
        if(can[to.h][to.w][to.d][to.f][to.r]) continue;
        
        can[to.h][to.w][to.d][to.f][to.r]=1;
        DFS(to,u);
    }
}

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    cin>>H>>W;
    for(int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            char c;cin>>c;
            if(c=='#') S[i][j]=0;
            else S[i][j]=c-'0';
        }
    }
    
    can[0][0][6][2][3]=1;
    
    for(int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            for(int d=1;d<=6;d++){
                for(int f=1;f<=6;f++){
                    for(int r=1;r<=6;r++){
                        if(d==f) continue;
                        if(d==r) continue;
                        if(f==r) continue;
                        
                        G[i][j][d][f][r].push_back({i+1,j,f,7-d,r});
                        G[i][j][d][f][r].push_back({i,j+1,r,f,7-d});
                        G[i][j][d][f][r].push_back({i-1,j,7-f,d,r});
                        G[i][j][d][f][r].push_back({i,j-1,7-r,f,d});
                    }
                }
            }
        }
    }
    
    DFS({0,0,6,2,3},{-1,-1,-1,-1,-1});
    
    bool ok=false;
    
    for(int f=1;f<=6;f++){
        for(int r=1;r<=6;r++){
            if(can[H-1][W-1][S[H-1][W-1]][f][r]) ok=true;
        }
    }
    
    if(ok) cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define pb push_back
#define mt make_tuple
#define ALL(a) (a).begin(),(a).end()
#define FST first
#define SEC second
#define DEB cerr<<"!"<<endl
#define SHOW(a,b) cerr<<(a)<<" "<<(b)<<endl
#define vi vector<int>

using ll = long long;
const int INF = (INT_MAX/2);
const ll LLINF = (LLONG_MAX/2);
const double eps = 1e-8;
const ll DIV =1e9+7;
//const double PI = M_PI;
inline ll pow(ll x,ll n,ll m){ll r=1;while(n>0){if((n&1)==1)r=r*x%m;x=x*x%m;n>>=1;}return r%m;}
inline ll lcm(ll d1, ll d2){return d1 / __gcd(d1, d2) * d2;}
inline ll inv(ll x, ll div){return pow(x, div - 2, div);}
#define chmax(a,b) a=max(a,b)

/*Coding Space*/
class Dice{
public:
  int ue;
  int temae;
  int migi;
  Dice(int u, int t, int m){
    ue = u, temae = t, migi = m;
  }
};

using tiiiii = tuple<int,int,int,int,int>;

Dice rotate_r(Dice a){
  return Dice(7-a.migi,a.temae,a.ue);
}
Dice rotate_l(Dice a){
  return Dice(a.migi,a.temae,7-a.ue);
}
Dice rotate_oku(Dice a){
  return Dice(a.temae,7-a.ue,a.migi);
}
Dice rotate_temae(Dice a){
  return Dice(7-a.temae,a.ue,a.migi);
}
Dice dice(1,2,3);

int h,w;
vector<string> m;
map<tiiiii,bool> memo;
bool s(Dice d, int x, int y){
  if(x < 0 || x >= w || y < 0 || y >= h || m[y][x] == '#' || m[y][x] != 7 - d.ue + '0' || memo.count(tiiiii{d.ue,d.temae,d.migi,x,y}) == 1) return false;
  memo[tiiiii{d.ue,d.temae,d.migi,x,y}] = true;
  //cerr << x  << ' ' << y << ' ' << 7 - d.ue << endl;
  if(x == w - 1 && y == h - 1) return true;
  else{
    bool ret = false;
    ret |= s(rotate_r(d), x+1, y);
    ret |= s(rotate_l(d), x-1, y);
    ret |= s(rotate_oku(d), x, y-1);
    ret |= s(rotate_temae(d), x, y+1);
    return ret;
  }
}
int main(){
  cin >> h >> w;
  m.resize(h);
  rep(i,h) cin >> m[i];
  if(s(dice,0,0)) cout << "YES" << endl;
  else cout << "NO" << endl;
}

C++: #pragma GCC optimize("O3")
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;

using ll = long long;
using P = pair<int, int>;
using T = tuple<int, int, int>;

template <class T> inline T chmax(T &a, const T b) {return a = (a < b) ? b : a;}
template <class T> inline T chmin(T &a, const T b) {return a = (a > b) ? b : a;}

constexpr int MOD = 1e9 + 7;
constexpr int inf = 1e9;
constexpr long long INF = 1e18;

#define all(a) (a).begin(), (a).end()

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    int h, w; cin>>h>>w;
    vector<string> s(h);
    for(int i=0; i<h; i++) cin>>s[i];

    vector<vector<char>> base(h, vector<char>(w, '#'));
    char v0[] = {'6', '3', '1', '4'};
    char v2[] = {'1', '3', '6', '4'};
    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            if(i % 4 == 0) base[i][j] = v0[j % 4];
            if(i % 4 == 1) if(j % 2 == 0) base[i][j] = '2';
            if(i % 4 == 2) base[i][j] = v2[j % 4];
            if(i % 4 == 3) if(j % 2 == 0) base[i][j] = '5';
        }
    }

    queue<P> que;
    que.emplace(0, 0);
    vector<vector<bool>> visited(h, vector<bool>(w, false));
    visited[0][0] = true;
    
    while(que.size()){
        int cy, cx; tie(cy, cx) = que.front(); que.pop();

        if(cy == h - 1 && cx == w - 1){
            cout << "YES" << endl;
            return 0;
        }

        for(int i=0; i<4; i++){
            int ny = cy + dy[i], nx = cx + dx[i];
        
            if(!(0 <= ny && ny < h && 0 <= nx && nx < w)) continue;
            if(s[ny][nx] == '#') continue;
            if(s[ny][nx] != base[ny][nx]) continue;
            if(visited[ny][nx]) continue;

            visited[ny][nx] = true;
            que.emplace(ny, nx);
        }
    }

    cout << "NO" << endl;

    return 0;
}