#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[400];
bool mincost[(1 << 16)][9];
bool found = true;
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;
  }
}
void dfs(int state, int cur, int day) {
  if (__builtin_popcount(state) == 16) {
    found = true;
    return;
  }
  for (int i = 0; i < 8; i++) {
    int nx = cur % 4 + dx[i];
    int ny = cur / 4 + dy[i];
    int next = nx + ny * 4;
    if (!(0 <= nx && nx < 3 && 0 <= ny && ny < 3)) continue;
    int nstate = getBit(next);
    if (nstate & fest[day]) continue;
    nstate |= state;
    if (!mincost[nstate][next] && day < n) {
      mincost[nstate][next] = 1;
      dfs(nstate, next, day + 1);
      if (found) return;
    }
  }
}
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++) mincost[i][j] = false;
    int initial = 0;
    initial |= (1 << 5);
    initial |= (1 << 6);
    initial |= (1 << 9);
    initial |= (1 << 10);
    mincost[initial][5] = 1;
    if (initial & fest[0]) {
      cout << 0 << endl;
      continue;
    }
    found = false;
    dfs(initial, 5, 1);
    cout << found << endl;
  }
  return 0;
}
