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++: #pragma target("avx")
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef unordered_map<ll, ll> U_MAP;
typedef priority_queue<ll> pq;
typedef priority_queue<ll, vector<ll>, greater<ll>> rpq;
constexpr ll INF = 1e9, MOD = 1e9 + 7, ohara = 1e6 + 10;
constexpr ll LINF = 1e18;

#define rep(i, n) for (ll(i) = 0; (i) < (int)(n); (i)++)
#define rrep(i, a, b) for (ll i = (a); i < (b); i++)
#define rrrep(i, a, b) for (ll i = (a); i >= (b); i--)
#define all(v) (v).begin(), (v).end()
#define Size(n) (n).size()
#define Cout(x) cout << (x) << endl
#define doublecout(a) cout << fixed << setprecision(15) << a << endl;
#define fi first
#define se second
#define m_p make_pair
#define p_b push_back
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto& x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

//------ Believe yourself as a genius!!!!!! ------

int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
// int dy[]={-1,0,1,-1,1,-1,0,1};int dx[]={-1,-1,-1,0,0,1,1,1};
string alph("abcdefghijklmnopqrstuvwxyz");
char s[200][200];
ll n, cnt, ans, a, b, c, d, tmp, m, h, w, x, y, sum, k, q;
ll yoko1[] = {6, 3, 1, 4};
ll tate1[] = {6, 2, 1, 5};
ll yoko2[] = {1, 3, 6, 4};
ll tate2[] = {5, 1, 2, 6};
ll fil[200][200];

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

  cin >> h >> w;
  rep(i, h) rep(j, w) cin >> s[i][j];
  rep(i, h) {
    rep(j, w) {
      if (i % 2 == 1) continue;
      if ((i / 2) % 2 == 0)
        fil[i][j] = yoko1[j % 4];
      else
        fil[i][j] = yoko2[j % 4];
    }
  }
  rep(i, h) {
    rep(j, w) {
      if (i % 2 == 0) continue;
      if (j % 2 == 1) continue;
      if ((i - 1) / 2 % 2 == 0)
        fil[i][j] = 2;
      else
        fil[i][j] = 5;
    }
  }

  ll min_cost[h + 10][w + 10];
  rep(i, h) rep(j, w) min_cost[i][j] = LINF;
  queue<P> qu;
  qu.push({0LL, 0LL});
  min_cost[0][0] = 0;

  while (!qu.empty()) {
    auto p = qu.front();
    qu.pop();
    rep(i, 4) {
      int nx = p.se + dx[i], ny = p.fi + dy[i];
      if (nx < 0 || ny < 0 || nx >= w || ny >= h) continue;
      if (s[ny][nx] == '#') continue;
      if (min_cost[p.fi][p.se] + 1 >= min_cost[ny][nx]) continue;
      if ((s[ny][nx] - '0') != fil[ny][nx]) continue;
      min_cost[ny][nx] = min_cost[p.fi][p.se] + 1;
      qu.push({ny, nx});
    }
  }
  ans = min_cost[h - 1][w - 1];
  if (ans == LINF)
    Cout("NO");
  else
    Cout("YES");
  return 0;
}

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcountll

#define INF 1e16
#define mod 1000000007

struct dice{ int a[6]; };
void rot(int &a,int &b,int &c,int &d){ int tmp=a; a=b; b=c; c=d; d=tmp; }
void roll_f(dice &d){ rot(d.a[0],d.a[3],d.a[2],d.a[1]); }
void roll_b(dice &d){ rot(d.a[0],d.a[1],d.a[2],d.a[3]); }
void roll_l(dice &d){ rot(d.a[3],d.a[5],d.a[1],d.a[4]); }
void roll_r(dice &d){ rot(d.a[3],d.a[4],d.a[1],d.a[5]); }

bool vis[101][101][7][7][7]; // 0,3,5

struct state{
	int i,j;
	dice d;
};

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

  int h,w;
  cin>>h>>w;

  vector<string> b(h);
  rep(i,h)cin>>b[i];

  dice ini;
  ini.a[0]=2; ini.a[1]=6; ini.a[2]=5; ini.a[3]=1; ini.a[4]=4; ini.a[5]=3;
  queue<state> que;
  que.push((state){0,0,ini});
  vis[0][0][ini.a[0]][ini.a[3]][ini.a[5]]=true;
  while(que.size()){
  	state s=que.front(); que.pop();
  	dice crt=s.d;
  	int i=s.i,j=s.j;
  	if(i+1<h&&b[i+1][j]!='#'){
  		roll_f(crt);
  		if(crt.a[1]==b[i+1][j]-'0'&&!vis[i+1][j][crt.a[0]][crt.a[3]][crt.a[5]]){
  			vis[i+1][j][crt.a[0]][crt.a[3]][crt.a[5]]=true;
  			que.push((state){i+1,j,crt});
  		}
  		roll_b(crt);
  	}
  	if(i-1>=0&&b[i-1][j]!='#'){
  		roll_b(crt);
  		if(crt.a[1]==b[i-1][j]-'0'&&!vis[i-1][j][crt.a[0]][crt.a[3]][crt.a[5]]){
  			vis[i-1][j][crt.a[0]][crt.a[3]][crt.a[5]]=true;
  			que.push((state){i-1,j,crt});
  		}
  		roll_f(crt);
  	}
  	if(j+1<w&&b[i][j+1]!='#'){
  		roll_r(crt);
  		if(crt.a[1]==b[i][j+1]-'0'&&!vis[i][j+1][crt.a[0]][crt.a[3]][crt.a[5]]){
  			vis[i][j+1][crt.a[0]][crt.a[3]][crt.a[5]]=true;
  			que.push((state){i,j+1,crt});
  		}
  		roll_l(crt);
  	}
  	if(j-1>=0&&b[i][j-1]!='#'){
  		roll_l(crt);
  		if(crt.a[1]==b[i][j-1]-'0'&&!vis[i][j-1][crt.a[0]][crt.a[3]][crt.a[5]]){
  			vis[i][j-1][crt.a[0]][crt.a[3]][crt.a[5]]=true;
  			que.push((state){i,j-1,crt});
  		}
  		roll_r(crt);
  	}
  }

  bool res=false;
  rep(x,7)rep(y,7)rep(z,7){
  	res|=vis[h-1][w-1][x][y][z];
  }
  if(res)cout<<"YES"<<endl;
  else cout<<"NO"<<endl;

  return 0;
}