#include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T>
inline string toStr(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX / 10;
int dx[] = {1, 0, -1, 0, 2, 0, -2, 0};
int dy[] = {0, 1, 0, -1, 0, 2, 0, -2};
int cx[] = {0, 1, 0, 1};
int cy[] = {0, 0, 1, 1};
struct state {
  int d, x, y, f00, f03, f30, f33;
  state(int d, int x, int y, int f00, int f03, int f30, int f33)
      : d(d), x(x), y(y), f00(f00), f03(f03), f30(f30), f33(f33){};
};
bool isProperXY(int x, int y) { return 0 <= x && x <= 2 && 0 <= y && y <= 2; }
bool isProperF(int f00, int f03, int f30, int f33) {
  return f00 < 7 && f03 < 7 && f30 < 7 && f33 < 7;
}
int conv(int x, int y) { return 3 * x + y; }
int main() {
  int N;
  while (cin >> N, N) {
    vector<vector<vector<int> > > f(N, vector<vector<int> >(4, vector<int>(4)));
    for (int i = (0); i <= ((N)-1); ++i) {
      for (int j = (0); j <= ((4) - 1); ++j) {
        for (int k = (0); k <= ((4) - 1); ++k) {
          cin >> f[i][j][k];
        }
      }
    }
    stack<state> S;
    vector<vector<vector<vector<vector<vector<bool> > > > > > visited(
        N + 1,
        vector<vector<vector<vector<vector<bool> > > > >(
            9,
            vector<vector<vector<vector<bool> > > >(
                7, vector<vector<vector<bool> > >(
                       7, vector<vector<bool> >(7, vector<bool>(7, false))))));
    bool ok = true;
    for (int c = (0); c <= ((4) - 1); ++c) {
      if (f[0][1 + cy[c]][1 + cx[c]]) {
        ok = false;
        break;
      }
    }
    if (ok) {
      S.push(state(1, 1, 1, 1, 1, 1, 1));
      int p = conv(1, 1);
      visited[1][p][1][1][1][1] = true;
    }
    bool finished = false;
    while (!S.empty()) {
      state st = S.top();
      S.pop();
      if (st.d == N) {
        finished = true;
        break;
      }
      int nd = st.d + 1;
      for (int d = (0); d <= ((8) - 1); ++d) {
        int nx = st.x + dx[d], ny = st.y + dy[d];
        if (isProperXY(nx, ny)) {
          ok = true;
          for (int c = (0); c <= ((4) - 1); ++c) {
            if (f[st.d][ny + cy[c]][nx + cx[c]]) {
              ok = false;
              break;
            }
          }
          int nf00 = st.f00 + 1, nf03 = st.f03 + 1, nf30 = st.f30 + 1,
              nf33 = st.f33 + 1;
          if (nx == 0) {
            if (ny == 0) {
              nf00 = 0;
            } else if (ny == 2) {
              nf30 = 0;
            }
          } else if (nx == 2) {
            if (ny == 0) {
              nf03 = 0;
            } else if (ny == 2) {
              nf33 = 0;
            }
          }
          if (ok && isProperF(nf00, nf03, nf30, nf33)) {
            int p = conv(nx, ny);
            if (!visited[nd][p][nf00][nf03][nf30][nf33]) {
              visited[nd][p][nf00][nf03][nf30][nf33] = true;
              S.push(state(nd, nx, ny, nf00, nf03, nf30, nf33));
            }
          }
        }
      }
    }
    cout << (finished ? 1 : 0) << endl;
  }
  return 0;
}
