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: import sys
readline = sys.stdin.readline

H, W = map(int, readline().split())

Gr = [list(map(lambda x: int(x) if x != '#' else 7, readline().strip())) for _ in range(H)]

Ri = []
for h in range(H):
    if h % 4 == 0:
        Ri.append([6, 3, 1, 4]*W)
    elif h % 4 == 1:
        Ri.append([2, 8, 2, 8]*W)
    elif h % 4 == 2:
        Ri.append([1, 3, 6, 4]*W)
    else:
        Ri.append([5, 8, 5, 8]*W)

for h in range(H):
    for w in range(W):
        if Gr[h][w] != Ri[h][w]:
            Gr[h][w] = -1

stack = [(0, 0)]
used = set(stack)
dim = [(0, -1), (0, 1), (1, 0), (-1, 0)]
while stack:
    nx, ny = stack.pop()
    for dx, dy in dim:
        fx, fy = nx+dx, ny+dy
        if not 0 <= fx < W or not 0 <= fy < H:
            continue
        if Gr[fy][fx] == -1:
            continue
        if (fx, fy) in used:
            continue
        used.add((fx, fy))
        stack.append((fx, fy))
print('YES' if (W-1, H-1) in used else 'NO')

Python: H,W = map(int,input().split())
S = [input() for i in range(H)]

def tor(x,y):
    to = {'6':'3','3':'1','1':'4','4':'6'} if y%4==0 else {'6':'4','4':'1','1':'3','3':'6'}
    return to[S[y][x]]
def tol(x,y):
    to = {'6':'3','3':'1','1':'4','4':'6'} if y%4==2 else {'6':'4','4':'1','1':'3','3':'6'}
    return to[S[y][x]]
def tod(x,y):
    to = {'6':'2','2':'1','1':'5','5':'6'} if x%4==0 else {'6':'5','5':'1','1':'2','2':'6'}
    return to[S[y][x]]
def tou(x,y):
    to = {'6':'2','2':'1','1':'5','5':'6'} if x%4==2 else {'6':'5','5':'1','1':'2','2':'6'}
    return to[S[y][x]]
dxyto = [(0,1,tod),(1,0,tor),(0,-1,tou),(-1,0,tol)]
stack = [(0,0)]
visited = [[0]*W for i in range(H)]
while stack:
    x,y = stack.pop()
    visited[y][x] = 1
    if (x,y) == (W-1,H-1):
        print('YES')
        exit()
    c = S[y][x]
    for dx,dy,to in dxyto:
        nx,ny = x+dx,y+dy
        if not 0 <= nx < W: continue
        if not 0 <= ny < H: continue
        if visited[ny][nx]: continue
        if S[ny][nx] == '#': continue
        if to(x,y) != S[ny][nx]: continue
        stack.append((nx,ny))
print('NO')

Python: def main():
    H, W = map( int, input().split())
    S = [ list( input()) for _ in range(H)]
    T = [[6,3,1,4], [2,0,2,0],[1,3,6,4],[5,0,5,0]]
    C = [[0]*W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            if S[i][j] == '#':
                continue
            if T[i%4][j%4] == int(S[i][j]):
                C[i][j] = 1
    d = [(0,0)]
    P = [[False]*W for _ in range(H)]
    P[0][0] = True
    while d:
        x, y = d.pop()
        if x > 0:
            if C[x-1][y] == 1 and not P[x-1][y]:
                d.append((x-1,y))
                P[x-1][y] = True
        if x < H-1:
            if C[x+1][y] == 1 and not P[x+1][y]:
                d.append((x+1,y))
                P[x+1][y] = True
        if y > 0:
            if C[x][y-1] == 1 and not P[x][y-1]:
                d.append((x,y-1))
                P[x][y-1] = True
        if y < W-1:
            if C[x][y+1] == 1 and not P[x][y+1]:
                d.append((x,y+1))
                P[x][y+1] = True
    if P[H-1][W-1]:
        print("YES")
    else:
        print("NO")

if __name__ == '__main__':
    main()