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>
#include <iomanip>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <numeric>
#include <random>
#include <algorithm>
#include <functional>
#include <cassert>

using namespace std;
typedef long long ll;

template<class T> inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template<class T> inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

pair<int,int> operator+(const pair<int,int> &lhs, const pair<int,int> &rhs){
  pair<int,int> p = { lhs.first + rhs.first, lhs.second + rhs.second };
  return p;
}

bool operator==(const pair<int,int> &lhs, const pair<int,int> &rhs){
  return (lhs.first == rhs.first) && (lhs.second == rhs.second);
}

const int INF = (1 << 30) - 1;
const ll INFLL= (1LL << 61) - 1;
const int MOD = 1000000007;
#define ALL(a) (a).begin(),(a).end()
#define rALL(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

int main(){
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int h,w;
  cin>>h>>w;
  vector<vector<int>> field(h,vector<int>(w));
  REP(i,h){
    string str;
    cin>>str;
    REP(j,w){
      int num=str[j]-'0';
      if(i%2==0 && j%2==0){
        if((i/2+j/2)%2==0){
          if(num==6)field[i][j]=1;
        }else{
          if(num==1)field[i][j]=1;
        }
      }
      if(i%2!=0 && j%2==0){
        if(i%4==1){
          if(num==2)field[i][j]=1;
        }
        if(i%4==3){
          if(num==5)field[i][j]=1;
        }
      }
      if(i%2==0 && j%2!=0){
        if(j%4==1){
          if(num==3)field[i][j]=1;
        }
        if(j%4==3){
          if(num==4)field[i][j]=1;
        }
      }
    }
  }

  queue<pair<int,int> > q;

  q.push(make_pair(0,0));

  const pair<int,int> moves[] = {
    { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 },
  };

  vector<vector<int>> field2(h,vector<int>(w, -1));
  field2[0][0]=0;

  while( !q.empty() ) {
    pair<int,int> cur = q.front(); q.pop();
    for(auto &move : moves){
      pair<int,int> next=cur+move;
      if(next.first<0||next.first>=h||next.second<0||next.second>=w)continue;
      if(field2[next.first][next.second]==-1 && field[next.first][next.second]==1){
        field2[next.first][next.second]=field2[cur.first][cur.second]+1;
        q.push(next);
      }
    }
  }
  if(field2[h-1][w-1]==-1){
    cout<<"NO"<<endl;
  }else{
    cout<<"YES"<<endl;
  }
}

C++: #include <iostream>
#include <utility>
#include <string>
#include <algorithm>
#include <map>
#include <vector>
#include <climits>
#include <numeric>
#include<stdio.h>
#include <queue>
#include <iomanip>
#include <float.h>
#include <set>
#include<functional>
#include <stack>
#include <time.h>
#include <climits>
#include <bitset>
#include <fstream>
using namespace std;
long long modinv(long long a, long long m) {
	long long b = m, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= m;
	if (u < 0) u += m;
	return u;
}
typedef pair<int, int> p;
vector< int64_t > divisor(int64_t n) {
	vector< int64_t > ret;
	for (int64_t i = 1; i * i <= n; i++) {
		if (n % i == 0) {
			ret.push_back(i);
			if (i * i != n) ret.push_back(n / i);
		}
	}
	sort(begin(ret), end(ret));
	return (ret);
}
long long mod = 1e9 + 7;
p mov[4] = { p(0,1),p(1,0),p(-1,0),p(0,-1) };
int main(int argc, char* argv[]) {
	int h, w;
	cin >> h >> w;
	int grid[103][103];
	int visit[103][103];
	for (int i = 0; i < 103; i++) {
		for (int j = 0; j < 103; j++) {
			visit[i][j] = 0;
		}
	}
	for (int i = 1; i <= h; i++) {
		string str;
		cin >> str;
		for (int j = 1; j <= w; j++) {
			if (i % 2 == 0 && j % 2 == 0) {
				grid[i][j] = 0;
				continue;
			}
			int num;
			int s = i / 2;
			int t = j / 2;
			if (i % 2 == 1 && j % 2 == 1) {
				if ((s + t) % 2 == 0)num = 6;
				else num = 1;
			}
			if (i % 2 == 1 && j % 2 == 0) {
				if (t % 2 == 1)num = 3;
				else num = 4;
			}
			if (i % 2 == 0 && j % 2 == 1) {
				if (s % 2 == 1)num = 2;
				else num = 5;
			}
			if (str[j-1] - '0' == num)grid[i][j] = 1;
			else grid[i][j] = 0;
		}
	}
	queue<p>que;
	que.push(p(1, 1));
	visit[1][1] = 1;
	while (!que.empty()) {
		p now = que.front();
		que.pop();
		for (int i = 0; i < 4; i++) {
			p next = p(now.first + mov[i].first, now.second + mov[i].second);
			if (next.first <= h &&next.first>=1&& next.second <= w &&next.second>=1&& visit[next.first][next.second] == 0 && grid[next.first][next.second] == 1) {
				que.push(next);
				visit[next.first][next.second] = 1;
			}
		}
	}
	if (visit[h][w] == 1) {
		cout << "YES\n";
	}
	else {
		cout << "NO\n";
	}
}