#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer", "inline")
#pragma GCC option("arch=native", "tune=native", "no-zero-upper")
#pragma GCC target("avx2")
using namespace std;
template <typename T>
void maxtt(T& t1, T t2) {
  t1 = max(t1, t2);
}
template <typename T>
void mintt(T& t1, T t2) {
  t1 = min(t1, t2);
}
bool debug = 0;
int n, m, k;
string direc = "RDLU";
const long long MOD2 = (long long)998244353 * (long long)998244353;
long long ln, lk, lm;
void etp(bool f = 0) {
  puts(f ? "YES" : "NO");
  exit(0);
}
void addmod(int& x, int y, int mod = 998244353) {
  x += y;
  if (x >= mod) x -= mod;
  if (x < 0) x += mod;
  assert(x >= 0 && x < mod);
}
void et(int x = -1) {
  printf("%d\n", x);
  exit(0);
}
long long fastPow(long long x, long long y, int mod = 998244353) {
  long long ans = 1;
  while (y > 0) {
    if (y & 1) ans = (x * ans) % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return ans;
}
long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; }
bitset<8005> bs[8005];
int in[8005], ot[8005], D;
long long ans;
bool del[8005];
char S[8005];
int gn(char x) {
  if (x >= '0' && x <= '9') return x - '0';
  return (x - 'A') + 10;
}
void rd() {
  scanf("%d", &n);
  for (int(i) = 1; (i) <= (int)(n); (i)++) {
    int st = 1;
    scanf("%s", S);
    for (int(j) = 0; (j) < (int)(n / 4); (j)++) {
      int x = gn(S[j]);
      int ed = st + 3;
      for (int z = ed; z >= st; z--) {
        bs[i][z] = x % 2;
        x /= 2;
      }
      st += 4;
    }
  }
}
void cala() {
  queue<int> q;
  for (int(i) = 1; (i) <= (int)(n); (i)++)
    if (in[i] == 0) q.push(i);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    del[x] = 1;
    for (int(i) = 1; (i) <= (int)(n); (i)++)
      if (bs[x][i]) {
        ans += D + 1;
        in[i]--;
        if (in[i] == 0) q.push(i);
      }
  }
}
int d[8005], inP[8005], inQ[8005];
void calPQ() {
  int x = -1;
  for (int(i) = 1; (i) <= (int)(n); (i)++)
    if (!del[i]) {
      if (x == -1 || in[i] > in[x]) x = i;
    }
  vector<int> P, Q;
  P.push_back(x);
  for (int(i) = 1; (i) <= (int)(n); (i)++)
    if (!del[i] && i != x) {
      if (bs[i][x]) {
        P.push_back(i);
      } else {
        Q.push_back(i);
      }
    }
  for (int(i) = 0; (i) < (int)(P.size()); (i)++)
    for (int j = 1 + i; j < P.size(); j++) {
      if (bs[P[i]][P[j]])
        d[P[i]]++;
      else
        d[P[j]]++;
    }
  for (int(i) = 0; (i) < (int)(Q.size()); (i)++)
    for (int j = i + 1; j < Q.size(); j++) {
      if (bs[Q[i]][Q[j]])
        d[Q[i]]++;
      else
        d[Q[j]]++;
    }
  auto cmp = [&](int a, int b) { return d[a] < d[b]; };
  sort(P.begin(), P.end(), cmp);
  sort(Q.begin(), Q.end(), cmp);
  for (int(i) = 0; (i) < (int)(P.size()); (i)++)
    for (int(j) = 0; (j) < (int)(Q.size()); (j)++) {
      ans += 3;
      if (bs[P[i]][Q[j]])
        inP[Q[j]]++;
      else
        inQ[P[i]]++;
    }
  for (int(i) = 0; (i) < (int)(P.size()); (i)++)
    for (int j = i + 1; j < P.size(); j++) {
      ans += 3 + (inQ[P[i]] == inQ[P[j]]);
    }
  for (int(i) = 0; (i) < (int)(Q.size()); (i)++)
    for (int j = i + 1; j < Q.size(); j++) {
      ans += 3 + (inP[Q[i]] == inP[Q[j]]);
    }
}
void fmain(int tid) {
  rd();
  D = 614 * n;
  for (int(i) = 1; (i) <= (int)(n); (i)++)
    for (int j = i + 1; j <= n; j++) {
      if (bs[i][j])
        in[j]++;
      else
        in[i]++;
    }
  cala();
  calPQ();
  printf("%lld\n", ans);
}
int main() {
  int t = 1;
  for (int(i) = 1; (i) <= (int)(t); (i)++) {
    fmain(i);
  }
  return 0;
}
