Problem:


会津山スキー場の新企画

 会津山スキー場の経営者である油木屋さんは、上級者向けとして障害物やジャンプ台を配置したコースを用意しました。コースにはいろいろな滑り方があり、シーズン中に全てのパターンの滑り方が出来た利用者には、プレゼントを贈ることになっています。

油木屋さんのために、コースの見取り図をもとに滑り方のパターン数を出力するプログラムを作ってあげましょう。

  コースは上図のような X &times; Y 個のマスからなるグリッドで表されます。左上を原点とし、x 座標は右に行くにつれて大きくなり、y 座標は下にいくにつれて大きくなるものとします。

 各滑り方のパターンは、最も高いところ(y = 1 、ただし障害物の無いところ) からスタートし、ゴール(y = Y) に向かって進んでいきます。グリッドのマス(x, y) にいる滑走者は、(x &minus; 1, y + 1)、(x, y + 1)、 (x + 1, y + 1) のいずれかに移動することができます。マスには、障害物やジャンプ台があり、障害物のあるマスには進入できず、ジャンプ台があるマスに進入すると(x, y + 2) へ移動します。ただし、いちばん高いマス (y = 1 のマス) にはジャンプ台は存在せず、ジャンプ台のあるマスに進入する際には x 座標が同じマスからしか進入できません。コースの上端(y = 1) からスタートし、コースからはずれることなく下端を超えれば(y ≥ Y) 1つの滑り方とみなし、滑り終わります。

  コースの情報を入力とし、滑り方の総数を出力するプログラムを作成してください。

Input

複数のデータセットの並びが入力として与えられます。入力の終わりはゼロふたつの行で示されます。各データセットは以下の形式で与えられます。

X Y
c11 c21 ... cX1
c12 c22 ... cX2
:
c1Y c2Y ... cXY	

1 行目にコースの大きさX, Y (1 ≤ X, Y ≤ 15) が与えられます。続く Y 行にコースの情報が与えられます。cij (0 , 1 , 2 のいずれか) はx = i, y = j のマスの情報を表す整数で、0 が移動可能なマス、1 が障害物があるマス、2 がジャンプ台があるマスを表します。

データセットの数は 50 を超えません。

Output

入力データセットごとに、コースの滑り方のパターン数を1行に出力します。

Sample Input

5 5
0 0 0 0 1
2 1 0 2 0
1 0 0 1 1
0 2 1 2 0
0 1 0 0 0
5 5
0 0 1 0 0
2 1 0 2 0
1 0 0 1 1
0 2 1 2 0
0 1 0 0 0
15 15
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0

Output for the Sample Input

8
6
52694573



Sample input:
5 5
0 0 0 0 1
2 1 0 2 0
1 0 0 1 1
0 2 1 2 0
0 1 0 0 0
5 5
0 0 1 0 0
2 1 0 2 0
1 0 0 1 1
0 2 1 2 0
0 1 0 0 0
15 15
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0


Sample output:
8
6
52694573

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0203

