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")
	}
}

Python: from heapq import *
from collections import deque
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

class Dice:
    def __init__(self,top,bot,lef,rig,fro,bac):
        self.top = top
        self.bot = bot
        self.lef = lef
        self.rig = rig
        self.fro = fro
        self.bac = bac

    def up(self):
        top, bac, bot, fro = self.fro, self.top, self.bac, self.bot
        return Dice(top,bot,self.lef,self.rig,fro,bac)

    def down(self):
        top, bac, bot, fro = self.bac, self.bot, self.fro, self.top
        return Dice(top,bot,self.lef,self.rig,fro,bac)

    def right(self):
        top, rig, bot, lef = self.lef, self.top, self.rig, self.bot
        return Dice(top,bot,lef,rig,self.fro,self.bac)

    def left(self):
        top, rig, bot, lef = self.rig, self.bot, self.lef, self.top
        return Dice(top,bot,lef,rig,self.fro,self.bac)

    def state(self):
        return (self.top, self.rig, self.bot, self.lef, self.fro, self.bac)

def main():
    h, w = MI()
    ss = [[0 if c == "#" else int(c) for c in SI()] for _ in range(h)]
    stack=[(0,0,Dice(1,6,4,3,2,5))]
    fin=[[set() for _ in range(w)] for _ in range(h)]
    fin[0][0].add(Dice(1,6,4,3,2,5).state())
    while stack:
        i,j,d=stack.pop()
        if i==h-1 and j==w-1:
            print("YES")
            exit()
        ni,nj=i-1,j
        if i-1>=0:
            nd=d.up()
            if nd.bot==ss[ni][nj] and nd.state() not in fin[ni][nj]:
                fin[ni][nj].add(nd.state())
                stack.append((i-1,j,nd))
        ni,nj=i+1,j
        if i+1<h:
            nd=d.down()
            if nd.bot==ss[ni][nj] and nd.state() not in fin[ni][nj]:
                fin[ni][nj].add(nd.state())
                stack.append((i+1,j,nd))
        ni,nj=i,j-1
        if j-1>=0:
            nd=d.left()
            if nd.bot==ss[ni][nj] and nd.state() not in fin[ni][nj]:
                fin[ni][nj].add(nd.state())
                stack.append((i,j-1,nd))
        ni,nj=i,j+1
        if j+1<w:
            nd=d.right()
            if nd.bot==ss[ni][nj] and nd.state() not in fin[ni][nj]:
                fin[ni][nj].add(nd.state())
                stack.append((i,j+1,nd))
    print("NO")

main()

Python: import sys
sys.setrecursionlimit(10**7)

dice = [[6,3,1,4],
        [2,0,2,0],
        [1,3,6,4],
        [5,0,5,0]]
H,W = map(int,input().split())
s = list(input() for _ in range(H))
board = [[0]*W for _ in range(H)]

def dfs(x,y):
    board[x][y] = 1
    for dx,dy in [[0,-1],[0,1],[1,0],[-1,0]]:
        if not(0 <= dx+x < H and 0 <= dy+y < W):
            continue
        if(board[x+dx][y+dy] == 1):
            continue

        if s[x+dx][y+dy] == str(dice[(x+dx)%4][(y+dy)%4]):
            dfs(dx+x,dy+y)

dfs(0,0)
print(["NO","YES"][board[-1][-1]])

Python: import sys
sys.setrecursionlimit(pow(10, 8))

S = [6, 3, 1, 4, 2, 5]
H, W = map(int, input().split())
X = []
for i in range(H):
    X.append(input().strip())

def migi(S):
    return [S[1], S[2], S[3], S[0], S[4], S[5]]
def hidari(S):
    return [S[3], S[0], S[1], S[2], S[4], S[5]]
def sita(S):
    return [S[4], S[1], S[5], S[3], S[2], S[0]]
def ue(S):
    return [S[5], S[1], S[4], S[3], S[0], S[2]]

vs = set()

def dfs(x, y, s):
    if (x, y) == (H-1, W-1):
        return True
    vs.add((x, y))
    f = False
    if x+1 < H and (x+1, y) not in vs:
        ns = sita(s)
        if str(ns[0]) == X[x+1][y]:
            f = f|dfs(x+1, y, ns)
    if y+1 < W and (x, y+1) not in vs:
        ns = migi(s)
        if str(ns[0]) == X[x][y+1]:
            f = f|dfs(x, y+1, ns)
    if x-1 >= 0 and (x-1, y) not in vs:
        ns = ue(s)
        if str(ns[0]) == X[x-1][y]:
            f = f|dfs(x-1, y, ns)
    if y-1 >= 0 and (x, y-1) not in vs:
        ns = hidari(s)
        if str(ns[0]) == X[x][y-1]:
            f = f|dfs(x, y-1, ns)
    return f
if dfs(0, 0, S):
    print("YES")
else:
    print("NO")

Python: #!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

d = [(1,0),(-1,0),(0,1),(0,-1)]
def solve():
    def rotate(dice,d):
        d1,d2,d3,d4,d5,d6 = dice
        if d == (0,1):
            return (d4,d2,d1,d6,d5,d3)
        elif d == (0,-1):
            return (d3,d2,d6,d1,d5,d4)
        elif d == (1,0):
            return (d5,d1,d3,d4,d6,d2)
        else:
            return (d2,d6,d3,d4,d1,d5)
    h,w = LI()
    s = [input() for i in range(h)]
    bfs = [[0 for i in range(w)] for j in range(h)]
    bfs[0][0] = 1
    q = deque([(0,0,(1,2,3,4,5,6))])
    while q:
        y,x,dice = q.popleft()
        if y == h-1 and x == w-1:
            print("YES")
            return
        for dy,dx in d:
            ny,nx = y+dy,x+dx
            if 0 <= ny < h and 0 <= nx < w:
                nd = rotate(dice,(dy,dx))
                if not bfs[ny][nx] and str(nd[-1]) == s[ny][nx]:
                    bfs[ny][nx] = 1
                    q.append((ny,nx,nd))
    print("NO")
    return

#Solve
if __name__ == "__main__":
    solve()