#include <bits/stdc++.h>
using namespace std;
int diff_row_sun[4] = {0, 0, 1, 1}, diff_col_sun[4] = {0, 1, 0, 1};
int diff_row[9] = {-2, -1, 0, 0, 0, 0, 0, 1, 2},
    diff_col[9] = {0, 0, -2, -1, 0, 1, 2, 0, 0};
int N;
struct Info {
  int row, col;
  int rain_table[16], day;
};
bool rangeCheck(int row, int col) {
  if (row >= 0 && row <= 2 && col >= 0 && col <= 2) {
    return true;
  } else {
    return false;
  }
}
void func() {
  int table[N + 1][16];
  for (int i = 1; i <= N; i++) {
    for (int k = 0; k < 16; k++) scanf("%d", &table[i][k]);
  }
  Info first;
  first.row = 1;
  first.col = 1;
  for (int i = 0; i < 16; i++) first.rain_table[i] = 0;
  first.day = 1;
  bool FLG = false;
  bool check;
  if (table[1][5] == 1 || table[1][6] == 1 || table[1][9] == 1 ||
      table[1][10] == 1) {
    printf("0\n");
    return;
  }
  queue<Info> Q;
  Q.push(first);
  int map[4][4], row, col, tmp_row, tmp_col, next_row, next_col;
  while (!Q.empty()) {
    for (int i = 0; i < 16; i++) {
      Q.front().rain_table[i]++;
    }
    Q.front().rain_table[4 * Q.front().row + Q.front().col] = 0;
    Q.front().rain_table[4 * Q.front().row + Q.front().col + 1] = 0;
    Q.front().rain_table[4 * (Q.front().row + 1) + Q.front().col] = 0;
    Q.front().rain_table[4 * (Q.front().row + 1) + Q.front().col + 1] = 0;
    check = true;
    for (int i = 0; i < 16; i++) {
      if (Q.front().rain_table[i] >= 7) {
        check = false;
        break;
      }
    }
    if (!check) {
      Q.pop();
      continue;
    }
    if (Q.front().day >= N) {
      FLG = true;
      break;
    }
    for (int a = 0; a < 9; a++) {
      next_row = Q.front().row + diff_row[a];
      next_col = Q.front().col + diff_col[a];
      row = 0, col = 0;
      for (int i = 0; i < 16; i++) {
        map[row][col++] = table[Q.front().day + 1][i];
        if (col >= 4) {
          col = 0;
          row++;
        }
      }
      if (rangeCheck(next_row, next_col)) {
        check = true;
        for (int k = 0; k < 4; k++) {
          tmp_row = next_row + diff_row_sun[k];
          tmp_col = next_col + diff_col_sun[k];
          if (map[tmp_row][tmp_col] == 1) {
            check = false;
            break;
          }
        }
        if (check) {
          Info next;
          next.row = next_row;
          next.col = next_col;
          for (int k = 0; k < 16; k++)
            next.rain_table[k] = Q.front().rain_table[k];
          next.day = Q.front().day + 1;
          Q.push(next);
        }
      }
    }
    Q.pop();
  }
  if (!FLG) {
    printf("0\n");
  } else {
    printf("1\n");
  }
}
int main() {
  while (true) {
    scanf("%d", &N);
    if (N == 0) break;
    func();
  }
  return 0;
}