"""
import sys
from sys import stdin
from collections import deque, defaultdict
input = stdin.readline


def solve(field):
    BLANK, OBSTACLE, JUMP = 0, 1, 2
    ans = 0                     #  ??????????????°???????????°

    # dy = [1, 1, 1]
    dx = [0, -1, 1] # ?????????????????????????????????????§???????
    x_limit = len(field[0])
    y_limit = len(field)

    path = defaultdict(int)     #  ??????????????????????????°???????????°????????????'x???_y???'???????????????
    Q = deque()                 #  ?????§??????????????????????????¨??????????????\???

    # ???????????§??????????????§????????´????????¢???????????\??????????????????
    for x, m in enumerate(field[0]):
        if m == BLANK:              #  ?????????????????°?????´?????????????????????????????????
            k = '{}_{}'.format(x, 0)
            Q.append((x, 0))
            path[k] = 1

    while Q:
        cx, cy = Q.popleft()        #  ?????¨??°?????§?¨?
        k = '{}_{}'.format(cx, cy)
        num = path.pop(k)       #  ????????§?¨?????????????????????°

        if field[cy][cx] == OBSTACLE: #  ?????£????????§?????°??????????????????????????±????????????????????????????????§?????£?????????????????
            continue
        elif field[cy][cx] == JUMP: #  ?????£????????§?????°?????????????????£????????°
            if cy+2 > y_limit-1: #  ?????£???????????????????¶?????????´??????OK??¨?????????
                ans += num
            else:
                k = '{}_{}'.format(cx, cy+2)
                if not path[k]:
                    Q.append((cx, cy+2))
                path[k] += num
            continue
        elif cy == y_limit -1:
            ans += num
            continue

        for i in range(3):      #  ?????????3???len(dy)?????????
            nx = cx + dx[i]     #  ?????°????????§?¨?
            ny = cy + 1         #  ??????+1????????§???dy[i]??¨?????????????????????OK

            if 0<= nx < x_limit:
                if field[ny][nx] == JUMP and dx[i] == 0: #  ?????£????????°????????£??????????????\????????´???
                    if ny+2 > y_limit - 1:
                        ans += num
                    else:
                        k = '{}_{}'.format(nx, ny+2)
                        if not path[k]:
                            Q.append((nx, ny+2))
                        path[k] += num
                elif field[ny][nx] == BLANK:
                    if (ny >= y_limit - 1):
                        ans += num
                    else:
                        k = '{}_{}'.format(nx, ny)
                        if not path[k]:
                            Q.append((nx, ny))
                        path[k] += num
    return ans


def solve2(field):
    BLANK, OBSTACLE, JUMP = 0, 1, 2
    ans = 0                     #  ??????????????°???????????°

    dy = [1, 1, 1]  # ?????????????????????????????????????§???????
    dx = [0, -1, 1]
    x_limit = len(field[0])
    y_limit = len(field)

    path = defaultdict(int)     #  ??????????????????????????°???????????°????????????'x???_y???'???????????????
    Q = deque()
    for x, m in enumerate(field[0]):
        if m == BLANK:              #  ?????????????????°?????´?????????????????????????????????
            t = '{}_{}'.format(x, 0)
            Q.append((x, 0))
            path[t] = 1

    while Q:
        cx, cy = Q.popleft()        #  ?????¨??°?????§?¨?
        t = '{}_{}'.format(cx, cy)
        num = path.pop(t)

        if field[cy][cx] == OBSTACLE:
            continue
        elif field[cy][cx] == JUMP: #  ?????£????????§?????°?????????????????£????????°
            if cy+2 > y_limit-1:
                ans += num
            else:
                t = '{}_{}'.format(cx, cy+2)
                if not path[t]:
                    Q.append((cx, cy+2))
                path[t] += num
            continue
        elif cy == y_limit -1:
            ans += num
            continue

        for i in range(3):      #  ?????????3???len(dy)?????????
            nx = cx + dx[i]     #  ?????°????????§?¨?
            ny = cy + dy[i]

            if 0<= nx < x_limit:
                if field[ny][nx] == JUMP and dx[i] == 0: #  ?????£????????°????????£??????????????\????????´???
                    if ny+2 > y_limit - 1:
                        ans += num
                    else:
                        t = '{}_{}'.format(nx, ny+2)
                        if not path[t]:
                            Q.append((nx, ny+2))
                        path[t] += num
                elif field[ny][nx] == BLANK:
                    if (ny >= y_limit - 1):
                        ans += num
                    else:
                        t = '{}_{}'.format(nx, ny)
                        if not path[t]:
                            Q.append((nx, ny))
                        path[t] += num
    return ans


def solve3(field):
    BLANK, OBSTACLE, JUMP = 0, 1, 2
    X = len(field[0])
    Y = len(field)
    dp = [[0] * X for _ in range(Y)]

    # ??????????????°????????????
    for x in range(X):
        if field[0][x] == BLANK:
            dp[0][x] = 1

    for y in range(1, Y):
        for x in range(X):
            t = 0
            mark = field[y][x]
            if mark == BLANK:
                if field[y-1][x] != JUMP:
                    t += dp[y-1][x]
                if x > 0 and field[y-1][x-1] != JUMP:
                    t += dp[y-1][x-1]
                if x < X-1 and field[y-1][x+1] != JUMP:
                    t += dp[y-1][x+1]
                if y > 1 and field[y-2][x] == JUMP:
                    t += dp[y-2][x]
            elif mark == JUMP:
                t = dp[y-1][x]
                if y > 1 and field[y-2][x] == JUMP:
                    t += dp[y-2][x]
            dp[y][x] = t
    ans = sum(dp[-1])
    if Y > 1:
        for x in range(X):
            if field[-2][x] == JUMP:
                ans += dp[-2][x]
    return ans


def main(args):
    while True:
        X, Y = map(int, input().strip().split())
        if X == 0 and Y == 0:
            break
        field = []
        for _ in range(Y):
            temp = [int(x) for x in input().strip().split()]
            field.append(temp)
        result = solve3(field)
        print(result)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Wrong Answer

Solution code:
while True:
    X,Y=map(int,raw_input().split())
    if X==Y==0:break
    C=[[0]+map(int,raw_input().split())+[0] for i in range(Y)]
    C.append([0]*(X+2))
    dp=[[0]*(X+2) for i in range(Y+1)]
    for i in range(1,X+1):
        if C[0][i]!=1:
            dp[0][i]=1
    for i in range(Y-1):
        for j in range(1,X+1):
            if C[i][j]==0:
                if C[i+1][j]!=1:
                    dp[i+1][j]+=dp[i][j]
                if C[i+1][j+1]!=1 and C[i+1][j+1]!=2:
                    dp[i+1][j+1]+=dp[i][j]
                if C[i+1][j-1]!=1 and C[i+1][j-1]!=2:
                    dp[i+1][j-1]+=dp[i][j]
            if C[i][j]==2:
                dp[i+2][j]+=dp[i][j]
    print sum(dp[-1][1:-1])+sum(dp[-2][1:-1])

Submission status: Wrong Answer

Solution code:
while 1:
	X,Y = map(int,raw_input().split())
	if X == 0: break
	A = [[0]+map(int,raw_input().split())+[0] for i in range(Y)]
	S = [[0]*(X+2) for i in range(Y)]
	for x in range(1,X+1): S[0][x] = 1 if A[0][x] != 1 else 0
	for y in range(1,Y):
		for x in range(1,X+1):
			if A[y][x] == 0:
				for i in range(-1,2):
					S[y][x] += (S[y-1][x-i] if A[y-1][x-i] == 0 else 0)
			if A[y][x] == 2:
				S[y][x] += S[y-1][x-1]
				if y+2 < Y and A[y+2][x] != 1:
					S[y+2][x] = S[y][x]
	ans = 0
	for x in range(1,X+1):
		ans += S[Y-1][x] + (S[Y-2][x] if A[Y-2][x] == 2 else 0)
	print ans

Submission status: Accepted

Solution code:
while True:
    X, Y = map(int, raw_input().split())
    if (X, Y) == (0, 0):
        break
    grid = [map(int, raw_input().split()) for _ in xrange(Y)] + [[0] * X]
    D = (((-1, 1), (0, 1), (1, 1)), (), ((0, 2),))
    dp = [[1] * X] + [[0] * X for _ in xrange(Y+2)]
    for y in xrange(Y-1):
        for x in xrange(X):
            for dx, dy in D[grid[y][x]]:
                nx, ny = x + dx, y + dy
                if nx < 0 or X <= nx or y < 0 or Y+1 <= ny:
                    continue
                if grid[ny][nx] == 2 and nx != x:
                    continue
                dp[ny][nx] += dp[y][x]
    print sum(sum(dp[y][x] for x in xrange(X) if grid[y][x] != 1) for y in xrange(Y-1, Y+1))

Submission status: Accepted

Solution code:
def solve(c):
    w, h = len(c[0]), len(c)
    dp = [[0] * w for _ in range(h)]
    
    #?????????
    for x in range(w):
        if c[1][x] == 0:
            dp[1][x] = 1
    
    for y in range(2, h):
        for x in range(w):
            if c[y][x] == 1:
                continue
            if c[y - 2][x] == 2:
                dp[y][x] += dp[y - 2][x]
            if c[y - 1][x] == 0:
                dp[y][x] += dp[y - 1][x]
                
            if c[y][x] == 0:
                if c[y - 1][x - 1] == 0:
                    dp[y][x] += dp[y - 1][x - 1]
                if c[y - 1][x + 1] == 0:
                    dp[y][x] += dp[y - 1][x + 1]

    return sum(dp[-1]) + sum(dp[-2][x] for x in range(w) if c[-2][x] == 2)

import sys
f = sys.stdin

while True:
    w, h = map(int, f.readline().split())
    if w == 0:
        break
    course = [[1] * (w + 2)] + [[1] + list(map(int, f.readline().split())) + [1] for _ in range(h)]
    print(solve(course))

Submission status: Accepted

Solution code:
while(1):
    [X,Y]=[int(x) for x in raw_input().split()]
    if X==0:
        break
    else:
        F=[]
        for i in range(Y):
            F.append([0]+[int(x) for x in raw_input().split()]+[0])
        F.append([3 for x in range(X+2)])
        F.append([4 for x in range(X+2)])        
        S=[[0 for x in range(X+2)] for y in range(Y+2)] #F,S[y][x]
        #initilizing
        for i in range(1,X+1):
            S[0][i]=1-F[0][i]
        for j in range(Y+2):
            for i in range(1,X+1):
                if F[j][i]==2:
                    S[j][i]=S[j][i]+(1-F[j-1][i]/2)*S[j-1][i]
                    S[j+2][i]=S[j+2][i]+S[j][i]
                elif F[j][i]==1:
                    S[j][i]=0
                elif F[j][i]==3:
                    S[j][i]=S[j][i]+(1-F[j-1][i]/2)*S[j-1][i]
                elif F[j][i]==4:
                    S[j][i]=S[j][i]+S[j-1][i]                    
                else:
                    S[j][i]=S[j][i]+(1-F[j-1][i]/2)*S[j-1][i]+(1-F[j-1][i-1]/2)*S[j-1][i-1]+(1-F[j-1][i+1]/2)*S[j-1][i+1]
        print sum(S[Y+1])

Submission status: Accepted

Solution code:
while 1:
    X,Y = map(int,raw_input().split())
    if X == 0: break
    A = [[0]+map(int,raw_input().split())+[0] for i in range(Y)]
    S = [[0]*(X+2) for i in range(Y)]
    for x in range(1,X+1): S[0][x] = 1 if A[0][x] == 0 else 0
    for y in range(1,Y):
        for x in range(1,X+1):
            if A[y][x] == 0:
                for i in range(-1,2):
                    S[y][x] += S[y-1][x-i] if A[y-1][x-i] == 0 else 0
            if A[y][x] == 2:
                S[y][x] += S[y-1][x] if A[y-1][x] == 0 else 0
                if y+2 < Y and A[y+2][x] != 1:
                    S[y+2][x] = S[y][x]
    ans = 0
    for x in range(1,X+1):
        ans += S[Y-1][x] + (S[Y-2][x] if A[Y-2][x] == 2 else 0)
    print ans

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0203

"""
import sys
from sys import stdin
from collections import deque, defaultdict
input = stdin.readline


