#include <bits/stdc++.h>
using namespace std;
bool getBit(long long num, long long i) { return ((num & (1 << i)) != 0); }
long long setBit(long long num, long long i) {
  if (i < 0) return num;
  return num | (1 << i);
}
long long clearBit(long long num, long long i) {
  long long mask = ~(1 << i);
  return num & mask;
}
long long updateBit(long long num, long long i, long long v) {
  long long mask = ~(1 << i);
  return (num & mask) | (v << i);
}
long long updateRain(long long num, long long i, long long zero = 1) {
  long long k = 1;
  for (long long j = 0; j < (i); j++) k *= 10;
  long long n = num / k % 10;
  return num - n * k + (n + 1) * k * zero;
}
long long getRain(long long num, long long i) {
  long long k = 1;
  for (long long j = 0; j < (i); j++) k *= 10;
  return num / k % 10;
}
const long long dy[16] = {0, -1, 0, 1, 0, -2, 0, 2, 0};
const long long dx[16] = {1, 0, -1, 0, 2, 0, -2, 0, 0};
long long n;
long long fes[366];
set<pair<long long, long long>> m;
bool check(long long& rain, long long ny, long long nx, long long day) {
  for (long long j = 0; j < (4); j++) {
    for (long long k = 0; k < (4); k++) {
      if (j >= ny && j <= ny + 1 && k >= nx && k <= nx + 1) {
        rain = updateRain(rain, j * 4 + k, 0);
      } else {
        if (getRain(rain, j * 4 + k) >= 6) {
          return false;
        } else {
          rain = updateRain(rain, j * 4 + k);
        }
      }
    }
  }
  return true;
}
bool dfs(long long rain, long long cur, long long day) {
  if (m.count(make_pair(rain, cur))) return false;
  m.emplace(make_pair(rain, cur));
  if (getBit(fes[day], cur)) return false;
  if (day == n) {
    for (long long i = 0; i < (16); i++) {
      if (getRain(rain, i) >= 7) return false;
    }
    return true;
  }
  bool res = false;
  for (long long i = 0; i < (9); i++) {
    long long ny = cur / 4 + dy[i];
    long long nx = cur % 4 + dx[i];
    if (ny < 0 || ny >= 3 || nx < 0 || nx >= 3) continue;
    long long cp = rain;
    if (check(cp, ny, nx, day)) {
      res |= dfs(cp, ny * 4 + nx, day + 1);
    }
  }
  return res;
}
signed main() {
  while (cin >> n, n) {
    m.clear();
    memset(fes, 0, sizeof(fes));
    for (long long i = 0; i < (n); i++) {
      for (long long j = 0; j < (16); j++) {
        long long x;
        cin >> x;
        if (x) {
          fes[i] = setBit(fes[i], j);
          fes[i] = setBit(fes[i], j - 4);
          fes[i] = setBit(fes[i], j - 1);
          fes[i] = setBit(fes[i], j - 5);
        }
      }
    }
    cout << dfs(0, 5, 0) << endl;
  }
}
