#include <bits/stdc++.h>
using namespace std;
static const double EPS = 1e-8;
static const int tx[] = {-8, -4, -2, -1, 1, 2, 4, 8, 0};
class State {
 public:
  long long int no_rainny_summary;
  int dx;
  State(int _no_rainny_days[20], int _dx) {
    dx = _dx;
    long long int digit = 1;
    no_rainny_summary = 0;
    for (int i = 0; i < 16; i++) {
      no_rainny_summary += _no_rainny_days[i] * digit;
      digit *= 10;
    }
  }
  bool operator<(const State& s) const {
    if (no_rainny_summary == s.no_rainny_summary) {
      return dx < s.dx;
    } else {
      return no_rainny_summary < s.no_rainny_summary;
    }
  }
  bool operator>(const State& s) const {
    if (no_rainny_summary == s.no_rainny_summary) {
      return dx > s.dx;
    } else {
      return no_rainny_summary > s.no_rainny_summary;
    }
  }
};
set<State> visited[400];
int stage[400];
int no_rainny_days[20];
void print_stage(int num) {
  for (int y = 0; y < 4; y++) {
    for (int x = 0; x < 4; x++) {
      printf("%d ", num & (1 << (y * 4 + x)) ? 1 : 0);
    }
    printf("\n");
  }
  printf("\n");
}
int rain(int bits) {
  for (int i = 0; i < 16; i++) {
    if (bits & (1 << i)) {
      no_rainny_days[i] = 0;
    } else {
      no_rainny_days[i]++;
    }
  }
}
bool check() {
  for (int i = 0; i < 16; i++) {
    if (no_rainny_days[i] >= 7) return false;
  }
  return true;
}
int dfs(int day, int cloud_pos, int total_days) {
  int res = 0;
  for (int i = 0; i < 9; i++) {
    int dx = cloud_pos + tx[i];
    if (dx < 0 || dx == 3 || dx == 7 || dx == 11 || (dx >= 12)) continue;
    int store[20];
    memcpy(store, no_rainny_days, sizeof(int) * 20);
    int next = 0;
    next |= (1 << dx);
    next |= (1 << (dx + 1));
    next |= (1 << (dx + 4));
    next |= (1 << (dx + 5));
    if (stage[day + 1] & next) continue;
    if (day + 1 > total_days) continue;
    rain(next);
    if (!check()) {
      memcpy(no_rainny_days, store, sizeof(int) * 20);
      continue;
    }
    State next_state(no_rainny_days, dx);
    if (visited[day + 1].count(next_state)) continue;
    visited[day + 1].insert(next_state);
    res = max(max(res, day + 1), dfs(day + 1, dx, total_days));
    memcpy(no_rainny_days, store, sizeof(int) * 20);
  }
  return res;
}
int main() {
  int total_days;
  while (~scanf("%d", &total_days)) {
    if (total_days == 0) break;
    for (int i = 0; i < 400; i++) visited[i].clear();
    memset(stage, 0, sizeof(stage));
    memset(no_rainny_days, 0, sizeof(no_rainny_days));
    for (int day = 1; day <= total_days; day++) {
      int bits = 0;
      for (int pos = 0; pos < 16; pos++) {
        int state;
        scanf("%d", &state);
        if (state == 1) {
          bits |= (1 << pos);
        }
      }
      stage[day] = bits;
    }
    int init = 0;
    init |= (1 << 5);
    init |= (1 << (5 + 1));
    init |= (1 << (5 + 4));
    init |= (1 << (5 + 5));
    rain(init);
    printf("%d\n", (init & stage[1])                    ? 0
                   : dfs(1, 5, total_days) < total_days ? 0
                                                        : 1);
  }
}