def solve(field):
    BLANK, OBSTACLE, JUMP = 0, 1, 2
    ans = 0                     #  ??????????????°???????????°

    dy = [1, 1, 1]  # ?????????????????????????????????????§???????
    dx = [0, -1, 1]
    x_limit = len(field[0])
    y_limit = len(field)

    path = defaultdict(int)     #  ??????????????????????????°???????????°????????????'x???_y???'???????????????
    Q = deque()
    for x, m in enumerate(field[0]):
        if m == BLANK:              #  ?????????????????°?????´?????????????????????????????????
            t = '{}_{}'.format(x, 0)
            Q.append((x, 0))
            path[t] = 1
    if y_limit < 2:
        return len(Q)


    while Q:
        cx, cy = Q.popleft()        #  ?????¨??°?????§?¨?
        t = '{}_{}'.format(cx, cy)
        num = path.pop(t)

        if field[cy][cx] == OBSTACLE:
            continue
        elif field[cy][cx] == JUMP: #  ?????£????????§?????°?????????????????£????????°
            if cy+2 > y_limit-1:
                ans += num
            else:
                t = '{}_{}'.format(cx, cy+2)
                if not path[t]:
                    Q.append((cx, cy+2))
                path[t] += num
            continue
        elif cy == y_limit -1:
            ans += num
            continue

        for i in range(len(dx)):
            nx = cx + dx[i]     #  ?????°????????§?¨?
            ny = cy + dy[i]

            if 0<= nx < x_limit:
                if (ny >= y_limit - 1) and field[ny][nx] == BLANK:
                    ans += num
                else:
                    if field[ny][nx] == JUMP and dx[i] == 0: #  ?????£????????°????????£??????????????\????????´???
                        if ny+2 > y_limit - 1:
                            ans += num
                        else:
                            t = '{}_{}'.format(nx, ny+2)
                            if not path[t]:
                                Q.append((nx, ny+2))
                            path[t] += num
                    elif field[ny][nx] == BLANK:
                        t = '{}_{}'.format(nx, ny)
                        if not path[t]:
                            Q.append((nx, ny))
                        path[t] += num
    return ans


