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 <iostream> // cin, cout, cerr
#include <algorithm> // minmax, sort, swap
#include <numeric> // iota
#include <cstdio> // printf, scanf
#include <string> // string, stoi, to_string
#include <vector> // vector
#include <queue> // queue, priority_queue
#include <deque> // deque
#include <map> // key-value pairs sorted by keys
#include <set> // set
#include <iomanip> // cout<<setprecision(n)
#include <functional> // function<void(int)>
#include <cmath>
#include <cassert>
#include <bitset>

#ifdef DEBUG
#include "debug.hpp"
#else
#define debug(...)
#endif

#define int long long // at least int64 > 9*10^18
#define EL '\n'
#define rep(i,n) for(int i = 0; i < (n); i++)
#define print(i) std::cout << (i) << '\n'
#define all(v) (v).begin(), (v).end()
/* libraries */

struct UnionFind
{
  int n;
  std::vector<int> par,size;
  UnionFind(int sz) : n(sz), par(sz,0), size(sz,1) {
    std::iota(par.begin(),par.end(),0);
  }
  int find(int x) {
    if(par[x]==x) return x;
    return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x=find(x);y=find(y);
    if(x==y) return;
    if(size[x]<size[y]) std::swap(x,y);
    size[x]+=size[y];
    par[y]=x;
  }
};


signed main() {
	int clar[4][4] = {
		6,3,1,4,
		2,-1,2,-1,
		1,3,6,4,
		5,-1,5,-1
	};
	int h,w;
	std::cin >> h >> w;
	std::vector<std::string> g(h);
	rep(i,h) std::cin >> g[i];
	auto enc = [&w](int x, int y) -> int {
		return x*w+y;
	};
	UnionFind uf(h*w);
	rep(i,h) {
		rep(j,w) {
			if(g[i][j]-'0'!=clar[i%4][j%4]) continue;
			rep(dx,2) rep(dy,2) {
				debug(dx,dy);
				if(dx==0 && dy==0) continue;
				if(dx*dy!=0) continue;
				if(i+dx>=h || j+dy>=w) continue;
				if(g[i+dx][j+dy]-'0'!=clar[(i+dx)%4][(j+dy)%4]) continue;
				debug(enc(i,j),enc(j+dx,j+dy));
				uf.unite(enc(i,j),enc(i+dx,j+dy));
			}
		}
	}
	debug(uf.par);
	if(uf.find(0)==uf.find(h*w-1)) print("YES");
	else print("NO");

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> P;

#define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0)
#define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0)

template<typename T1,typename T2>ostream& operator<<(ostream& os,const pair<T1,T2>& a) {os << "(" << a.first << ", " << a.second << ")";return os;}

const char newl = '\n';
struct Dice
{
  // int x, y;
  int l, r, f, b, d, u;

  void RollS()
  {
    //  --y;
    int buff = d;
    d = f;
    f = u;
    u = b;
    b = buff;
  }

  void RollN()
  {
    // ++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;
  }


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

struct tapi {
    int x,y;
    Dice d;
    int get() {
        return d.d+d.f*6+d.r*36;
    }
};

int main() {
    int h,w;
    cin >> h >> w;
    string s[100];
    vector<vector<vector<bool>>> used(220,vector<vector<bool>>(h,vector<bool>(w,false)));
    queue<tapi> que;
    for (int i = 0;i < h;++i) cin >> s[i];
    for (int i = 0;i < h;++i) for (int j = 0;j < w;++j) if (s[i][j] == '6') s[i][j] = '0';
    que.push(tapi{0,0,Dice{4,3,2,5,0,1}});
    used[que.front().get()][0][0] = true;
    while (!que.empty()) {
        tapi t = que.front();que.pop();
        if (t.x > 0) {
            t.d.RollN();
            t.x--;
            if (t.d.d == s[t.x][t.y]-'0' && !used[t.get()][t.x][t.y]) {
                que.push(t);
                used[t.get()][t.x][t.y] = true;
            }
            t.d.RollS();
            t.x++;
        }
        if (t.x < h-1) {
            t.d.RollS();
            t.x++;
            if (t.d.d == s[t.x][t.y]-'0' && !used[t.get()][t.x][t.y]) {
                que.push(t);
                used[t.get()][t.x][t.y] = true;
            }
            t.d.RollN();
            t.x--;
        }
        if (t.y > 0) {
            t.d.RollE();
            t.y--;
            if (t.d.d == s[t.x][t.y]-'0' && !used[t.get()][t.x][t.y]) {
                que.push(t);
                used[t.get()][t.x][t.y] = true;
            }
            t.d.RollW();
            t.y++;
        }
        if (t.y < w-1) {
            t.d.RollW();
            t.y++;
            if (t.d.d == s[t.x][t.y]-'0' && !used[t.get()][t.x][t.y]) {
                que.push(t);
                used[t.get()][t.x][t.y] = true;
            }
            t.d.RollE();
            t.y--;
        }
    }
    bool ans = false;
    for (int i = 0;i < 220;++i) if (used[i][h-1][w-1]) ans = true;
    cout << (ans ? "YES" : "NO") << endl;
}