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 GCC optimize("Ofast")
#include <cstdio>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <bitset>
#include <cmath>
#include <limits>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <iomanip>
#include <functional>
#include <complex>

using namespace std;


template<class T,class U>inline constexpr bool chmin(T&a,const U b){if(a<=b)return false;a=b;return true;}
template<class T,class U>inline constexpr bool chmax(T&a,const U b){if(a>=b)return false;a=b;return true;}
#define bit(n,k) ( (n>>k)&1 )

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


signed main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr); 
    cout<<fixed<<setprecision(20);

    Dice d;
    d.u=1;
    d.f=2;
    d.d=6;
    d.l=3;
    d.r=4;
    d.b=5;
    int H,W;
    cin>>H>>W;
    char S[110][110];
    for(int i=1;i<=H;i++){
        for(int j=1;j<=W;j++){
            cin>>S[i][j];
        }
    }
    using T=tuple<int,int,int,int,int,int,int,int>;
    queue<T> que;
    set<T> se;

    se.insert(T(1,1,d.u,d.d,d.r,d.l,d.f,d.b));
    que.emplace(1,1,d.u,d.d,d.r,d.l,d.f,d.b);
    bool flag=false;
    while(!que.empty()){
        int x,y;
        Dice d;
        tie(x,y,d.u,d.d,d.r,d.l,d.f,d.b)=que.front(); que.pop();
        if(x==H && y==W) flag=true;
        Dice w=d; w.RollW();
        Dice e=d; e.RollE();
        Dice n=d; n.RollN();
        Dice s=d; s.RollS();
        if(y<=W-1 && e.d==S[x][y+1]-'0'){
            d=e;
            if(se.find(T(x,y+1,d.u,d.d,d.r,d.l,d.f,d.b))==se.end()){
                se.insert(T(x,y+1,d.u,d.d,d.r,d.l,d.f,d.b));
                que.emplace(x,y+1,d.u,d.d,d.r,d.l,d.f,d.b);
            }
        }
        if(y>=2 && w.d==S[x][y-1]-'0'){
            d=w;
            if(se.find(T(x,y-1,d.u,d.d,d.r,d.l,d.f,d.b))==se.end()){
                se.insert(T(x,y-1,d.u,d.d,d.r,d.l,d.f,d.b));
                que.emplace(x,y-1,d.u,d.d,d.r,d.l,d.f,d.b);
            }
        }
        if(x<=H-1 && n.d==S[x+1][y]-'0'){
            d=n;
            if(se.find(T(x+1,y,d.u,d.d,d.r,d.l,d.f,d.b))==se.end()){
                se.insert(T(x+1,y,d.u,d.d,d.r,d.l,d.f,d.b));
                que.emplace(x+1,y,d.u,d.d,d.r,d.l,d.f,d.b);
            }
        }
        if(x>=2 && s.d==S[x-1][y]-'0'){
            d=s;
            if(se.find(T(x-1,y,d.u,d.d,d.r,d.l,d.f,d.b))==se.end()){
                se.insert(T(x-1,y,d.u,d.d,d.r,d.l,d.f,d.b));
                que.emplace(x-1,y,d.u,d.d,d.r,d.l,d.f,d.b);
            }
        }

    }
    if(flag) cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
}