def main(args):
    while True:
        X, Y = map(int, input().strip().split())
        if X == 0 and Y == 0:
            break
        field = []
        for _ in range(Y):
            temp = [int(x) for x in input().strip().split()]
            field.append(temp)
        result = solve(field)
        print(result)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0203

"""
import sys
from sys import stdin
from collections import deque, defaultdict
input = stdin.readline


def solve(field):
    BLANK, OBSTACLE, JUMP = 0, 1, 2
    ans = 0                     #  ??????????????°???????????°

    dy = [1, 1, 1]  # ?????????????????????????????????????§???????
    dx = [0, -1, 1]
    x_limit = len(field[0])
    y_limit = len(field)

    path = defaultdict(int)     #  ??????????????????????????°???????????°????????????'x???_y???'???????????????
    Q = deque()
    for x, m in enumerate(field[0]):
        if m == BLANK:              #  ?????????????????°?????´?????????????????????????????????
            t = '{}_{}'.format(x, 0)
            Q.append((x, 0))
            path[t] = 1

    while Q:
        cx, cy = Q.popleft()        #  ?????¨??°?????§?¨?
        t = '{}_{}'.format(cx, cy)
        num = path.pop(t)

        if field[cy][cx] == OBSTACLE:
            continue
        elif field[cy][cx] == JUMP: #  ?????£????????§?????°?????????????????£????????°
            if cy+2 > y_limit-1:
                ans += num
            else:
                t = '{}_{}'.format(cx, cy+2)
                if not path[t]:
                    Q.append((cx, cy+2))
                path[t] += num
            continue
        elif cy == y_limit -1:
            ans += num
            continue

        for i in range(len(dx)):
            nx = cx + dx[i]     #  ?????°????????§?¨?
            ny = cy + dy[i]

            if 0<= nx < x_limit:
                if (ny >= y_limit - 1) and field[ny][nx] == BLANK:
                    ans += num
                else:
                    if field[ny][nx] == JUMP and dx[i] == 0: #  ?????£????????°????????£??????????????\????????´???
                        if ny+2 > y_limit - 1:
                            ans += num
                        else:
                            t = '{}_{}'.format(nx, ny+2)
                            if not path[t]:
                                Q.append((nx, ny+2))
                            path[t] += num
                    elif field[ny][nx] == BLANK:
                        t = '{}_{}'.format(nx, ny)
                        if not path[t]:
                            Q.append((nx, ny))
                        path[t] += num
    return ans


def main(args):
    while True:
        X, Y = map(int, input().strip().split())
        if X == 0 and Y == 0:
            break
        field = []
        for _ in range(Y):
            temp = [int(x) for x in input().strip().split()]
            field.append(temp)
        result = solve(field)
        print(result)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0203

"""
import sys
from sys import stdin
from collections import deque, defaultdict
input = stdin.readline


