#include <bits/stdc++.h>
int schedule[365];
int number_of_days;
int map[3][3][365];
int dfs(int x, int y, int day, int* moisture);
int main(void) {
  char buf[32];
  int dryness[16];
  while (scanf("%d%*c", &number_of_days), number_of_days) {
    int i, j;
    for (i = 0; i < number_of_days; i++) {
      fread(buf, sizeof(char), 32, stdin);
      for (j = 15; ~j; j--) {
        if (buf[j * 2] == '1') {
          schedule[i]++;
        }
        if (j) schedule[i] <<= 1;
      }
    }
    int x, y;
    for (i = 0; i < number_of_days; i++) {
      for (x = 0; x < 3; x++) {
        for (y = 0; y < 3; y++) {
          if (((0b110011) << (x + 4 * y)) & schedule[i]) {
            map[x][y][i] = 0;
          } else {
            map[x][y][i] = 1;
          }
        }
      }
    }
    for (i = 0; i < 16; i++) {
      dryness[i] = 0;
    }
    if (map[1][1][0] == 0) {
      printf("0\n");
      continue;
    }
    if (~dfs(1, 1, 0, dryness)) {
      printf("1\n");
    } else {
      printf("0\n");
    }
  }
  return 0;
}
int dfs(int x, int y, int day, int* dryness) {
  int drystats[16];
  int i;
  for (i = 0; i < 16; i++) drystats[i] = dryness[i] + 1;
  drystats[x + 4 * y] = 0;
  drystats[x + 4 * y + 1] = 0;
  drystats[x + 4 * y + 4] = 0;
  drystats[x + 4 * y + 5] = 0;
  for (i = 0; i < 16; i++) {
    if (drystats[i] > 6) return -1;
  }
  if (day + 1 == number_of_days) return 0;
  for (i = -2; i < 3; i++) {
    if (x + i >= 0 && x + i < 3) {
      if (map[x + i][y][day + 1]) {
        if (~dfs(x + i, y, day + 1, drystats)) {
          return 0;
        }
      }
    }
    if (y + i >= 0 && y + i < 3) {
      if (map[x][y + i][day + 1]) {
        if (~dfs(x, y + i, day + 1, drystats)) {
          return 0;
        }
      }
    }
  }
  return -1;
}
