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;

struct dice {
public:
    int soko;
    int r;
    int oku;
    dice(int soko,int r,int oku):soko(soko),r(r),oku(oku) {}

    dice rotr(){
        return dice(r,7-soko,oku);
    }

    dice rotd(){
        return dice(7-oku,r,soko);
    }

    dice rotl() {
        return dice(7-r,soko,oku);
    }

    dice rotu(){
        return dice(oku,r,7-soko);
    }


};
bool operator< (dice a,dice b){
    return true;
}

int main() {
    int h,w;
    cin>>h>>w;
    vector<string> mp(h);
    for(int i = 0;i < h;++i) {
        cin>>mp[i];
    }
    queue<pair<pair<int,int>,dice>> q;
    q.push(make_pair(make_pair(0,0),dice(6,3,5)));
    bool ok=false;
    vector<vector<bool>> visited(h,vector<bool>(w,false));
    while(!q.empty()){
        auto t=q.front();
        int y=t.first.first;
        int x=t.first.second;
    //cerr<<"y:"<<y<<" x:"<<x<<endl;
        auto d=t.second;
        q.pop();
        if(y==h-1&&x==w-1){
            ok=true;
            break;
        }
//cerr<<d.rotr().soko<<endl;

        if(x+1<w&&!visited[y][x+1]&&mp[y][x+1]!='#'&&d.rotr().soko+'0'==mp[y][x+1]){
            q.push(make_pair(make_pair(y,x+1),d.rotr()));
            visited[y][x+1]=true;
        }
        if(y+1<h&&!visited[y+1][x]&&mp[y+1][x]!='#'&&d.rotd().soko+'0'==mp[y+1][x]){
            q.push(make_pair(make_pair(y+1,x),d.rotd()));
            visited[y+1][x]=true;
        }
        if(x-1>=0&&!visited[y][x-1]&&mp[y][x-1]!='#'&&d.rotl().soko+'0'==mp[y][x-1]){
            q.push(make_pair(make_pair(y,x-1),d.rotl()));
            visited[y][x-1]=true;
        }
        if(y-1>=0&&!visited[y-1][x]&&mp[y-1][x]!='#'&&d.rotu().soko+'0'==mp[y-1][x]){
            q.push(make_pair(make_pair(y-1,x),d.rotu()));
            visited[y-1][x]=true;
        }
    }
    if(ok)cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep1(i, n) for (ll i = 1; i <= (n); ++i)
#define rrep(i, n) for (ll i = (n - 1); i >= 0; --i)
#define ALL(obj) (obj).begin(), (obj).end()
#define pb push_back
#define to_s to_string
#define len(v) (ll)v.size()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define print(x) cout << (x) << '\n'
#define debug(x) cout << #x << ": " << (x) << '\n'
using namespace std;
using ll = long long;
typedef pair<ll, ll> P;
ll MOD = 1e9 + 7;
ll devc(ll x, ll y) { return 1 + (x - 1) / y; }

struct dice{
    ll x, y, z;
};

dice roll(dice d, ll dir){
    ll x = d.x, y = d.y, z = d.z;
    if(dir == 0) d.x = y, d.y = 7 - x;
    if(dir == 1) d.x = z, d.z = 7 - x;
    if(dir == 2) d.x = 7 - y, d.y = x;
    if(dir == 3) d.x = 7 - z, d.z = x;
    return d;
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(20);

    ll H, W;
    cin >> H >> W;
    int s[105][105];
    rep(i, H + 2) rep(j, W + 2) if(i == 0 || j == 0 || i == H + 1 || j == W + 1) s[i][j] = -1;

    rep1(i,H){
        string S;
        cin >> S;
        rep1(j, len(S)){
            if(S[j - 1] == '#') s[i][j] = -1;
            else s[i][j] = S[j - 1] - '0';
        }
    }

    stack<pair<P,dice>> sk;
    sk.push({{1, 1},{6,2,3}});

    while(!sk.empty()){
        ll h = sk.top().first.first, w = sk.top().first.second;
        dice d = sk.top().second;
        sk.pop();
        if(s[h][w] != d.x) continue;
        if (h == H && w == W){
            print("YES");
            return 0;
        }

        sk.push({{h + 1, w}, roll(d, 0)});
        sk.push({{h, w + 1}, roll(d, 1)});
        sk.push({{h - 1, w}, roll(d, 2)});
        sk.push({{h, w - 1}, roll(d, 3)});

        s[h][w] = -1;
    }

    print("NO");

    return 0;
}

C++: #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> rot = {
  {4, 2, 0, 3, 1, 5},
  {2, 4, 1, 3, 0, 5},
  {5, 3, 2, 0, 4, 1},
  {3, 5, 2, 1, 4, 0},
};
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
set<vector<int>> g[101][101];
signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  int h, w;
  cin >> h >> w;
  vector<string> in(h);
  for (int i = 0; i < h; i++) cin >> in[i];
  g[0][0].insert({6, 1, 2, 3, 5, 4});
  vector<pair<int, int>> que;
  vector<vector<bool>> vis(h, vector<bool>(w));
  vis[0][0] = true;
  que.emplace_back(0, 0);
  for (int u = 0; u < (int) que.size(); u++) {
    for (int i = 0; i < 4; i++) {
      int x = que[u].first + dx[i];
      int y = que[u].second + dy[i];
      if (x < 0 || x >= h || y < 0 || y >= w || in[x][y] == '#') continue;
      for (auto& v : g[que[u].first][que[u].second]) {
        vector<int> nxt(6);
        for (int j = 0; j < 6; j++) {
          nxt[rot[i][j]] = v[j];
        }
        if (nxt[0] != (in[x][y] - '0')) continue;
        g[x][y].insert(nxt);
        if (!vis[x][y]) {
          vis[x][y] = true;
          que.emplace_back(x, y);
        } else {
        }
      }
    }
  }
  // for (int i = 0; i < h; i++) {
  //   for (int j = 0; j < w; j++) cerr << vis[i][j] << " ";
  //   cerr << endl;
  // }
  cout << (vis[h - 1][w - 1] ? "YES\n" : "NO\n");
  return 0;
}