def solve(field):
    BLANK, OBSTACLE, JUMP = 0, 1, 2
    ans = 0                     #  ??????????????°???????????°

    dy = [1, 1, 1]  # ?????????????????????????????????????§???????
    dx = [0, -1, 1]
    x_limit = len(field[0])
    y_limit = len(field)

    path = defaultdict(int)     #  ??????????????????????????°???????????°????????????'x???_y???'???????????????
    Q = deque()
    for x, m in enumerate(field[0]):
        if m == BLANK:              #  ?????????????????°?????´?????????????????????????????????
            t = '{}_{}'.format(x, 0)
            Q.append((x, 0))
            path[t] = 1

    while Q:
        cx, cy = Q.popleft()        #  ?????¨??°?????§?¨?
        t = '{}_{}'.format(cx, cy)
        num = path.pop(t)

        if field[cy][cx] == OBSTACLE:
            continue
        elif field[cy][cx] == JUMP: #  ?????£????????§?????°?????????????????£????????°
            if cy+2 > y_limit-1:
                ans += num
            else:
                t = '{}_{}'.format(cx, cy+2)
                if not path[t]:
                    Q.append((cx, cy+2))
                path[t] += num
            continue
        elif cy == y_limit -1:
            ans += num
            continue

        for i in range(3):      #  ?????????3???len(dy)?????????
            nx = cx + dx[i]     #  ?????°????????§?¨?
            ny = cy + dy[i]

            if 0<= nx < x_limit:
                if field[ny][nx] == JUMP and dx[i] == 0: #  ?????£????????°????????£??????????????\????????´???
                    if ny+2 > y_limit - 1:
                        ans += num
                    else:
                        t = '{}_{}'.format(nx, ny+2)
                        if not path[t]:
                            Q.append((nx, ny+2))
                        path[t] += num
                elif field[ny][nx] == BLANK:
                    if (ny >= y_limit - 1):
                        ans += num
                    else:
                        t = '{}_{}'.format(nx, ny)
                        if not path[t]:
                            Q.append((nx, ny))
                        path[t] += num
    return ans


