#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 1, 0, -1, 0, 2, 0, -2};
int dy[] = {1, 0, -1, 0, 2, 0, -2, 0};
int n;
int fest[366];
bool mincost[(1 << 16)][9][7];
bool found = true;
struct P {
  int state, total, cur, day;
  P(int state = (1 << 29), int total = (1 << 29), int cur = (1 << 29),
    int day = (1 << 29))
      : state(state), total(total), cur(cur), day(day) {}
  bool operator<(const P& a) const { return day > a.day; }
};
int getBit(int next) {
  int nstate = 0;
  nstate |= (1 << next);
  nstate |= (1 << (next + 1));
  nstate |= (1 << (next + 4));
  nstate |= (1 << (next + 5));
  return nstate;
}
void printbit(int state) {
  bitset<16> bb(state);
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) cout << bb[j + i * 4];
    cout << endl;
  }
}
int main() {
  while (scanf("%d", &n), n) {
    for (int i = 0; i < n; i++) {
      fest[i] = false;
      int f;
      for (int j = 0; j < 16; j++) {
        scanf("%d", &f);
        if (f) fest[i] |= (1 << j);
      }
    }
    for (int i = 0; i < (1 << 16); i++)
      for (int j = 0; j < 9; j++)
        for (int k = 0; k < 7; k++) mincost[i][j][k] = false;
    int initial = 0;
    initial |= (1 << 5);
    initial |= (1 << 6);
    initial |= (1 << 9);
    initial |= (1 << 10);
    mincost[initial][5][0] = 1;
    if (initial & fest[0]) {
      cout << 0 << endl;
      continue;
    }
    found = false;
    priority_queue<P> Q;
    Q.push(P(initial, initial, 5, 1));
    while (!Q.empty()) {
      P p = Q.top();
      Q.pop();
      if (__builtin_popcount(p.total) == 16) {
        found = true;
        cout << 1 << endl;
        break;
      }
      if (p.day % 7 == 0) p.state = 0;
      for (int i = 0; i < 8; i++) {
        int nx = p.cur % 4 + dx[i];
        int ny = p.cur / 4 + dy[i];
        int next = nx + ny * 4;
        int ntotal = p.total;
        if (!(0 <= nx && nx < 3 && 0 <= ny && ny < 3)) continue;
        int nstate = getBit(next);
        ntotal |= nstate;
        if (nstate & fest[p.day]) continue;
        nstate |= p.state;
        if (!mincost[nstate][next][(p.day + 1) % 7] && p.day < n) {
          mincost[nstate][next][(p.day + 1) % 7] = 1;
          Q.push(P(nstate, ntotal, next, p.day + 1));
        }
      }
    }
    if (!found) cout << 0 << endl;
  }
  return 0;
}
