#include <bits/stdc++.h>
using namespace std;
struct cl {
  int a, b, c, d, id;
};
bool operator<(const cl& x, const cl& y) {
  if (x.a != y.a) return x.a < y.a;
  if (x.b != y.b) return x.b < y.b;
  if (x.c != y.c) return x.c < y.c;
  if (x.d != y.d) return x.d < y.d;
  return x.id < y.id;
}
bool operator==(const cl& x, const cl& y) {
  return x.a == y.a && x.b == y.b && x.c == y.c && x.d == y.d && x.id == y.id;
}
inline bool ok(int x) { return 0 <= x && x < 16 && x % 4 < 3 && x / 4 < 3; }
int mv[9] = {0, 1, 2, -1, -2, 4, 8, -4, -8};
int main() {
  while (true) {
    int N;
    cin >> N;
    if (N == 0) break;
    cl be = cl{7, 7, 7, 7, 5};
    vector<cl> vc;
    vc.push_back(be);
    for (int i = 0; i < (N); ++i) {
      int x[16];
      for (int j = 0; j < (16); ++j) cin >> x[j];
      vector<cl> nvc;
      for (auto cloud : vc) {
        int a = cloud.a, b = cloud.b, c = cloud.c, d = cloud.d, id = cloud.id;
        if (x[id] || x[id + 1] || x[id + 4] || x[id + 5]) continue;
        for (int di = 0; di < (9); ++di) {
          int ni = id + mv[di];
          if (!ok(ni)) continue;
          int na = a - 1, nb = b - 1, nc = c - 1, nd = d - 1;
          if (ni == 0) na = 7;
          if (ni == 2) nb = 7;
          if (ni == 8) nc = 7;
          if (ni == 10) nd = 7;
          if (na == 0 || nb == 0 || nc == 0 || nd == 0) continue;
          nvc.push_back(cl{na, nb, nc, nd, ni});
        }
      }
      sort((nvc).begin(), (nvc).end());
      nvc.erase(unique((nvc).begin(), (nvc).end()), nvc.end());
      vc = nvc;
      cout << "vc.size()"
           << " " << vc.size() << endl;
    }
    cout << (vc.size() > 0) << endl;
  }
}
