#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 28;
const double INF = 1e10, EPS = 1e-9;
int d;
bool m[365][16];
int f[16];
int dir[] = {-1, -2, 4, 8, -4, -8, 1, 2, 0};
int dc[] = {0, 1, 4, 5};
bool solve(int now, int c) {
  for (int i = 0; i < 4; i++)
    if (m[now][c + dc[i]]) return 0;
  for (int i = 0; i < 4; i++) f[c + dc[i]] = -1;
  for (int i = 0; i < 16; i++)
    if (++f[i] > 6) return 0;
  if (now == d - 1) return 1;
  if (now < d - 3)
    for (int i = 0; i < 16; i++) {
      if ((i & 3) == (c & 3) || (i & 3) == (c & 3) + 1) continue;
      if ((i >> 2) == (c >> 2) || (i >> 2) == (c >> 2) + 1) continue;
      if (f[i] > 4) return 0;
    }
  for (int i = 0; i < 9; i++) {
    int nc = c + dir[i];
    if (nc < 0 || nc > 10 || c % 4 == 2 && (i == 6 || i == 7) ||
        c % 4 == 0 && (i == 0 || i == 1))
      continue;
    int tmp[16];
    for (int j = 0; j < 16; j++) tmp[j] = f[j];
    if (solve(now + 1, nc)) return 1;
    for (int j = 0; j < 16; j++) f[j] = tmp[j];
  }
  return 0;
}
int main() {
  while (cin >> d, d) {
    for (int i = 0; i < d; i++)
      for (int j = 0; j < 16; j++) cin >> m[i][j];
    fill(f, f + 16, 0);
    cout << solve(0, 5) << endl;
  }
  return 0;
}
