#include <bits/stdc++.h>
using namespace std;
int read() {
  int x = 0;
  bool flg = false;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') flg = true;
  for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ 48);
  return flg ? -x : x;
}
const int INF = 1e9;
int n;
bool mp[8010][8010];
long long work(int *p, int m) {
  if (m == 1) return 0;
  long long W = 0;
  const int o = p[1];
  static bool mrk[8010], ok[8010];
  for (int i(1); i <= int(m); i++) ok[p[i]] = 1;
  for (int i(1); i <= int(m); i++)
    if (mp[p[i]][o]) mrk[p[i]] = 1;
  static int d[8010], c[8010];
  for (int i(1); i <= int(n); i++)
    if (ok[i])
      for (int j(1); j <= int(n); j++)
        if (ok[j])
          if (mp[i][j] && !(!mrk[i] && mrk[j])) ++d[j];
  static int que[8010], hd, tl;
  for (int i(1); i <= int(n); i++)
    if (ok[i] && !d[i]) que[++tl] = i;
  while (hd < tl) {
    const int u = que[++hd];
    for (int v(1); v <= int(n); v++)
      if (ok[v] && mp[u][v] && !--d[v]) que[++tl] = v;
  }
  assert(tl == m);
  static int id[8010];
  for (int i(1); i <= int(m); i++) id[que[i]] = i;
  const int M = id[o];
  assert(~M);
  assert(M > 1);
  assert(M < m);
  for (int i(1); i <= int(M); i++) {
    const int x = que[i];
    for (int j(1); j <= int(m); j++) c[j] = d[j] = INF;
    d[i] = 0;
    int y = -1;
    for (int j(M + 1); j <= int(m); j++)
      if (mp[x][que[j]]) {
        y = que[j];
        d[j] = min(d[j], 1);
      }
    if (~y) {
      for (int j(1); j <= int(M); j++)
        if (mp[y][que[j]]) d[j] = min(d[j], 2);
    }
    y = que[m];
    for (int j(1); j <= int(M); j++)
      if (mp[y][que[j]]) d[j] = min(d[j], 3);
    for (int j(M + 1); j <= int(m); j++)
      if (mp[o][que[j]]) d[j] = min(d[j], 2);
    for (int j(1); j <= int(m); j++) {
      if (j ^ M) c[j + 1] = min(c[j + 1], min(c[j], d[j] + 1));
      W += min(c[j], d[j]);
    }
  }
  memset(mrk, 0, sizeof mrk);
  for (int i(M + 1); i <= int(m); i++) {
    const int x = que[i];
    for (int j(1); j <= int(m); j++) c[j] = d[j] = INF;
    d[i] = 0;
    for (int j(1); j <= int(M); j++)
      if (mp[x][que[j]] && !mrk[j]) {
        mrk[j] = 1;
        const int y = que[j];
        for (int k(M + 1); k <= int(m); k++) mrk[k] = mrk[k] || mp[y][que[k]];
      }
    for (int j(M + 1); j <= int(m); j++)
      if (mrk[j])
        d[j] = min(d[j], 2);
      else
        d[j] = min(d[j], 3);
    for (int j(1); j <= int(M); j++)
      if (mp[x][que[j]]) d[j] = min(d[j], 1);
    const int y = que[m];
    for (int j(1); j <= int(M); j++)
      if (mp[y][que[j]]) d[j] = min(d[j], 2);
    for (int j(1); j <= int(m); j++) {
      if (j ^ M) c[j + 1] = min(c[j + 1], min(c[j], d[j] + 1));
      W += min(c[j], d[j]);
    }
  }
  return W;
}
void solve() {
  n = read();
  for (int i(1); i <= int(n); i++) {
    static char s[2010];
    scanf("%s", s + 1);
    for (int j(1); j <= int(n >> 2); j++) {
      const int t = isdigit(s[j]) ? s[j] - '0' : 10 + s[j] - 'A';
      mp[i][(j << 2) - 3] = t >> 3 & 1;
      mp[i][(j << 2) - 2] = t >> 2 & 1;
      mp[i][(j << 2) - 1] = t >> 1 & 1;
      mp[i][(j << 2)] = t & 1;
    }
  }
  static int p[8010], d[8010];
  for (int i(1); i <= int(n); i++)
    for (int j(1); j <= int(n); j++) d[i] += mp[i][j];
  for (int i(1); i <= int(n); i++) p[i] = i;
  sort(p + 1, p + n + 1,
       [&](const int &x, const int &y) { return d[x] < d[y]; });
  int q = -1;
  int S = 0;
  for (int i(1); i <= int(n); i++) {
    S += d[p[i]];
    if (S == i * (i - 1) / 2) {
      q = i;
      break;
    }
  }
  long long ans = 0;
  for (int i(q + 1); i <= int(n); i++) {
    ans += i - 1 + 614LL * n * (n - i);
  }
  ans += 614LL * n * q * (n - q);
  ans += work(p, q);
  printf("%lld\n", ans);
}
int main() {
  for (int T = 1; T--;) solve();
  return 0;
}
