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 ALL(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<n;i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl; 
#define INF 1000000000
#define mod 1000000007
using ll=long long;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

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

    void o(){
        cout<<d<<endl;
    }

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

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

    int h,w;cin>>h>>w;
    string s[h];
    rep(i,h) cin>>s[i];
    bool check[h][w]={};
    
    queue<pair<Dice,pair<int,int>>> que;
    Dice d[h][w];
    d[0][0].r=3;d[0][0].l=4;d[0][0].f=2;
    d[0][0].b=5;d[0][0].d=6;d[0][0].u=1;
    check[0][0]=true;
    que.push(make_pair(d[0][0],make_pair(0,0)));
    
    while(!que.empty()){
        auto p=que.front();que.pop();
        auto pos=p.second;auto dice=p.first;
        int i=pos.first,j=pos.second;
        // cout<<"ima ";dice.o();
        if(i-1>=0 and !check[i-1][j]){
            dice.RollS();
            // dice.o();
            if(dice.d+'0'==s[i-1][j]){
                check[i-1][j]=true;
                que.push(make_pair(dice,make_pair(i-1,j)));
            }
            dice.RollN();
        }
        if(i+1<h and !check[i+1][j]){
            dice.RollN();
            // dice.o();
            if(dice.d+'0'==s[i+1][j]){
                check[i+1][j]=true;
                que.push(make_pair(dice,make_pair(i+1,j)));
            }
            dice.RollS();
        }
        if(j-1>=0 and !check[i][j-1]){
            dice.RollE();
            // dice.o();
            if(dice.d+'0'==s[i][j-1]){
                check[i][j-1]=true;
                que.push(make_pair(dice,make_pair(i,j-1)));
            }
            dice.RollW();
        }
        if(j+1<w and !check[i][j+1]){
            dice.RollW();
            // dice.o();
            if(dice.d+'0'==s[i][j+1]){
                check[i][j+1]=true;
                que.push(make_pair(dice,make_pair(i,j+1)));
            }
            dice.RollE();
        }
    }
    
    if(check[h-1][w-1])     cout<<"YES"<<endl;
    else                    cout<<"NO"<<endl;
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef long double D;
//typedef complex<D> P;
#define F first
#define S second
const ll MOD=1000000007;
//const ll MOD=998244353;

template<typename T,typename U>istream & operator >> (istream &i,pair<T,U> &A){i>>A.F>>A.S; return i;}
template<typename T>istream & operator >> (istream &i,vector<T> &A){for(auto &I:A){i>>I;} return i;}
template<typename T,typename U>ostream & operator << (ostream &o,const pair<T,U> &A){o<<A.F<<" "<<A.S; return o;}
template<typename T>ostream & operator << (ostream &o,const vector<T> &A){int i=A.size(); for(auto &I:A){o<<I<<(--i?" ":"");} return o;}

vector<int> uft;

int parent(int a){return uft[a]==a?a:uft[a]=parent(uft[a]);}

void merge(int a,int b){uft[parent(a)]=parent(b);}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int H,W;
  cin>>H>>W;
  vector<string> A(H);
  cin>>A;
  vector<string> C={{'6','3','1','4'},{'2','#','2','#'},{'1','3','6','4'},{'5','#','5','#'}};
  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      if(A[i][j]!=C[i%4][j%4]){A[i][j]='#';}
    }
  }
  uft.resize(H*W);
  for(int i=0;i<H*W;i++){uft[i]=i;}
  vector<int> dx={1,0,-1,0};
  vector<int> dy={0,1,0,-1};
  for(int i=0;i<H;i++){
    for(int j=0;j<W;j++){
      for(int k=0;k<4;k++){
        int x=i+dx[k],y=j+dy[k];
        if(0<=x && x<H && 0<=y && y<W && A[i][j]!='#' && A[x][y]!='#'){merge(x*W+y,i*W+j);}
      }
    }
  }
  cout<<(parent(H*W-1)==parent(0)?"YES":"NO")<<endl;
  

  return 0;
}