def main(args):
    while True:
        X, Y = map(int, input().strip().split())
        if X == 0 and Y == 0:
            break
        field = []
        for _ in range(Y):
            temp = [int(x) for x in input().strip().split()]
            field.append(temp)
        result = solve(field)
        print(result)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0203

"""
import sys
from sys import stdin
from collections import deque, defaultdict
input = stdin.readline


def solve(field):
    BLANK, OBSTACLE, JUMP = 0, 1, 2
    ans = 0                     #  ??????????????°???????????°

    # dy = [1, 1, 1]
    dx = [0, -1, 1] # ?????????????????????????????????????§???????
    x_limit = len(field[0])
    y_limit = len(field)

    path = defaultdict(int)     #  ??????????????????????????°???????????°????????????'x???_y???'???????????????
    Q = deque()                 #  ?????§??????????????????????????¨??????????????\???
    
    # ???????????§??????????????§????????´????????¢???????????\??????????????????
    for x, m in enumerate(field[0]):
        if m == BLANK:              #  ?????????????????°?????´?????????????????????????????????
            k = '{}_{}'.format(x, 0)
            Q.append((x, 0))
            path[k] = 1

    while Q:
        cx, cy = Q.popleft()        #  ?????¨??°?????§?¨?
        k = '{}_{}'.format(cx, cy)
        num = path.pop(k)       #  ????????§?¨?????????????????????°

        if field[cy][cx] == OBSTACLE: #  ?????£????????§?????°??????????????????????????±????????????????????????????????§?????£?????????????????
            continue
        elif field[cy][cx] == JUMP: #  ?????£????????§?????°?????????????????£????????°
            if cy+2 > y_limit-1: #  ?????£???????????????????¶?????????´??????OK??¨?????????
                ans += num
            else:
                k = '{}_{}'.format(cx, cy+2)
                if not path[k]:
                    Q.append((cx, cy+2))
                path[k] += num
            continue
        elif cy == y_limit -1:
            ans += num
            continue

        for i in range(3):      #  ?????????3???len(dy)?????????
            nx = cx + dx[i]     #  ?????°????????§?¨?
            ny = cy + 1         #  ??????+1????????§???dy[i]??¨?????????????????????OK

            if 0<= nx < x_limit:
                if field[ny][nx] == JUMP and dx[i] == 0: #  ?????£????????°????????£??????????????\????????´???
                    if ny+2 > y_limit - 1:
                        ans += num
                    else:
                        k = '{}_{}'.format(nx, ny+2)
                        if not path[k]:
                            Q.append((nx, ny+2))
                        path[k] += num
                elif field[ny][nx] == BLANK:
                    if (ny >= y_limit - 1):
                        ans += num
                    else:
                        k = '{}_{}'.format(nx, ny)
                        if not path[k]:
                            Q.append((nx, ny))
                        path[k] += num
    return ans


def main(args):
    while True:
        X, Y = map(int, input().strip().split())
        if X == 0 and Y == 0:
            break
        field = []
        for _ in range(Y):
            temp = [int(x) for x in input().strip().split()]
            field.append(temp)
        result = solve(field)
        print(result)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0203

"""
import sys
from sys import stdin
from collections import deque, defaultdict
input = stdin.readline


