#include <bits/stdc++.h>
using namespace std;
int n, m, deg[8012], ans1, ans2, ans3, cn = 0, mx[8012], X[8012];
bool w[8012][8012], vis[8012];
queue<int> q;
long long ans = 0;
int main() {
  scanf("%d", &n);
  m = n >> 2;
  int i, j, k, v, x, y, o;
  char ch;
  for (i = 0, getchar(); i < n; ++i, getchar())
    for (x = j = 0; j < m; ++j) {
      ch = getchar();
      v = (ch <= '9') ? (ch - '0') : (10 + ch - 'A');
      for (k = 3; ~k; --k, ++x)
        if ((v >> k) & 1) {
          w[i][x] = 1;
          ++deg[x];
        }
    }
  for (i = 0; i < n; i++)
    if (!deg[i]) q.push(i);
  while (!q.empty()) {
    vis[x = q.front()] = 1;
    q.pop();
    ++cn;
    bool *p = w[x];
    for (y = 0; y < n; y++)
      if (p[y]) {
        p[y] = 0;
        if (!(--deg[y])) q.push(y);
      }
  }
  ans = 1ll * cn * (cn - 1) / 2 * (1 + 614 * n) +
        1ll * cn * (n - cn) * (1 + 614 * n);
  ans1 = (n - cn) * (n - cn - 1) / 2;
  for (x = 0; x < n; ++x) X[x] = x;
  for (x = 0; x < n; ++x)
    if (!vis[x]) {
      bool *p = w[x];
      v = deg[x];
      for (y = 0; y < n; ++y)
        if (p[y])
          mx[y] = max(mx[y], v - deg[y] + 1), o = X[y], X[y] = p[o] ? x : o;
    }
  for (x = 0; x < n; ++x) ans2 += max(mx[x], deg[X[x]]);
  ans3 = (n - cn) * (n - cn - 1) - ans1 - ans2;
  ans += ans1 + ans2 * 2 + ans3 * 3;
  printf("%lld", ans);
  return 0;
}
