#include <bits/stdc++.h>
using namespace std;
const int N = 16;
const int L = 6;
const int MAX_A = 370;
const int MAX_B = 9;
const int MAX_C = 7778;
int dp[MAX_A][MAX_B][MAX_C], D;
int m[9][5] = {{0, 1, 2, 3, 6}, {1, 3, 4, 7, 0}, {2, 5, 8, 0, 1},
               {3, 0, 4, 5, 6}, {4, 1, 5, 3, 7}, {5, 2, 8, 3, 4},
               {6, 0, 3, 7, 8}, {7, 1, 4, 8, 6}, {8, 2, 5, 6, 7}};
int d[16][4] = {
    {0},          {0, 1}, {1, 2}, {2},          {0, 3},       {0, 1, 3, 4},
    {1, 2, 4, 5}, {2, 5}, {3, 6}, {3, 4, 6, 7}, {4, 5, 7, 8}, {5},
    {6},          {6, 7}, {7, 8}, {8},
};
int l[16]{1, 2, 2, 1, 2, 4, 3, 2, 2, 4, 4, 1, 1, 2, 2, 1};
int check(int a, int b, int c, int n, vector<vector<int> > &v) {
  int _h = c, tmp = 1000;
  while (_h) {
    if (tmp == 1000 && _h % 10 == 6 && n != 8) return -1;
    if (tmp == 100 && _h % 10 == 6 && n != 6) return -1;
    if (tmp == 10 && _h % 10 == 6 && n != 2) return -1;
    if (tmp == 1 && _h % 10 == 6 && n != 0) return -1;
    _h /= 10;
    tmp /= 10;
  }
  for (int i = 0; i < (int)(v[a].size()); ++i) {
    if (!v[a][i]) continue;
    bool f = true;
    for (int j = 0; j < (int)(l[i]); ++j)
      if (d[i][j] == n) f = false;
    if (!f) return -1;
  }
  if (n != 0) c += 1000;
  if (n != 2) c += 100;
  if (n != 6) c += 10;
  if (n != 8) c += 1;
  if (n == 0) c %= 1000;
  if (n == 2) c = c - (c % 1000) + (c % 100);
  if (n == 6) c = c - (c % 100) + (c % 10);
  if (n == 8) c -= c % 10;
  return c;
}
int solve() {
  vector<vector<int> > v(D, vector<int>(N));
  for (int i = 0; i < (int)(D); ++i)
    for (int j = 0; j < (int)(N); ++j) cin >> v[i][j];
  memset(dp, 0, sizeof(dp));
  if (v[0][5] || v[0][6] || v[0][9] || v[0][10]) return 0;
  dp[1][4][1111] = 1;
  for (int a = (int)(1); a < (int)(D); ++a) {
    for (int b = 0; b < (int)(MAX_B); ++b) {
      for (int c = 0; c < (int)(MAX_C); ++c) {
        if (!dp[a][b][c]) continue;
        for (int i = 0; i < (int)(5); ++i) {
          int nb = m[b][i], nc;
          if ((nc = check(a, b, c, nb, v)) == -1) continue;
          dp[a + 1][nb][nc] = true;
        }
      }
    }
  }
  bool ans = false;
  for (int b = 0; b < (int)(MAX_B); ++b)
    for (int c = 0; c < (int)(MAX_C); ++c)
      if (dp[D][b][c]) ans = true;
  return (ans ? 1 : 0);
}
int main() {
  while (cin >> D && D) cout << solve() << endl;
  return 0;
}
