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 r(i,n) for(int i=0;i<n;i++)
using namespace std;
bool dp[366][16][8][8][8][8];
int dx[]={-2,2,-1,1,0,0,0,0,0};
int dy[]={0,0,0,0,1,-1,0,2,-2};
int t[366][17],p[4],n;
int main(){
  while(cin>>n,n){
    int ans=0;
    memset(dp,0,sizeof(dp));
    r(i,n)r(j,16)cin>>t[i][j];
    dp[0][5][1][1][1][1]=1;
    r(a,n-1)r(b,16)r(c,7)r(d,7)r(e,7)r(f,7)
    if(dp[a][b][c][d][e][f]){
      int x1=b%4,y1=b/4;
      r(i,9){
        int x=x1+dx[i],y=y1+dy[i],flag=0;
        if(x<0||y<0||2<x||2<y)continue;
        p[0]=y*4+x,p[1]=y*4+x+1;
        p[2]=(y+1)*4+x,p[3]=(y+1)*4+x+1;
        r(k,4)if(t[a+1][p[k]])flag++;
        if(!flag){
          if(p[0]==0)dp[a+1][p[0]][0][d+1][e+1][f+1]=1;
          else if(p[0]==2)dp[a+1][p[0]][c+1][0][e+1][f+1]=1;
          else if(p[0]==8)dp[a+1][p[0]][c+1][d+1][0][f+1]=1;
          else if(p[0]==10)dp[a+1][p[0]][c+1][d+1][e+1][0]=1;
          else dp[a+1][p[0]][c+1][d+1][e+1][f+1]=1;
        }
      }
    }
    r(b,16)r(c,7)r(d,7)r(e,7)r(f,7)
      if(dp[n-1][b][c][d][e][f])ans++;
    if(t[0][5]||t[0][6]||t[0][9]||t[0][10])ans=0;
    cout<<(ans?1:0)<<endl;
  }
}