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<vector>
#include<string>
#include<queue>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template<typename T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return 0;}
template<typename T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return 0;}

struct dice {
    vector<int> side, front;
    dice() {
        side = vector<int>({1, 3, 6, 4});  // top -> right -> bottom -> left
        front = vector<int>({1, 2, 6, 5});  // top -> front -> bottom -> back
    }
    void rotateFront() {
        int tail = front.back();
        rrep(i, 4, 1) {
            front[i] = front[i - 1];
        }
        front[0] = tail;
        side[0] = front[0];
        side[2] = front[2];
    }
    void rotateBack() {
        int head = front.front();
        rep(i, 0, 3) {
            front[i] = front[i + 1];
        }
        front[3] = head;
        side[0] = front[0];
        side[2] = front[2];
    }
    void rotateRight() {
        int tail = side.back();
        rrep(i, 4, 1) {
            side[i] = side[i - 1];
        }
        side[0] = tail;
        front[0] = side[0];
        front[2] = side[2];
    }
    void rotateLeft() {
        int head = side.front();
        rep(i, 0, 3) {
            side[i] = side[i + 1];
        }
        side[3] = head;
        front[0] = side[0];
        front[2] = side[2];
    }
    int getBottom() const {
        return side[2];
    }
};

using P = pair<dice, pair<int, int>>;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // dice D;
    // cout << "First bottom=" << D.getBottom() << endl;
    // int T;
    // cin >> T;
    // rep(_, 0, T) {
    //     cout << "ROTATE -> ";
    //     flush(cout);
    //     char dir;
    //     cin >> dir;
    //     if (dir == 'L') {
    //         D.rotateLeft();
    //     }
    //     else if (dir == 'R') {
    //         D.rotateRight();
    //     }
    //     else if (dir == 'F') {
    //         D.rotateFront();
    //     }
    //     else {
    //         D.rotateBack();
    //     }
    //     cout << "Bottom=" << D.getBottom() << endl;
    // }
    int H, W;
    cin >> H >> W;
    vector<string> grid(H);
    for (auto& g : grid) {
        cin >> g;
    }
    dice D;
    queue<P> que;
    vector<vector<bool>> visited(H, vector<bool>(W, false));
    que.push(P(D, make_pair(0, 0)));
    visited[0][0] = true;
    while (!que.empty()) {
        P now = que.front();
        que.pop();
        dice now_d = now.first;
        int now_h = now.second.first;
        int now_w = now.second.second;
        dice d1(now_d);
        d1.rotateFront();
        if (now_h + 1 < H && grid[now_h + 1][now_w] != '#' && d1.getBottom() == (int)(grid[now_h + 1][now_w] - '0') && !visited[now_h + 1][now_w]) {
            visited[now_h + 1][now_w] = true;
            que.push(P(d1, make_pair(now_h + 1, now_w)));
        }
        dice d2(now_d);
        d2.rotateRight();
        if (now_w + 1 < W && grid[now_h][now_w + 1] != '#' && d2.getBottom() == (int)(grid[now_h][now_w + 1] - '0') && !visited[now_h][now_w + 1]) {
            visited[now_h][now_w + 1] = true;
            que.push(P(d2, make_pair(now_h, now_w + 1)));
        }
        dice d3(now_d);
        d3.rotateBack();
        if (now_h - 1 >= 0 && grid[now_h - 1][now_w] != '#' && d3.getBottom() == (int)(grid[now_h - 1][now_w] - '0') && !visited[now_h - 1][now_w]) {
            visited[now_h - 1][now_w] = true;
            que.push(P(d3, make_pair(now_h - 1, now_w)));
        }
        dice d4(now_d);
        d4.rotateLeft();
        if (now_w - 1 >= 0 && grid[now_h][now_w - 1] != '#' && d4.getBottom() == (int)(grid[now_h][now_w - 1] - '0') && !visited[now_h][now_w - 1]) {
            visited[now_h][now_w - 1] = true;
            que.push(P(d4, make_pair(now_h, now_w - 1)));
        }
    }
    if (visited[H - 1][W - 1]) {
        cout << "YES" << endl;
    }
    else {
        cout << "NO" << endl;
    }
    return 0;
}