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 <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <cstring>
#include <functional>
#include <cmath>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
#define rep1(i,n) for(int i=1;i<=(n);++i)
#define all(c) (c).begin(),(c).end()
#define fs first
#define sc second
#define pb push_back
#define show(x) cout << #x << " " << x << endl
struct cl{int v[4],x,y;};
int dx[9]={0,0,0,0,0,1,-1,2,-2};
int dy[9]={0,1,-1,2,-2,0,0,0,0};
int valid(int x,int y){
	return 0<=x&&x<3&&0<=y&&y<3;
}
bool operator<(const cl& x,const cl& y){
	rep(i,4) if(x.v[i]!=y.v[i]) return x.v[i]<y.v[i];
	return x.x*4+x.y<y.x*4+y.y;
}
bool operator==(const cl& x,const cl& y){
	rep(i,4) if(x.v[i]!=y.v[i]) return false;
	return x.x*4+x.y==y.x*4+y.y;
}
int main(){
	while(true){
		int N;
		cin>>N;
		if(N==0) break;
		cl be;
		rep(i,4) be.v[i]=0;
		be.x=1,be.y=1;
		vector<cl> vc,nvc;
		vc.pb(be);
		rep(i,N){
			int f[4][4];
			rep(j,4) rep(k,4) cin>>f[j][k];
			for(auto cloud:vc){
				int v[4];
				rep(j,4) v[j]=cloud.v[j]+1;
				int x=cloud.x,y=cloud.y;
				if(x==0&&y==0) v[0]=0;
				if(x==0&&y==2) v[1]=0;
				if(x==2&&y==0) v[2]=0;
				if(x==2&&y==2) v[3]=0;
				bool can=true;
				rep(ii,2) rep(jj,2) if(f[x+ii][y+jj]) can=false;
				rep(j,4) if(v[j]==7) can=false;
				if(!can) continue;
				rep(j,9){
					int nx=x+dx[j],ny=y+dy[j];
					if(!valid(nx,ny)) continue;
					cl hoge;
					rep(k,4) hoge.v[k]=v[k];
					hoge.x=nx,hoge.y=ny;
					nvc.pb(hoge);
				}
			}
			sort(all(nvc));
			nvc.erase(unique(all(nvc)),nvc.end());
			vc=nvc;
			nvc.clear();
//			show(vc.size());
		}
		cout<<(vc.size()?1:0)<<endl;
	}
}