#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 366;
int N;
int D[MAX_N];
int day[4][4];
int dfs(int n, int x, int y) {
  const static int dx[9] = {-2, -1, 1, 2, 0, 0, 0, 0, 0};
  const static int dy[9] = {0, 0, 0, 0, -2, -1, 1, 2, 0};
  if (D[n] & (51 << (x + y * 4))) return 0;
  if (n == N) return 1;
  bool flag = false;
  int mem[4];
  int cnt = 0;
  for (int i = 0; i < 4; ++i) {
    for (int j = 0; j < 4; ++j) {
      if (j - x <= 1 && j - x >= 0 && i - y <= 1 && i - y >= 0) {
        mem[cnt++] = day[i][j];
        day[i][j] = 0;
      } else {
        if (++day[i][j] == 7) flag = true;
      }
    }
  }
  if (!flag) {
    for (int i = 0; i < 9; ++i) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 0 || nx >= 4) continue;
      if (ny < 0 || ny >= 4) continue;
      if (dfs(n + 1, nx, ny)) return 1;
    }
  }
  cnt = 0;
  for (int i = 0; i < 4; ++i) {
    for (int j = 0; j < 4; ++j) {
      if (j - x <= 1 && j - x >= 0 && i - y <= 1 && i - y >= 0) {
        day[i][j] = mem[cnt++];
      } else {
        --day[i][j];
      }
    }
  }
  return 0;
}
int main() {
  while (cin >> N && N) {
    for (int i = 0; i < N; ++i) {
      D[i] = 0;
      for (int j = 0; j < 16; ++j) {
        int a;
        cin >> a;
        D[i] |= a << j;
      }
    }
    fill(day[0], day[4], 0);
    cout << dfs(0, 1, 1) << endl;
  }
  return 0;
}
