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 <cstdio>
#include <iostream>
#include <set>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long int ll;

struct Elem {
    int pos, day;
    vector<int> sunny;
    bool operator<(const Elem &x) const {
        if(day != x.day) return day < x.day;
        if(pos != x.pos) return pos < x.pos;
        return sunny < x.sunny;
    }
};

// generate binary string (not less than k-digit)
string to_binString(int n, int k) {
    string ret = "";
    while(n) ret += ('0' + (n&1)), n >>= 1;
    while(ret.length() < k) ret += '0';
    // reverse(ret.begin(), ret.end());
    return ret;
}

int N, val[400];
set<Elem> S;

int d[9][4] = {
    // URDL
    {-4, -3, 0, 1},
    {1, 2, 5, 6},
    {4, 5, 8, 9},
    {-1, 0, 3, 4},

    {-8, -7, -4, -3},
    {2, 3, 6, 7},
    {8, 9, 12, 13},
    {-2, -1, 2, 3},

    {0, 1, 4, 5}
};

bool ans = false;
void solve(Elem e) {
    if(!S.count(e)) {
        S.insert(e);
        if(e.day == N) {
            ans = true;
            return;
        }
        if(ans) return;
        for(int k=0; k<9; k++) {
            if(k == 0 && e.pos/4 == 0) continue;
            if(k == 1 && e.pos%4 == 2) continue;
            if(k == 2 && e.pos/4 == 2) continue;
            if(k == 3 && e.pos%4 == 0) continue;

            if(k == 4 && e.pos/4 <  2) continue;
            if(k == 5 && e.pos%4 != 0) continue;
            if(k == 6 && e.pos/4 != 0) continue;
            if(k == 7 && e.pos%4 <  2) continue;
            bool ok = true;
            int nbit = (1 << 16) - 1, npos = e.pos + d[k][0];
            for(int x=0; x<4; x++) {
                int c = d[k][x] + e.pos;
                nbit ^= (1 << c);
            }
            /*
            printf("day = %d, dir = %d, bit = \n", e.day, k);
            for(int i=0; i<4; i++) {
                for(int j=0; j<4; j++) printf("%d", val[e.day]>>(i*4+j)&1);
                printf("  ");
                for(int j=0; j<4; j++) printf("%d", (nbit >> (i*4+j) & 1));
                printf("\n");
            }
            printf("\n");
            */
            if((nbit & val[e.day]) != val[e.day]) continue;
            vector<int> nxt_vec = e.sunny;
            for(int i=0; i<16; i++) {
                if(nbit >> i & 1) {
                    if(nxt_vec[i] + 1 > 6) ok = false;
                    nxt_vec[i]++;
                }
                else {
                    nxt_vec[i] = 0;
                }
            }

            /*
            for(int i=0; i<4; i++) {
                for(int j=0; j<4; j++) {
                    printf("%d", nxt_vec[i*4+j]);
                }
                printf("\n");
            }
            */

            if(ok) {
                Elem nxt{npos, e.day+1, nxt_vec};
                solve(nxt);
            }
        }
    }
}

int main() {
    while(1) {
        S.clear();
        scanf("%d", &N);
        if(!N) break;
        memset(val, 0, sizeof(val));

        for(int i=0; i<N; i++) {
            for(int j=0; j<16; j++) {
                int p; scanf("%d", &p);
                // printf("%d", p);
                // if(j%4 == 3) printf("\n");
                val[i] = (val[i] << 1) | p;
            }
        }

        // 初日の判定
        vector<int> vec(16, 1);
        int bit = (1 << 16) - 1, pos = 5;
        for(int i=0; i<4; i++) {
            int c = d[8][i] + pos;
            bit ^= (1 << c);
            vec[c] = 0;
        }
        if((bit & val[0]) != val[0]) {
            printf("0\n");
            continue;
        }

        ans = false;
        Elem s{pos, 1, vec};
        solve(s);
        printf("%d\n", ans);
    }
    return 0;
}