#include <bits/stdc++.h>
using namespace std;
bool vis[366][4][4][8][8][8][8] = {0};
bool isevent[366][4][4];
int dx[] = {-2, -1, 0, 0, 1, 2, 0, 0, 0};
int dy[] = {0, 0, -2, -1, 0, 0, 1, 2, 0};
bool canstay(int day, int tary, int tarx) {
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
      if (isevent[day][tary + i][tarx + j]) return false;
    }
  }
  return true;
}
bool is_rain_full(int cur[4][4]) {
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
      if (cur[i][j] == 7) return false;
    }
  }
  return true;
}
void rain(int ney, int nex, int cur[4][4]) {
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
      cur[i][j]++;
    }
  }
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
      cur[ney + i][nex + j] = 0;
    }
  }
}
bool dfs(int n, int now, int y, int x, int lu, int ru, int ld, int rd,
         int cur[4][4]) {
  if (!is_rain_full(cur)) return false;
  if (n == now) return true;
  if (vis[now][y][x][lu][ru][ld][rd]) return false;
  vis[now][y][x][lu][ru][ld][rd] = true;
  if (!canstay(now, y, x)) return false;
  int tmp[4][4];
  for (int k = 0; k < 9; k++) {
    int nex = x + dx[k], ney = y + dy[k];
    if (nex < 0 || ney < 0 || nex > 2 || ney > 2) continue;
    for (int i = 0; i < 4; i++)
      for (int j = 0; j < 4; j++) tmp[i][j] = cur[i][j];
    rain(ney, nex, cur);
    if (dfs(n, now + 1, ney, nex, cur[0][0], cur[0][3], cur[3][0], cur[3][3],
            cur))
      return true;
    for (int i = 0; i < 4; i++)
      for (int j = 0; j < 4; j++) cur[i][j] = tmp[i][j];
  }
  return false;
}
int main() {
  int n;
  while (cin >> n && n) {
    for (int k = 0; k < n; k++) {
      for (int i = 0; i < 4; i++)
        for (int j = 0; j < 4; j++) cin >> isevent[k][i][j];
    }
    for (int i = 0; i < 4; i++)
      for (int j = 0; j < 4; j++) isevent[n][i][j] = 0;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < 4; j++)
        for (int k = 0; k < 4; k++)
          for (int l = 0; l < 8; l++)
            for (int m = 0; m < 8; m++)
              for (int o = 0; o < 8; o++)
                for (int p = 0; p < 8; p++) vis[i][j][k][l][m][o][p] = false;
    int cur[4][4] = {0};
    cout << dfs(n, 0, 1, 1, 0, 0, 0, 0, cur) << endl;
  }
  return false;
}
