#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 toStr(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = INT_MAX / 10;
int dx[] = {1, 0, -1, 0, 2, 0, -2, 0, 0};
int dy[] = {0, 1, 0, -1, 0, 2, 0, -2, 0};
int dx2[] = {0, 1, 0, 1};
int dy2[] = {0, 0, 1, 1};
int cp[] = {0, 3, 12, 15};
string getField(string s) { return s.substr(0, 16); }
int getX(string s) { return s[16] - '0'; }
int getY(string s) { return s[17] - '0'; }
int getP(int x, int y) { return 4 * y + x; }
int getDay(string s) { return toInt(s.substr(18)); }
string pack(string field, int x, int y, int day) {
  return field + toStr(x) + toStr(y) + toStr(day);
}
string updateField(string field, int x, int y) {
  for (int i = (0); i <= ((16) - 1); ++i) {
    field[i]++;
  }
  for (int i = (0); i <= ((4) - 1); ++i) {
    field[getP(x + dx2[i], y + dy2[i])] = '0';
  }
  return field;
}
bool isProperXY(int x, int y) {
  if (0 <= x && x <= 2 && 0 <= y && y <= 2) {
    return true;
  } else {
    return false;
  }
}
bool isProperRain(string field, int x, int y) {
  for (int i = (0); i <= ((4) - 1); ++i) {
    if ('6' < field[cp[i]]) {
      return false;
    }
  }
  return true;
}
bool isProperFestival(int x, int y, string festival) {
  for (int i = (0); i <= ((4) - 1); ++i) {
    if (festival[getP(x + dx2[i], y + dy2[i])] == '1') {
      return false;
    }
  }
  return true;
}
bool isProperField(string field, int x, int y, string festival) {
  return isProperRain(field, x, y) && isProperFestival(x, y, festival);
}
int main() {
  int N;
  while (cin >> N, N) {
    vector<string> festival(N);
    char c;
    for (int i = (0); i <= ((N)-1); ++i) {
      for (int j = (0); j <= ((16) - 1); ++j) {
        cin >> c;
        festival[i].push_back(c);
      }
    }
    int ans = 0;
    stack<string> S;
    string ofield = updateField(string(16, '0'), 1, 1);
    if (isProperField(ofield, 1, 1, festival[0])) {
      S.push(pack(ofield, 1, 1, 1));
    }
    while (!S.empty()) {
      string s = S.top();
      S.pop();
      string field = getField(s);
      int x = getX(s), y = getY(s), day = getDay(s);
      if (day == N) {
        ans++;
        break;
      }
      for (int i = (0); i <= ((9) - 1); ++i) {
        int nx = x + dx[i], ny = y + dy[i];
        if (isProperXY(nx, ny)) {
          string nfield = updateField(field, nx, ny);
          if (isProperField(nfield, nx, ny, festival[day])) {
            S.push(pack(nfield, nx, ny, day + 1));
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
