#include <bits/stdc++.h>
using namespace std;
using Field = array<int, 16>;
using Event = vector<Field>;
struct State {
  int day;
  int pos;
  Field f;
};
bool operator<(State lhs, State rhs) {
  return make_tuple(lhs.day, lhs.pos, lhs.f) <
         make_tuple(rhs.day, rhs.pos, rhs.f);
}
set<State> s;
int n;
int dfs(State st, const Event &e);
vector<vector<int>> nei = {
    {0, 1, 2, 4, 8},  {0, 1, 2, 5, 9},  {0, 1, 2, 6, 10}, {},
    {0, 4, 8, 5, 6},  {1, 4, 9, 5, 6},  {2, 4, 10, 5, 6}, {},
    {0, 4, 8, 9, 10}, {1, 5, 8, 9, 10}, {2, 6, 8, 9, 10}, {}};
int dfs(State st, const Event &e) {
  if (st.day == n) return true;
  if (s.count(st)) return false;
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 2; j++) {
      if (e[st.day][st.pos + i * 4 + j]) {
        return false;
      }
      st.f[st.pos + i * 4 + j] = -1;
    }
  }
  for (int i = 0; i < 16; i++) {
    if (++st.f[i] >= 7) return false;
  }
  st.day++;
  int prep = st.pos;
  for (int i = 0; i < nei[prep].size(); i++) {
    st.pos = nei[prep][i];
    if (dfs(st, e)) return true;
    st.pos = nei[st.pos][i];
  }
  return false;
}
int solve(Event e) {
  State ini;
  ini.day = 0;
  ini.pos = 5;
  fill(ini.f.begin(), ini.f.end(), 0);
  return dfs(ini, e);
}
int main() {
  while (cin >> n, n) {
    s.clear();
    Event event(n);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 16; j++) cin >> event[i][j];
    }
    cout << solve(event) << endl;
  }
  return 0;
}
