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 <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string.h>
#include <set>
#include <bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

int diff_row_sun[4] = {0,0,1,1},diff_col_sun[4] = {0,1,0,1};
int diff_row[9] = {-2,-1,0,0,0,0,0,1,2},diff_col[9] = {0,0,-2,-1,0,1,2,0,0};
int N;

struct Info{
	int row,col; //?????????????????§?¨?
	int rain_table[16],day;
};

bool rangeCheck(int row,int col){
	if(row >= 0 && row <= 2 && col >= 0 && col <= 2){
		return true;
	}else{
		return false;
	}
}

std::set<tuple<int,int,int,ull>> SET; //day,row,col,hash
decltype(SET)::iterator IT;


void func(){

	SET.clear();

	int table[N+1][16];

	for(int i = 1; i <= N; i++){
		for(int k = 0; k < 16; k++)scanf("%d",&table[i][k]);
	}

	Info first;
	first.row = 1;
	first.col = 1;
	for(int i = 0; i < 16; i++)first.rain_table[i] = 0;
	first.day = 1;

	bool FLG = false;
	bool check;

	//?????\????¢?????????¨???????????´??????1????????£????????????
	if(table[1][5] == 1 || table[1][6] == 1 || table[1][9] == 1 || table[1][10] == 1){
		printf("0\n");
		return;
	}

	queue<Info> Q;
	Q.push(first);

	int weather_map[4][4],row,col,tmp_row,tmp_col,next_row,next_col;

	while(!Q.empty()){

		//??¨????????£??????????????\??°????????????????????´??°
		for(int i = 0; i < 16; i++){
			Q.front().rain_table[i]++;
		}
		//??¨????????£?????\???0?????????
		Q.front().rain_table[4*Q.front().row+Q.front().col] = 0;
		Q.front().rain_table[4*Q.front().row+Q.front().col+1] = 0;
		Q.front().rain_table[4*(Q.front().row+1)+Q.front().col] = 0;
		Q.front().rain_table[4*(Q.front().row+1)+Q.front().col+1] = 0;

		//7??\??\?????¨?????????????????°???????????£????????????
		check = true;
		for(int i = 0; i < 16; i++){
			if(Q.front().rain_table[i] >= 7){
				check = false;
				break;
			}
		}

		if(!check){
			Q.pop();
			continue;
		}

		//????????¢??????
		if(Q.front().day >= N){
			FLG = true;
			break;
		}

		//?§??????????
		for(int a = 0; a < 9; a++){
			next_row = Q.front().row + diff_row[a];
			next_col = Q.front().col + diff_col[a];

			//?§????????????¶???:??????????????????????????????????????????
			//day??\??????????????¨???????????¶??????????????????
			row = 0,col = 0;
			for(int i = 0; i < 16; i++){
				weather_map[row][col++] = table[Q.front().day+1][i];
				if(col >= 4){
					col = 0;
					row++;
				}
			}

			//?§??????????????????????????????????1?????????
			if(rangeCheck(next_row,next_col)){
				check = true;
				for(int k = 0; k < 4; k++){
					tmp_row = next_row + diff_row_sun[k];
					tmp_col = next_col + diff_col_sun[k];
					if(weather_map[tmp_row][tmp_col] == 1){
						check = false;
						break;
					}
				}
				if(check){ //???????????\???????¨????
					ull pre = 0;
					ull current = 0;

					for(int i = 0; i < 16; i++){
						current = Q.front().rain_table[i] + pre*MOD;
						pre = current;
					}

				    auto Data = make_tuple(Q.front().day+1,next_row,next_col,pre);
				    if(SET.count(Data) != 0){
				    	check = false;
				    }else{
				    	SET.insert(Data);
				    }
				}

				if(check){
					Info next;
					next.row = next_row;
					next.col = next_col;
					for(int k = 0; k < 16; k++)next.rain_table[k] = Q.front().rain_table[k];
					next.day = Q.front().day+1;
					Q.push(next);
				}
			}
		}

		Q.pop();
	}

	if(!FLG){
		printf("0\n");
	}else{
		printf("1\n");
	}

}

int main(){

	while(true){
		scanf("%d",&N);
		if(N == 0)break;

		func();
	}

	return 0;
}