#include <bits/stdc++.h>
const int max_N = 8049;
int n, p, order[max_N];
bool G[max_N][max_N], del[max_N];
long long ans;
int read() {
  char c = getchar();
  int ans = 0;
  bool neg = false;
  while (!isdigit(c)) neg |= (c == '-'), c = getchar();
  while (isdigit(c)) ans = 10 * ans + c - '0', c = getchar();
  return neg ? -ans : ans;
}
void Write(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x < 10)
    putchar(x + '0');
  else
    Write(x / 10), putchar(x % 10 + '0');
}
int min(int x, int y) { return x < y ? x : y; }
int max(int x, int y) { return x > y ? x : y; }
void _min(int &x, int y) {
  if (x > y) x = y;
}
void _max(int &x, int y) {
  if (x < y) x = y;
}
void read_graph() {
  static char s[max_N];
  for (int i = 1; i <= n; i++) {
    scanf("%s", s);
    for (int j = 0; j < n; j += 4) {
      int mask = (isdigit(s[j / 4]) ? s[j / 4] - '0' : s[j / 4] - 'A' + 10);
      if (mask & 8) G[i][j + 1] = true;
      if (mask & 4) G[i][j + 2] = true;
      if (mask & 2) G[i][j + 3] = true;
      if (mask & 1) G[i][j + 4] = true;
    }
  }
}
bool check(int x) {
  for (int i = 1; i <= n; i++)
    if (i != x && !del[i] && G[i][x]) return false;
  return true;
}
int main() {
  n = read();
  read_graph();
  std::iota(order + 1, order + n + 1, 1);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j < n; j++)
      if (G[order[j + 1]][order[j]]) std::swap(order[j], order[j + 1]);
  for (p = 1; p <= n && check(order[p]); del[order[p]] = true, p++)
    ;
  ans = (614ll * n + 1) * ((n - p + 1) * (p - 1) + (p - 1) * (p - 2) / 2) +
        2 * (n - p + 1) * (n - p);
  for (int i = p; i <= n; i++) {
    int x = -1;
    for (int j = p; j <= n; j++)
      if (i != j && G[order[i]][order[j]] && (x == -1 || G[x][order[j]]))
        x = order[j];
    for (int j = p; j <= n; j++)
      if (i != j && G[order[j]][order[i]] && G[x][order[j]]) ans--;
  }
  return Write(ans), 0;
}
