open Scanf

let space_pattern = Str.regexp " "

let split_columns line = Str.split space_pattern line

type square = Pass | NonPass | Jump | Terminal

let previous_positions grid (x, y) =
  let rec filter_positions acc = function
    | [] -> acc
    | ((x, y) as p, s) :: ps when grid.(y).(x) = s ->
       filter_positions (p :: acc) ps
    | _ :: ps -> filter_positions acc ps in
  match y, grid.(y).(x) with
  | 0, _ -> []
  | _, NonPass -> []
  | 1, Jump -> filter_positions [] [ ((x, y - 1), Pass) ]
  | _, Jump | _, Terminal ->
     filter_positions [] [ ((x, y - 1), Pass); ((x, y - 2), Jump) ]
  | 1, Pass ->
     filter_positions [] [ ((x - 1, y - 1), Pass);
                           ((x    , y - 1), Pass);
                           ((x + 1, y - 1), Pass) ]
  | _, Pass ->
     filter_positions [] [ ((x - 1, y - 1), Pass);
                           ((x    , y - 1), Pass);
                           ((x + 1, y - 1), Pass);
                           ((x    , y - 2), Jump) ]

let square_of_int = function
  | 0 -> Pass
  | 1 -> NonPass
  | 2 -> Jump
  | _ -> assert false

let rec solve () =
  sscanf
    (read_line ()) "%d %d"
    (fun x y ->
     if x = 0 && y = 0 then ()
     else
       let grid =
         Array.init
           (y + 1)
           (fun j ->
            if j = y then Array.make (x + 2) Terminal
            else
              read_line ()
              |> split_columns
              |> List.map (fun column -> square_of_int (int_of_string column))
              |> (fun columns -> [ NonPass ] @ columns @ [ NonPass ])
              |> Array.of_list) in
       let num_routes = Array.make_matrix (y + 1) (x + 2) 0 in
       for i = 1 to x do
         num_routes.(0).(i) <- if grid.(0).(i) = Pass then 1 else 0
       done;
       for j = 1 to y do
         for i = 1 to x do
           num_routes.(j).(i) <-
             previous_positions grid (i, j)
             |> List.map (fun (x, y) -> num_routes.(y).(x))
             |> List.fold_left ( + ) 0
         done
       done;
       print_int (Array.fold_left ( + ) 0 num_routes.(y));
       print_newline ();
       solve ())

let () = solve ()