#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 28;
const long long MOD = 1000000007;
int n;
bool flg;
vector<int> a;
set<unsigned long long> s;
int d[9][9] = {
    {0, 1, 2, 3, 4, 6, INF, INF, INF},     {-1, 0, 1, 2, 3, 4, 6, INF, INF},
    {-2, -1, 0, 2, 3, 6, INF, INF, INF},   {-3, -2, 0, 1, 2, 3, 4, INF, INF},
    {-4, -3, -2, -1, 0, 1, 2, 3, 4},       {-4, -3, -2, -1, 0, 2, 3, INF, INF},
    {-6, -3, -2, 0, 1, 2, INF, INF, INF},  {-6, -4, -3, -2, -1, 0, 1, INF, INF},
    {-6, -4, -3, -2, -1, 0, INF, INF, INF}};
bool chk(int cnt, int cl, string &now) {
  if (cl == 0 && (0b1100110000000000 & a[cnt]) == 0) {
    now[0] = now[1] = now[4] = now[5] = '7';
    return true;
  }
  if (cl == 1 && (0b0110011000000000 & a[cnt]) == 0) {
    now[1] = now[2] = now[5] = now[6] = '7';
    return true;
  }
  if (cl == 2 && (0b0011001100000000 & a[cnt]) == 0) {
    now[2] = now[3] = now[6] = now[7] = '7';
    return true;
  }
  if (cl == 3 && (0b0000110011000000 & a[cnt]) == 0) {
    now[4] = now[5] = now[8] = now[9] = '7';
    return true;
  }
  if (cl == 4 && (0b0000011001100000 & a[cnt]) == 0) {
    now[5] = now[6] = now[9] = now[10] = '7';
    return true;
  }
  if (cl == 5 && (0b0000001100110000 & a[cnt]) == 0) {
    now[6] = now[7] = now[10] = now[11] = '7';
    return true;
  }
  if (cl == 6 && (0b0000000011001100 & a[cnt]) == 0) {
    now[8] = now[9] = now[12] = now[13] = '7';
    return true;
  }
  if (cl == 7 && (0b0000000001100110 & a[cnt]) == 0) {
    now[9] = now[10] = now[13] = now[14] = '7';
    return true;
  }
  if (cl == 8 && (0b0000000000110011 & a[cnt]) == 0) {
    now[10] = now[11] = now[14] = now[15] = '7';
    return true;
  }
  return false;
}
void dfs(int cnt, int cl, string now) {
  if (flg) return;
  for (int i = 0; i < (int)16; i++) {
    if (now[i] == '0') return;
    now[i]--;
  }
  if (cnt == n) flg = true;
  for (int i = 0; i < (int)9; i++) {
    if (d[cl][i] == INF) break;
    string now2 = now;
    if (chk(cnt, cl + d[cl][i], now2)) {
      unsigned long long u = stoull(now2) * 10000 + cnt + 10 + cl + d[cl][i];
      if (!s.count(u)) {
        s.insert(u);
        dfs(cnt + 1, cl + d[cl][i], now2);
      }
    }
  }
  return;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  while (cin >> n, n) {
    flg = false;
    a.clear();
    s.clear();
    for (int i = 0; i < (int)n; i++) {
      a.push_back(0);
      for (int j = 0; j < (int)16; j++) {
        int in;
        cin >> in;
        if (j) a[i] <<= 1;
        a[i] |= in;
      }
    }
    if ((a[0] & 0b0000011001100000)) {
      cout << 0 << endl;
      continue;
    }
    dfs(1, 4, "5555566556655555");
    cout << flg << endl;
  }
  return 0;
}
