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++: // #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;

// Dice library
//         ---------
//         | back  |
//         |  (5)  |
// --------+-------+-------+--------+
// | left  |  top  | right | bottom |
// |  (4)  |  (1)  |  (2)  |   (3)  |
// --------+-------+-------+---------
//         | front |
//         |  (0)  |
//         ---------

enum Direction {
    // 北、南、東、西方向 および 右、左回り に 90 度回転
    NORTH = 0, SOUTH = 1, EAST = 2, WEST = 3, ROT_R = 4, ROT_L = 5,
};

enum View {
    // どこから見た時の値か？
    FRONT = 0, TOP = 1, RIGHT = 2, BOTTOM = 3, LEFT = 4, BACK = 5,
};

struct Dice {
    vector<int> value;
    Dice(int front, int top, int right) {
        int sum = 7;
        assert(front + top   != sum);
        assert(top   + right != sum);
        assert(front + right != sum);
        value = {front, top, right, sum-top, sum-right, sum-front};
    }

    void rotate(int dir) {
        vector<int> fnc, swp(6);
        if(dir == NORTH) fnc = {1, 5, 2, 0, 4, 3};
        if(dir == SOUTH) fnc = {3, 0, 2, 5, 4, 1};
        if(dir == EAST ) fnc = {0, 2, 3, 4, 1, 5};
        if(dir == WEST ) fnc = {0, 4, 1, 2, 3, 5};
        if(dir == ROT_R) fnc = {4, 1, 0, 3, 5, 2};
        if(dir == ROT_L) fnc = {2, 1, 5, 3, 0, 4};

        for(int i=0; i<6; i++) swp[ fnc[i] ] = value[i];
        swap(value, swp);
    }

    int get_value(int view) {
        return value[view];
    }
};

int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, 1, -1};
const Direction dd[] = {NORTH, SOUTH, EAST, WEST};

bool visited[110][110][300];
vector< vector<int> > perm;
int nxt[300][4];

bool check(vector<int> P) {
    int front = P[0], top = P[1], right = P[2], sum = 7;
    bool ok = true;
    ok &= front + top   != sum;
    ok &= top   + right != sum;
    ok &= front + right != sum;
    return ok;
}

int main() {
    vector<int> P(6);
    for(int i=0; i<6; i++) P[i] = i+1;
    do {
        if(!check(P)) continue;
        perm.emplace_back(P);
    }while(next_permutation(P.begin(), P.end()));
    
    int ini = -1;
    {
        Dice dice(2, 1, 3);
        int idx = lower_bound(perm.begin(), perm.end(), dice.value) - perm.begin();
        ini = idx;
    }

    for(size_t i=0; i<perm.size(); i++) {
        int f = perm[i][0], t = perm[i][1], r = perm[i][2];
        Dice dice(f, t, r);
        for(int k=0; k<4; k++) {
            dice.rotate(dd[k]);
            int idx = lower_bound(perm.begin(), perm.end(), dice.value) - perm.begin();
            nxt[i][k] = idx;
            dice.rotate(dd[k^1]);
        }
    }
    
    int H, W; cin >> H >> W;
    vector<string> vs(H);
    for(int i=0; i<H; i++) cin >> vs[i];

    queue< tuple<int, int, int> > que;
    que.emplace(0, 0, ini);
    visited[0][0][ini] = true;
    while(que.size()) {
        int x, y, p; tie(x, y, p) = que.front(); que.pop();

        int f = perm[p][0], t = perm[p][1], r = perm[p][2];
        Dice dice(f, t, r);

        for(int k=0; k<4; k++) {
            int nx = x + dx[k], ny = y + dy[k];
            if(nx < 0 or nx >= H or ny < 0 or ny >= W) continue;

            /*
            fprintf(stderr, "x = %d, y = %d, p = %d\n", x, y, p);
            int iidx = lower_bound(perm.begin(), perm.end(), dice.value) - perm.begin();
            for(auto e : perm[iidx]) cerr << e << " ";
            cerr << endl;
            */

            dice.rotate(dd[k]);
            int np = nxt[p][k];
            
            /*


            fprintf(stderr, "nx = %d, ny = %d, np = %d\n", nx, ny, np);
            int idx = lower_bound(perm.begin(), perm.end(), dice.value) - perm.begin();
            for(auto e : perm[idx]) cerr << e << " ";
            cerr << endl;
            

            cerr << dice.get_value(BOTTOM) << " " << vs[nx][ny] << endl;
            */

            if(isdigit(vs[nx][ny]) and
               vs[nx][ny] - '0' == dice.get_value(BOTTOM) and
               !visited[nx][ny][np]) {
                // fprintf(stderr, "nx = %d, ny = %d\n", nx, ny);
                visited[nx][ny][np] = true;
                que.emplace(nx, ny, np);
            }
            dice.rotate(dd[k^1]);
        }
    }

    for(int i=0; i<300; i++) {
        if(visited[H-1][W-1][i]) {
            cout << "YES" << endl;
            return 0;
        }
    }
    
    cout << "NO" << endl;
    return 0;
}