#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 toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
const double EPS = 1e-9;
const double PI = acos(-1.0);
bool satisfied(vector<vector<int> > &field, int y, int x) {
  for (int i = (y); i < (y + 2); ++i) {
    for (int j = (x); j < (x + 2); ++j) {
      if (field[i][j]) {
        return false;
      }
    }
  }
  return true;
}
int h(int y, int x) { return y * 3 + x; }
bool thirsty(vector<int> pos) {
  vector<vector<int> > wetness(4, vector<int>(4));
  for (int i = (0); i < (pos.size()); ++i) {
    int y = pos[i] / 3;
    int x = pos[i] % 3;
    for (int j = (y); j < (y + 2); ++j) {
      for (int k = (x); k < (x + 2); ++k) {
        wetness[j][k]++;
      }
    }
  }
  for (int i = (0); i < (4); ++i) {
    for (int j = (0); j < (4); ++j) {
      if (wetness[i][j] == 0) {
        return true;
      }
    }
  }
  return false;
}
int dp[365][9][9][9][9][9][9];
int main() {
  int N;
  while (cin >> N, N) {
    for (int day = (0); day < (N); ++day)
      for (int i = (0); i < (9); ++i)
        for (int j = (0); j < (9); ++j)
          for (int k = (0); k < (9); ++k)
            for (int l = (0); l < (9); ++l)
              for (int m = (0); m < (9); ++m)
                for (int n = (0); n < (9); ++n) dp[day][i][j][k][l][m][n] = 0;
    for (int day = (0); day < (N); ++day) {
      vector<vector<int> > field(4, vector<int>(4));
      for (int i = (0); i < (4); ++i) {
        for (int j = (0); j < (4); ++j) {
          cin >> field[i][j];
        }
      }
      if (day == 0) {
        dp[day][0][0][0][0][0][h(1, 1)] = satisfied(field, 1, 1);
      } else {
        for (int i = (0); i < (9); ++i) {
          for (int j = (0); j < (9); ++j) {
            for (int k = (0); k < (9); ++k) {
              for (int l = (0); l < (9); ++l) {
                for (int m = (0); m < (9); ++m) {
                  for (int n = (0); n < (9); ++n) {
                    int py = n / 3, px = n % 3;
                    for (int o = (0); o < (9); ++o) {
                      int cy = o / 3, cx = o % 3;
                      if (cy == py || cx == px) {
                        if (dp[day - 1][i][j][k][l][m][n]) {
                          if (day >= 6) {
                            vector<int> pos;
                            pos.push_back(i);
                            pos.push_back(j);
                            pos.push_back(k);
                            pos.push_back(l);
                            pos.push_back(m);
                            pos.push_back(n);
                            pos.push_back(o);
                            if (thirsty(pos)) continue;
                          }
                          dp[day][j][k][l][m][n][o] |= satisfied(field, cy, cx);
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    bool ok = false;
    for (int i = (0); i < (9); ++i) {
      for (int j = (0); j < (9); ++j) {
        for (int k = (0); k < (9); ++k) {
          for (int l = (0); l < (9); ++l) {
            for (int m = (0); m < (9); ++m) {
              for (int n = (0); n < (9); ++n) {
                ok |= dp[N - 1][i][j][k][l][m][n];
              }
            }
          }
        }
      }
    }
    cout << ok << endl;
  }
}
