#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
int n;
int memo[366][3][3][7][7][7][7];
int fes[366][4][4];
vi dx = {1, 0, -1, 0};
vi dy = {0, -1, 0, 1};
bool rec(int day, int y, int x, int a, int b, int c, int d) {
  if (a == 7 or b == 7 or c == 7 or d == 7) return false;
  if (day == n) return true;
  for (int(i) = 0; (i) < (2); (i)++) {
    for (int(j) = 0; (j) < (2); (j)++) {
      if (fes[day][y + i][x + j]) {
        return false;
      }
    }
  }
  if (memo[day][y][x][a][b][c][d] != -1) return memo[day][y][x][a][b][c][d];
  int ret = 0;
  for (int(i) = 0; (i) < (4); (i)++) {
    for (int(j) = (1); (j) < (3); (j)++) {
      int ny = y + dy[i] * j;
      int nx = x + dx[i] * j;
      if (nx < 0 or 2 < nx or ny < 0 or 2 < ny) continue;
      int na = a, nb = b, nc = c, nd = d;
      if (ny != 0 or nx != 0)
        na++;
      else
        na = 0;
      if (ny != 0 or nx != 2)
        nb++;
      else
        nb = 0;
      if (ny != 2 or nx != 0)
        nc++;
      else
        nc = 0;
      if (ny != 2 or nx != 2)
        nd++;
      else
        nd = 0;
      ret |= rec(day + 1, ny, nx, na, nb, nc, nd);
    }
  }
  return memo[day][y][x][a][b][c][d] = ret;
}
int main(void) {
  for (; cin >> n, n;) {
    for (int(i) = 0; (i) < (n); (i)++) {
      for (int(y) = 0; (y) < (4); (y)++) {
        for (int(x) = 0; (x) < (4); (x)++) {
          cin >> fes[i][y][x];
        }
      }
    }
    for (int(i1) = 0; (i1) < (n); (i1)++)
      for (int(i2) = 0; (i2) < (3); (i2)++)
        for (int(i3) = 0; (i3) < (3); (i3)++)
          for (int(i4) = 0; (i4) < (7); (i4)++)
            for (int(i5) = 0; (i5) < (7); (i5)++)
              for (int(i6) = 0; (i6) < (7); (i6)++)
                for (int(i7) = 0; (i7) < (7); (i7)++)
                  memo[i1][i2][i3][i4][i5][i6][i7] = -1;
    cout << rec(0, 1, 1, 0, 0, 0, 0) << endl;
  }
  return 0;
}
