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.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

//Weather Forecast
public class Main{

	int[][] cover = {
			{0,1,4,5},
			{1,2,5,6},
			{2,3,6,7},
			{4,5,8,9},
			{5,6,9,10},
			{6,7,10,11},
			{8,9,12,13},
			{9,10,13,14},
			{10,11,14,15}
	};
//	int[][] forbid = {
//			{0},
//			{1},
//			{2},
//			{2},
//			{0,3},
//			{0,1,3,4},
//			{1,2,4,5},
//			{2,5},
//			{2,5},
//			{3,6},
//			{3,4,6,7},
//			{4,5,7,8},
//			{5,8},
//			{5,8},
//			{6},
//			{6,7},
//			{7,8},
//			{8},
//			{8}
//	};
	int move[][] = {
			{0,1,2,3,6},
			{0,1,2,4,7},
			{0,1,2,5,8},
			{0,3,4,5,6},
			{1,3,4,5,7},
			{2,3,4,5,8},
			{0,3,6,7,8},
			{1,4,6,7,8},
			{2,5,6,7,8}
	};
	
	long trans(int[] a){
		String s = "";
		for(int x:a)s+=x;
		return Long.parseLong(s);
	}
	
	int n;
	boolean sun[][];
	Set<Long>[][] set;
	
	//xツ禿コツづ可雲ツつェposツづ可仰渉づづヲツつ「ツつゥ
	boolean ok(int x, int pos){
		for(int i=0;i<4;i++)if(sun[x][cover[pos][i]])return false;
		return true;
	}
	
	boolean get(int x, int pos, int[] a){
		if(x==n)return true;
		if(set[x][pos].contains(trans(a)))return false;
		if(!ok(x, pos)){
			set[x][pos].add(trans(a));return false;
		}
		for(int j=0;j<5;j++){
			int[] t = new int[16];
			for(int i=0;i<16;i++)t[i]=a[i]-1;
			int nx = move[pos][j];
			for(int i=0;i<4;i++)t[cover[nx][i]] = 6;
			boolean f = true;
			for(int i=0;i<16;i++)if(t[i]<0)f = false;
			if(!f)continue;
			if(get(x+1, nx, t))return true;
		}
		set[x][pos].add(trans(a));
		return false;
	}
	
	@SuppressWarnings("unchecked")
	void run(){
		Scanner sc = new Scanner(System.in);
		for(;;){
			n = sc.nextInt();
			if(n==0)break;
			sun = new boolean[n][16];
			for(int i=0;i<n;i++)for(int j=0;j<16;j++)sun[i][j] = sc.nextInt()==1;
			set = new HashSet[n][9];
			for(int i=0;i<n;i++)for(int j=0;j<9;j++)set[i][j] = new HashSet<Long>();
			int[] a = new int[16];
			Arrays.fill(a, 5);
			if(!ok(0, 4)){
				System.out.println(0);continue;
			}
			for(int j=0;j<4;j++)a[cover[4][j]] = 6;
			System.out.println(get(0, 4, a)?1:0);
		}
	}
	
	public static void main(String[] args) {
		new Main().run();
	}
}