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 JAVA):

Solution:
import java.awt.geom.*;
import java.io.*;
import java.util.*;
public class Main {
	
	//0115 start
	//0130 cording end
	
	int n;
	int [][][] data;
	boolean ans ;
	int [] vx = {0,0,1,0,-1};
	int [] vy = {0,1,0,-1,0};
	int [] dx = {0,0,1,1};
	int [] dy = {0,1,0,1};
	HashSet<String> close;
	
	private void doit(){
		Scanner sc = new Scanner(System.in);
		while(true){
			n = sc.nextInt();
			if(n == 0)break;
			data = new int[n][4][4];
			for(int i  =0; i < n; i++){
				for(int j = 0; j < 4; j++){
					for(int k = 0; k < 4; k++){
						data[i][j][k] = sc.nextInt();
					}
				}
			}
			
			ans = false;
			boolean first = false;
			close = new HashSet<String>();
			for(int i = 0; i < dx.length; i++){
				if(data[0][1 + dy[i]][1 + dy[i]] == 1){
					first = true;
					break;
				}
			}
			if(first){
				System.out.println(0);
				continue;
			}
			dfs(1,1,1,6,6,6,6);
			
			System.out.println(ans ? 1 : 0);
		}
	}

	int max = 0;
	
	private void dfs(int deep, int x, int y, int lt, int lb, int rt, int rb) {
		if(deep == n){
			ans = true;
			return;
		}
		
		for(int i = 0; i < vx.length; i++){
			for(int j = 1; j <= 2; j++){
				int xx = x + vx[i] * j;
				int yy = y + vy[i] * j;
				if(! isOK(xx, yy))continue;
				boolean flg = true;
				for(int  k = 0; k < dx.length;k++){
					int xxx = xx + dx[k];
					int yyy = yy + dy[k];
					if(data[deep][yyy][xxx] == 1){
						flg = false;
						break;
					}
				}
				if(! flg)continue;
				
				int nlt = lt-1, nlb = lb-1, nrt = rt-1, nrb = rb-1;
				if(xx == 0 && yy == 0){
					nlt = 7;
				}
				if(xx == 0 && yy == 2){
					nlb = 7;
				}
				if(xx == 2 && yy == 0){
					nrt = 7;
				}
				if(xx == 2 && yy == 2){
					nrb = 7;
				}
				
				if(nlt == 0 || nlb == 0 || nrt == 0 || nrb == 0) continue;
				String next = (deep+1) + " " + xx+yy+nlt+nlb+nrt+nrb;
				if(close.contains(next)) continue;
				close.add(next);
				dfs(deep +1, xx,yy, nlt, nlb, nrt, nrb);
			}
		}
	}

	private boolean isOK(int xx, int yy) {
		if(0<= xx && xx < 3 && 0<= yy && yy < 3){
			return true;
		}
		return false;
	}

	public static void main(String [] args){
		new Main().doit();
	}
}