#include<iostream>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
int vx[3]={-1,0,1};
int main(){
  int x,y;
  while(cin>>y>>x,x+y!=0){
    int Map[y+3][x+3]={0};
    int cnt=0;
    for(int i=1;i<=y;i++)
      for(int j=1;j<=x;j++)
	cin>>Map[i][j];
    queue<pair<int,int> >qii;
    for(int i=1;i<=x;i++){
      if(Map[1][i]!=1)
	qii.push(make_pair(1,i));
    }
    while(!(qii.empty())){
      pair<int,int>now = qii.front();
      qii.pop();
     
      switch(Map[now.first][now.second]){
      case 0:
	for(int i=0;i<3;i++){
	  if(Map[now.first+1][now.second+vx[i]]==0){ 
	    if(now.second<1||now.second>x)
	      continue;
	    if(now.first > y-1){
	      cnt++;
	      continue;
	    }
	    qii.push(make_pair(now.first+1,now.second+vx[i]));
	  }
	}
	if(Map[now.first+1][now.second]==2){
	  if(now.first > y-1){
	    cnt++;
	    continue;
	  }
	  qii.push(make_pair(now.first+1,now.second));
	}
	break;
      case 2:
	if(Map[now.first+2][now.second]!=1){
	  if(now.first > y-1){
	    cnt++;
	continue;
	  }
	  qii.push(make_pair(now.first+2,now.second));
	}
	break;
      }
    }
    cout<<cnt<<endl;
  }
  return 0;
}