#include <bits/stdc++.h>

typedef struct {
    int y;
    int x;
    int x_temp;
    int end;
    int ans;
} Ski_data; 

int ski_resort[16][17];

Ski_data* ski_ans(Ski_data* ski_data_ptr) 
{
    bool retire_flag = false;

    while (ski_data_ptr->y <= (ski_data_ptr->end + 1) && !retire_flag) {
        std::cout << "(y, x) : "  << ski_data_ptr->y << " " << ski_data_ptr->x << std::endl;

        switch (ski_resort[ski_data_ptr->y][ski_data_ptr->x]) {
            
            case 0:
                ski_data_ptr->y += 1;
                for (int i = -1; i <= 1; ++i) {
                    int temp_x = ski_data_ptr->x;
                    int temp_y = ski_data_ptr->y;
                    ski_data_ptr->x += i;

                    ski_ans(ski_data_ptr);

                    if ( i <= 0) {
                        ski_data_ptr->x = temp_x;
                        ski_data_ptr->y = temp_y;
                    } else {
                        retire_flag = true;
                    }
                }
                
                break;
            case 1:
                retire_flag = true;
                break;
            case 2:
                if (ski_data_ptr-> y <= ski_data_ptr->end)
                ski_data_ptr->y += 2;
                break;
            case 3:
                ski_data_ptr->ans += 1;
                //std::cout << "ski_data_ptr->ans : " << ski_data_ptr->ans << std::endl;
                retire_flag = true;
                break;
            default:
                break;
        }
    }
    return ski_data_ptr;
}

int main(void)
{
    int ski_height, ski_width;
    bool half_flag = false;
    
    while (std::cin >> ski_width >> ski_height) {
        if (!ski_width && !ski_height) {
            break;
        }

        for (int y = 0; y < ski_height; ++y) {
            for (int x = 1; x < ski_width + 1; ++x) {
                std::cin >> ski_resort[y][x];
                if (ski_resort[y][x] != 0) {
                    half_flag = true;
                }
            }
        }

        for (int y = 0; y < ski_height; ++y){
            ski_resort[y][0] = ski_resort[y][ski_width + 1] = 1;
        }

        for (int x = 1; x < ski_width + 1; ++x) {
            if (ski_resort[ski_height - 1][x] != 1){
                ski_resort[ski_height - 1][x] = 3;
            }
        }

        for (int x = 0; x < ski_width + 2; ++x) {
            if (x == 0 || x == ski_width + 1) {
                ski_resort[ski_height][x] = 1;
            } else {
                ski_resort[ski_height][x] = 3;
            }
        }

        int ans = 0;
        
        for (int x = 1; x < ski_width + 1; ++x) {
            Ski_data ski_data = {0, x, 0, ski_height - 1, 0};

            ans += ski_ans(&ski_data)->ans;
            
            /*std::cout << "ski_data->ans : " << ski_data.ans 
                      << " ans : " << ans << std::endl;
            */
        }

        std::cout << ans / (half_flag ? 2 : 1) << std::endl;

        /*
        for (int y = 0; y < ski_height + 1; ++y) {
            for (int x = 0; x < ski_width + 2; ++x) {
                std::cout << ski_resort[y][x];
            }
            std::cout << std::endl;
        }
        */
    }
    
    return 0;
}
        
        