#include <bits/stdc++.h>
using namespace std;
struct cl {
  int v[4], id;
};
int mv[9] = {0, 1, 2, -1, -2, 4, 8, -4, -8};
int valid(int x) {
  return x == 0 || x == 1 || x == 2 || x == 4 || x == 5 || x == 6 || x == 8 ||
         x == 9 || x == 10;
}
bool operator<(const cl& x, const cl& y) {
  for (int i = 0; i < (4); ++i)
    if (x.v[i] != y.v[i]) return x.v[i] < y.v[i];
  return x.id < y.id;
}
bool operator==(const cl& x, const cl& y) {
  for (int i = 0; i < (4); ++i)
    if (x.v[i] != y.v[i]) return false;
  return x.id == y.id;
}
int main() {
  while (true) {
    int N;
    cin >> N;
    if (N == 0) break;
    cl be;
    for (int i = 0; i < (4); ++i) be.v[i] = 0;
    be.id = 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 v[4];
        for (int j = 0; j < (4); ++j) v[j] = cloud.v[j] + 1;
        int id = cloud.id;
        if (x[id] || x[id + 1] || x[id + 4] || x[id + 5]) continue;
        if (id == 0) v[0] = 0;
        if (id == 2) v[1] = 0;
        if (id == 8) v[2] = 0;
        if (id == 10) v[3] = 0;
        bool can = true;
        for (int j = 0; j < (4); ++j)
          if (v[j] == 7) can = false;
        if (!can) continue;
        for (int j = 0; j < (9); ++j) {
          int ni = id + mv[j];
          if (!valid(ni)) continue;
          cl hoge;
          for (int k = 0; k < (4); ++k) hoge.v[k] = v[k];
          hoge.id = ni;
          nvc.push_back(hoge);
        }
      }
      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() ? 1 : 0) << endl;
  }
}
