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>
using namespace std;

constexpr int MAX_N = 365;
constexpr int SIZE = 16;
constexpr int rain[3][3] = {{0x0033, 0x0066, 0x00cc},
							{0x0330, 0x0660, 0x0cc0},
							{0x3300, 0x6600, 0xcc00}};
constexpr int dx[9] = {0, 1, -1, 2, -2, 0, 0, 0, 0};
constexpr int dy[9] = {0, 0, 0, 0, 0, 1, -1, 2, -2};

int n;
int festivals[MAX_N];
unordered_map<int, int> get_index;
unordered_set<int> visited[MAX_N][3][3];

inline int calc_value(int idx, const array<int, 7> &prev) {
	int res = 0;
	for(int i = 0; i < 7; ++i) {
		if(idx != i) res = res * 9 + get_index[prev[i]];
	}
	return res;
}

bool dfs(int day, int x, int y, array<int, 7> &prev) {
	if(day == n) return true;

	const int idx = day % 7;
	const int value = calc_value(idx, prev);
	if(visited[day][x][y].count(value)) return false;


	int need = (1 << SIZE) - 1;
	for(int i = 0; i < 7; ++i) {
		if(idx != i) need &= ~prev[i];
	}

	for(int d = 0; d < 9; ++d) {
		const int nx = x + dx[d];
		const int ny = y + dy[d];
		if(nx < 0 || ny < 0 || nx >= 3 || ny >= 3) continue;
		if(need & ~rain[ny][nx]) continue;
		if(festivals[day] & rain[ny][nx]) continue;

		const int tmp = prev[idx];
		prev[idx] = rain[ny][nx];
		if(dfs(day + 1, nx, ny, prev)) return true;
		prev[idx] = tmp;
	}
	visited[day][x][y].insert(value);
	return false;
}

int main() {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	for(int x = 0; x < 3; ++x) {
		for(int y = 0; y < 3; ++y) {
			get_index[rain[y][x]] = y * 3 + x;
		}
	}

	while(cin >> n && n) {
		memset(festivals, 0, sizeof(festivals));
		for(int i = 0; i < n; ++i) {
			for(int j = 0; j < SIZE; ++j) {
				int in;
				cin >> in;
				festivals[i] |= (in << j);
			}
		}

		if(festivals[0] & rain[1][1]) {
			cout << 0 << endl;
			continue;
		}

		array<int, 7> prev;
		prev.fill((1 << SIZE) - 1);
		prev[0] = rain[1][1];

		for(int i = 0; i < n; ++i) {
			for(int x = 0; x < 3; ++x) {
				for(int y = 0; y < 3; ++y) {
					visited[i][x][y].clear();
				}
			}
		}

		cout << dfs(1, 1, 1, prev) << endl;
	}

	return EXIT_SUCCESS;
}