#include <bits/stdc++.h>
using namespace std;
inline int rd() {
  int x = 0;
  char c = getchar();
  while (!isdigit(c)) c = getchar();
  while (isdigit(c)) x = x * 10 + c - 48, c = getchar();
  return x;
}
const int N = 8005;
int n, nn, ql, qr, e[N][N], d[N], q[N], o[N], c[N];
char s[N];
long long ans;
int main() {
  n = rd();
  nn = n;
  for (int i = (1); i <= (n); ++i) {
    scanf("%s", s + 1);
    for (int j = (1); j <= (n / 4); ++j) {
      int x = isdigit(s[j]) ? s[j] - '0' : 10 + s[j] - 'A';
      for (int k = (0); k < (4); ++k) e[i][4 * j - k] = x >> k & 1;
    }
  }
  for (int i = (1); i <= (n); ++i)
    for (int j = (1); j <= (n); ++j)
      if (e[i][j]) d[j]++;
  for (int i = (1); i <= (n); ++i)
    if (!d[i]) q[++qr] = i;
  while (ql < qr) {
    int x = q[++ql];
    o[x] = 3;
    nn--;
    ans += (614ll * n + 1) * nn;
    for (int i = (1); i <= (n); ++i)
      if (e[x][i] && !--d[i]) q[++qr] = i;
  }
  if (!nn) {
    printf("%lld\n", ans);
    return 0;
  }
  int x = 0;
  for (int i = (1); i <= (n); ++i)
    if (d[i] > d[x]) x = i;
  for (int i = (1); i <= (n); ++i)
    if (o[i] != 3) o[i] = e[i][x] || i == x ? 1 : 2;
  for (int i = (1); i <= (n); ++i)
    if (o[i] != 3)
      for (int j = (1); j <= (n); ++j)
        if (o[j] != 3 && o[i] != o[j] && e[i][j]) c[j]++;
  for (int i = (1); i <= (n); ++i)
    if (o[i] != 3)
      for (int j = (i + 1); j <= (n); ++j)
        if (o[j] != 3) ans += 3 + (o[i] == o[j] && c[i] == c[j]);
  printf("%lld\n", ans);
  return 0;
}
