import std.stdio;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;

void main() {
    int n;
    while (readf("%d\n", &n), n) {
        int[][] fs = new int[][n];
        foreach (i; 0 .. n) {
            fs[i] = readln.chomp.split(" ").map!(to!int).array;
        }
        /*
        foreach (d; 0 .. n) {
            foreach (i; 0 .. 4) {
                fs[d][i * 4 .. i * 4 + 4].writeln;
            }
            writeln;
        }
        writeln;
        */
        bool dfs(int d, int y, int x, int[] corners) {
            assert(0 <= y && y <= 2 && 0 <= x && x <= 2);
            int[][] F = new int[][](4, 4);
            foreach (i, f; fs[d - 1]) {
                F[i / 4][i % 4] = f;
            }
            foreach (i; 0 .. 2) {
                foreach (j; 0 .. 2) {
                    if (F[y + i][x + j]) {
                        return false;
                    }
                }
            }
            if (y == 0 && x == 0) corners[0] = 0;
            if (y == 0 && x == 2) corners[1] = 0;
            if (y == 2 && x == 0) corners[2] = 0;
            if (y == 2 && x == 2) corners[3] = 0;
            if (corners.any!"a >= 7") return false;
            if (d == n) {
                //writeln(y, " ", x, " ", corners);
                return true;
            }
            corners = corners.map!("a + 1").array;
            // y固定
            for (int dx = -2; dx <= 2; dx++) {
                int nx = x + dx;
                if (nx < 0 || nx > 2) continue;
                if (dfs(d + 1, y, x + dx, corners.dup)) {
                    return true;
                }
            }
            // x固定
            for (int dy = -2; dy <= 2; dy++) {
                int ny = y + dy;
                if (ny < 0 || ny > 2) continue;
                if (dfs(d + 1, ny, x, corners.dup)) {
                    return true;
                }
            }
            return false;
        }
        writeln(dfs(1, 1, 1, [1, 1, 1, 1]) ? 1 : 0);
    }
}