Problem: You are the God of Wind.

By moving a big cloud around, you can decide the weather: it invariably rains under the cloud, and the sun shines everywhere else.

But you are a benign God: your goal is to give enough rain to every field in the countryside, and sun to markets and festivals. Small humans, in their poor vocabulary, only describe this as “weather forecast”.

You are in charge of a small country, called Paccimc. This country is constituted of 4 × 4 square areas, denoted by their numbers.

<image>

Your cloud is of size 2 × 2, and may not cross the borders of the country.

You are given the schedule of markets and festivals in each area for a period of time.

On the first day of the period, it is raining in the central areas (6-7-10-11), independently of the schedule.

On each of the following days, you may move your cloud by 1 or 2 squares in one of the four cardinal directions (North, West, South, and East), or leave it in the same position. Diagonal moves are not allowed. All moves occur at the beginning of the day.

You should not leave an area without rain for a full week (that is, you are allowed at most 6 consecutive days without rain). You don’t have to care about rain on days outside the period you were given: i.e. you can assume it rains on the whole country the day before the period, and the day after it finishes.



Input

The input is a sequence of data sets, followed by a terminating line containing only a zero.

A data set gives the number N of days (no more than 365) in the period on a single line, followed by N lines giving the schedule for markets and festivals. The i-th line gives the schedule for the i-th day. It is composed of 16 numbers, either 0 or 1, 0 standing for a normal day, and 1 a market or festival day. The numbers are separated by one or more spaces.

Output

The answer is a 0 or 1 on a single line for each data set, 1 if you can satisfy everybody, 0 if there is no way to do it.

Example

Input

1
0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
7
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 1 0 0 0 0 1 1 0 0 1
0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 1
0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0
0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0
7
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0
0 0 0 1 0 0 0 0 0 0 1 0 1 0 0 0
0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
15
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0
0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0
1 0 0 1 1 0 0 0 0 1 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 0
0


Output

0
1
0
1

Solution (in CPP):

Solution:
#include<bits/stdc++.h>
#define N 366
#define L 16
#define M 9
#define D 7
using namespace std;
bool dp[N][M][D][D][D][D];
int d[N][L];
int dy[5]={0,-1,0,1,0};
int dx[5]={0,0,1,0,-1};
int n;

int main(){
  while(1){
    cin>>n;
    if(!n)break;
    memset(d,0,sizeof(d));
    for(int i=0;i<n;i++)
      for(int j=0;j<L;j++)
	cin>>d[i][j];
    memset(dp,0,sizeof(dp));
    dp[0][4][1][1][1][1]=true;
    bool ans=false;
    for(int i=0;i<n-1;i++)
      for(int j=0;j<M;j++)
	for(int k1=0;k1<D;k1++)
	  for(int k2=0;k2<D;k2++)
	    for(int k3=0;k3<D;k3++)
	      for(int k4=0;k4<D;k4++){
		if(!dp[i][j][k1][k2][k3][k4])continue;
		int y=j/3,x=j%3;
		for(int l=0;l<5;l++){
		  for(int l2=0;l2<5;l2++){
		    if(!(l2==0||l==l2))continue;
		    int ny=y+dy[l]+dy[l2],nx=x+dx[l]+dx[l2];
		    if(ny<0||nx<0||3<=ny||3<=nx)continue;
		    int nj=ny*3+nx,flag=1;
		    if(nj<3){
		      if(d[i+1][nj]||d[i+1][nj+1]||d[i+1][nj+4]||d[i+1][nj+5])flag=0;
		    }else if(nj<6){
		      if(d[i+1][nj+1]||d[i+1][nj+2]||d[i+1][nj+5]||d[i+1][nj+6])flag=0;
		    }
		    else if(d[i+1][nj+2]||d[i+1][nj+3]||d[i+1][nj+6]||d[i+1][nj+7])flag=0;
		    int nk1=k1,nk2=k2,nk3=k3,nk4=k4;
		    if(nj==0)nk1=0;
		    else if(nk1==6)flag=0;
		    else nk1++;
		    if(nj==2)nk2=0;
		    else if(nk2==6)flag=0;
		    else nk2++;
		    if(nj==6)nk3=0;
		    else if(nk3==6)flag=0;
		    else nk3++;
		    if(nj==8)nk4=0;
		    else if(nk4==6)flag=0;
		    else nk4++;
		    if(flag)dp[i+1][nj][nk1][nk2][nk3][nk4]=true;
		  }
		}
	      }
    for(int i=0;i<M;i++)
      for(int k1=0;k1<D;k1++)
	for(int k2=0;k2<D;k2++)
	    for(int k3=0;k3<D;k3++)
	      for(int k4=0;k4<D;k4++)
		if(dp[n-1][i][k1][k2][k3][k4])ans=true;
    if(d[0][5]||d[0][6]
       ||d[0][9]||d[0][10])ans=false;
    cout<<ans<<endl;
  }
  return 0;
}