#include <bits/stdc++.h>
using namespace std;
int n;
int f[4][4];
bool dp[400][3][3][7][7][7][7];
void solve() {
  for (int i = 0; i < 400; i++)
    for (int j1 = 0; j1 < 3; j1++)
      for (int j2 = 0; j2 < 3; j2++)
        for (int k1 = 0; k1 < 7; k1++)
          for (int k2 = 0; k2 < 7; k2++)
            for (int k3 = 0; k3 < 7; k3++)
              for (int k4 = 0; k4 < 7; k4++)
                dp[i][j1][j2][k1][k2][k3][k4] = false;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 4; j++)
      for (int k = 0; k < 4; k++) cin >> f[j][k];
    if (i == 0) {
      if (f[1][1] + f[1][2] + f[2][1] + f[2][2] > 0) {
        puts("0");
        return;
      }
      for (int j = 0; j < 4; j++)
        for (int k = 0; k < 4; k++) {
          if (j == 1 && k == 1) continue;
          if (j == 1 && k == 2) continue;
          if (j == 2 && k == 1) continue;
          if (j == 2 && k == 2) continue;
          dp[0][1][1][1][1][1][1] = 1;
        }
      continue;
    }
    for (int j = 0; j < 3; j++)
      for (int k = 0; k < 3; k++) {
        if (f[j][k] + f[j + 1][k] + f[j][k + 1] + f[j + 1][k + 1] > 0) continue;
        for (int j1 = 0; j1 < 3; j1++)
          for (int k1 = 0; k1 < 3; k1++) {
            if (abs(j1 - j) + abs(k1 - k) > 2) continue;
            for (int l1 = 0; l1 < 7; l1++)
              for (int l2 = 0; l2 < 7; l2++)
                for (int l3 = 0; l3 < 7; l3++)
                  for (int l4 = 0; l4 < 7; l4++) {
                    if (j == 0 && k == 0) {
                      if (l2 == 0 || l3 == 0 || l4 == 0) continue;
                      for (int x = 0; x < 7; x++)
                        dp[i][j][k][0][l2][l3][l4] |=
                            dp[i - 1][j1][k1][x][l2 - 1][l3 - 1][l4 - 1];
                    } else if (j == 0 && k == 2) {
                      if (l1 == 0 || l3 == 0 || l4 == 0) continue;
                      for (int x = 0; x < 7; x++)
                        dp[i][j][k][0][l2][l3][l4] |=
                            dp[i - 1][j1][k1][l1 - 1][x][l3 - 1][l4 - 1];
                    } else if (j == 2 && k == 0) {
                      if (l1 == 0 || l2 == 0 || l4 == 0) continue;
                      for (int x = 0; x < 7; x++)
                        dp[i][j][k][0][l2][l3][l4] |=
                            dp[i - 1][j1][k1][l1 - 1][l2 - 1][x][l4 - 1];
                    } else if (j == 2 && k == 2) {
                      if (l1 == 0 || l2 == 0 || l3 == 0) continue;
                      for (int x = 0; x < 7; x++)
                        dp[i][j][k][0][l2][l3][l4] |=
                            dp[i - 1][j1][k1][l1 - 1][l2 - 1][l3 - 1][x];
                    } else {
                      if (l1 == 0 || l2 == 0 || l3 == 0 || l4 == 0) continue;
                      dp[i][j][k][l1][l2][l3][l4] |=
                          dp[i - 1][j1][k1][l1 - 1][l2 - 1][l3 - 1][l4 - 1];
                    }
                  }
          }
      }
    bool ok = false;
    for (int j = 0; j < 3; j++)
      for (int k = 0; k < 3; k++)
        for (int l1 = 0; l1 < 7; l1++)
          for (int l2 = 0; l2 < 7; l2++)
            for (int l3 = 0; l3 < 7; l3++)
              for (int l4 = 0; l4 < 7; l4++)
                if (dp[i][j][k][l1][l2][l3][l4]) ok = true;
    if (!ok) {
      puts("0");
      return;
    }
  }
  puts("1");
}
int main() {
  while (cin >> n) {
    if (n == 0) break;
    solve();
  }
}
