#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 28;
const double INF = 1e10, EPS = 1e-9;
struct Node {
  int d, c, f[16];
  bool operator<(const Node &r) const {
    if (d != r.d) return d < r.d;
    if (c != r.c) return c < r.c;
    for (int i = 0; i < 16; i++)
      if (f[i] != r.f[i]) return f[i] < r.f[i];
    return 0;
  }
  Node(int d_, int c_, int *f_) {
    d = d_, c = c_;
    for (int i = 0; i < 16; i++) f[i] = f_[i];
  }
};
int d;
bool m[365][16];
int f[16];
set<Node> V;
int dir[] = {-1, -2, 4, 8, -4, -8, 1, 2, 0};
int dc[] = {0, 1, 4, 5};
bool solve(int now, int c) {
  if (V.count(Node(d, c, f))) return 0;
  for (int i = 0; i < 4; i++)
    if (m[now][c + dc[i]]) return 0;
  for (int i = 0; i < 4; i++) f[c + dc[i]] = -1;
  for (int i = 0; i < 16; i++)
    if (++f[i] > 6) return 0;
  if (now == d - 1) return 1;
  V.insert(Node(d, c, f));
  for (int i = 0; i < 9; i++) {
    int nc = c + dir[i];
    if (nc < 0 || nc > 10 || c % 4 == 2 && (i == 6 || i == 7) ||
        c % 4 == 0 && (i == 0 || i == 1))
      continue;
    int tmp[16];
    for (int j = 0; j < 16; j++) tmp[j] = f[j];
    if (solve(now + 1, nc)) return 1;
    for (int j = 0; j < 16; 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 < 16; j++) cin >> m[i][j];
    fill(f, f + 16, 0);
    V.clear();
    cout << solve(0, 5) << endl;
  }
  return 0;
}