def solve(field):
    BLANK, OBSTACLE, JUMP = 0, 1, 2
    ans = 0                     #  ??????????????°???????????°

    # dy = [1, 1, 1]
    dx = [0, -1, 1] # ?????????????????????????????????????§???????
    x_limit = len(field[0])
    y_limit = len(field)

    path = defaultdict(int)     #  ??????????????????????????°???????????°????????????'x???_y???'???????????????
    Q = deque()                 #  ?????§??????????????????????????¨??????????????\???

    # ???????????§??????????????§????????´????????¢???????????\??????????????????
    for x, m in enumerate(field[0]):
        if m == BLANK:              #  ?????????????????°?????´?????????????????????????????????
            k = '{}_{}'.format(x, 0)
            Q.append((x, 0))
            path[k] = 1

    while Q:
        cx, cy = Q.popleft()        #  ?????¨??°?????§?¨?
        k = '{}_{}'.format(cx, cy)
        num = path.pop(k)       #  ????????§?¨?????????????????????°

        if field[cy][cx] == OBSTACLE: #  ?????£????????§?????°??????????????????????????±????????????????????????????????§?????£?????????????????
            continue
        elif field[cy][cx] == JUMP: #  ?????£????????§?????°?????????????????£????????°
            if cy+2 > y_limit-1: #  ?????£???????????????????¶?????????´??????OK??¨?????????
                ans += num
            else:
                k = '{}_{}'.format(cx, cy+2)
                if not path[k]:
                    Q.append((cx, cy+2))
                path[k] += num
            continue
        elif cy == y_limit -1:
            ans += num
            continue

        for i in range(3):      #  ?????????3???len(dy)?????????
            nx = cx + dx[i]     #  ?????°????????§?¨?
            ny = cy + 1         #  ??????+1????????§???dy[i]??¨?????????????????????OK

            if 0<= nx < x_limit:
                if field[ny][nx] == JUMP and dx[i] == 0: #  ?????£????????°????????£??????????????\????????´???
                    if ny+2 > y_limit - 1:
                        ans += num
                    else:
                        k = '{}_{}'.format(nx, ny+2)
                        if not path[k]:
                            Q.append((nx, ny+2))
                        path[k] += num
                elif field[ny][nx] == BLANK:
                    if (ny >= y_limit - 1):
                        ans += num
                    else:
                        k = '{}_{}'.format(nx, ny)
                        if not path[k]:
                            Q.append((nx, ny))
                        path[k] += num
    return ans


def solve2(field):
    BLANK, OBSTACLE, JUMP = 0, 1, 2
    ans = 0                     #  ??????????????°???????????°

    dy = [1, 1, 1]  # ?????????????????????????????????????§???????
    dx = [0, -1, 1]
    x_limit = len(field[0])
    y_limit = len(field)

    path = defaultdict(int)     #  ??????????????????????????°???????????°????????????'x???_y???'???????????????
    Q = deque()
    for x, m in enumerate(field[0]):
        if m == BLANK:              #  ?????????????????°?????´?????????????????????????????????
            t = '{}_{}'.format(x, 0)
            Q.append((x, 0))
            path[t] = 1

    while Q:
        cx, cy = Q.popleft()        #  ?????¨??°?????§?¨?
        t = '{}_{}'.format(cx, cy)
        num = path.pop(t)

        if field[cy][cx] == OBSTACLE:
            continue
        elif field[cy][cx] == JUMP: #  ?????£????????§?????°?????????????????£????????°
            if cy+2 > y_limit-1:
                ans += num
            else:
                t = '{}_{}'.format(cx, cy+2)
                if not path[t]:
                    Q.append((cx, cy+2))
                path[t] += num
            continue
        elif cy == y_limit -1:
            ans += num
            continue

        for i in range(3):      #  ?????????3???len(dy)?????????
            nx = cx + dx[i]     #  ?????°????????§?¨?
            ny = cy + dy[i]

            if 0<= nx < x_limit:
                if field[ny][nx] == JUMP and dx[i] == 0: #  ?????£????????°????????£??????????????\????????´???
                    if ny+2 > y_limit - 1:
                        ans += num
                    else:
                        t = '{}_{}'.format(nx, ny+2)
                        if not path[t]:
                            Q.append((nx, ny+2))
                        path[t] += num
                elif field[ny][nx] == BLANK:
                    if (ny >= y_limit - 1):
                        ans += num
                    else:
                        t = '{}_{}'.format(nx, ny)
                        if not path[t]:
                            Q.append((nx, ny))
                        path[t] += num
    return ans


def main(args):
    while True:
        X, Y = map(int, input().strip().split())
        if X == 0 and Y == 0:
            break
        field = []
        for _ in range(Y):
            temp = [int(x) for x in input().strip().split()]
            field.append(temp)
        result = solve2(field)
        print(result)


if __name__ == '__main__':
    main(sys.argv[1:])