#include <bits/stdc++.h>
using namespace std;
int vis[400][7][7][7][7][3][3];
int ng[400][3][3];
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
int dx2[] = {0, 1, 0, 1};
int dy2[] = {0, 0, 1, 1};
int n;
void dfs(int d, int lt, int rt, int lb, int rb, int x, int y) {
  if (lt > 6 || rt > 6 || lb > 6 || rb > 6) return;
  if (d == n) {
    throw 1;
    return;
  }
  if (ng[d][x][y] || vis[d][lt][rt][lb][rb][x][y]) return;
  vis[d][lt][rt][lb][rb][x][y] = 1;
  ++lt, ++rt, ++lb, ++rb;
  int pos = y * 3 + x;
  if (pos == 0) lt = 0;
  if (pos == 2) rt = 0;
  if (pos == 6) lb = 0;
  if (pos == 8) rb = 0;
  for (int k = (1); k < (int)(3); ++k)
    for (int dir = (0); dir < (int)(4); ++dir) {
      int nx = x + dx[dir] * k;
      int ny = y + dy[dir] * k;
      if (nx < 0 || nx >= 3 || ny < 0 || ny >= 3) continue;
      dfs(d + 1, lt, rt, lb, rb, nx, ny);
    }
}
int main() {
  while (cin >> n, n) {
    memset(vis, 0, sizeof(vis));
    memset(ng, 0, sizeof(ng));
    for (int d = (0); d < (int)(n); ++d)
      for (int y = (0); y < (int)(4); ++y)
        for (int x = (0); x < (int)(4); ++x) {
          int a;
          cin >> a;
          if (a) {
            int xx = x - 1, yy = y - 1;
            for (int dir = (0); dir < (int)(4); ++dir) {
              int nx = xx + dx2[dir], ny = yy + dy2[dir];
              if (nx < 0 || nx >= 3 || ny < 0 || ny >= 3) continue;
              ng[d][nx][ny] = 1;
            }
          }
        }
    try {
      dfs(0, 0, 0, 0, 0, 1, 1);
      cout << 0 << endl;
    } catch (int x) {
      cout << 1 << endl;
    }
  }
  return 0;
}
