#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 28;
const double INF = 1e10, EPS = 1e-9;
struct Node {
  int d, cy, cx, f[4];
  bool operator<(const Node &r) const {
    if (d != r.d) return d < r.d;
    if (cy != r.cy) return cy < r.cy;
    if (cx != r.cx) return cx < r.cx;
    for (int i = 0; i < 4; i++)
      if (f[i] != r.f[i]) return f[i] < r.f[i];
    return 0;
  }
  Node(int d_, int y, int x, int *f_) {
    d = d_, cy = y, cx = x;
    for (int i = 0; i < 4; i++) f[i] = f_[i];
  }
};
int d;
bool m[365][4][4];
int f[4];
set<Node> V;
bool solve(int now, int cy, int cx) {
  if (V.count(Node(now, cy, cx, f))) return 0;
  for (int i = 0; i < 2; i++)
    for (int j = 0; j < 2; j++)
      if (m[now][cy + i][cx + j]) return 0;
  for (int i = 0; i < 4; i++) {
    int y = (i << 1) ? 0 : 3, x = (i & 1) ? 0 : 3;
    if (cy <= y && y <= cy + 1 && cx <= x && x <= cx + 1) f[i] = -1;
  }
  for (int i = 0; i < 4; i++)
    if (++f[i] > 6) return 0;
  if (++now == d) return 1;
  V.insert(Node(now, cy, cx, f));
  for (int y = cy - 2; y <= cy + 2; y++)
    for (int x = cx - 2; x <= cx + 2; x++)
      if (y == cy || x == cx) {
        if (y < 0 || x < 0 || y >= 4 || x >= 4) continue;
        int tmp[4];
        for (int j = 0; j < 4; j++) tmp[j] = f[j];
        if (solve(now, y, x)) return 1;
        for (int j = 0; j < 4; j++) f[j] = tmp[j];
      }
  return 0;
}
int main() {
  while (cin >> d, d) {
    for (int i = 0; i < d; i++)
      for (int j = 0; j < 4; j++)
        for (int k = 0; k < 4; k++) cin >> m[i][j][k];
    fill(f, f + 4, 0);
    V.clear();
    cout << solve(0, 1, 1) << endl;
  }
  return 0;
}
