#include <bits/stdc++.h>
using namespace std;
const int N = 8e3 + 5;
bitset<N> ed[N], mark;
vector<int> A, B;
char c[N / 4];
long long ans;
int n, p[N];
bool cmp(int u, int v) { return ed[u][v]; }
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s", c);
    for (int j = 0; j < n / 4; j++)
      for (int x = c[j] < 'A' ? c[j] - '0' : 10 + c[j] - 'A', k = 0; k < 4; k++)
        ed[i].set(4 * j + 3 - k, x >> k & 1);
  }
  int root = -1, pr;
  memset(p, -1, sizeof p);
  for (int u = 0; u < n; u++) p[ed[u].count()] = u;
  for (int i = n - 1; ~i && ~p[i]; i--)
    ans += 614LL * n * i + i, mark[p[i]] = true;
  for (int u = 0; u < n; u++)
    if (!mark[u] && (!~root || ed[u].count() < ed[root].count())) root = pr = u;
  for (int u = 0; u < n; u++)
    if (!mark[u] && u ^ root)
      if (ed[u][root])
        if (ed[u][pr])
          A.push_back(pr), pr = u;
        else
          A.push_back(u);
      else
        B.push_back(u);
  sort(A.begin(), A.end(), cmp);
  sort(B.begin(), B.end(), cmp), B.push_back(pr);
  for (int t = 0, nxt = -1; t < 2; t++, nxt = -1, swap(A, B))
    for (auto u : A) {
      while (nxt + 1 < B.size() && ed[u][B[nxt + 1]]) nxt++;
      for (auto v : A)
        if (u ^ v)
          ed[u][v] ? ans++ : ~nxt&& ed[B[nxt]][v] ? ans += 2 : ans += 3;
      for (auto v : B)
        ed[u][v] ? ans++ : ~nxt || ed[A.back()][v] ? ans += 2 : ans += 3;
    }
  printf("%lld", ans);
}
