#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int first = 0, f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar()) {
    if (ch == '-') f = -1;
  }
  for (; isdigit(ch); ch = getchar()) {
    first = first * 10 + ch - 48;
  }
  return first * f;
}
const int mxN = 8000;
int ind[mxN + 3];
vector<int> s1, s2;
char a[mxN + 3][mxN + 3];
queue<int> que;
int n;
long long w, ans;
char decode(char first) { return first >= 65 ? first - 55 : first - 48; }
bool cmp(int first, int second) { return a[first][second]; }
int main() {
  scanf("%d", &n);
  w = 614ll * n;
  for (int i = 1; i <= n; i++) {
    char ch = getchar();
    for (int j = 4; j <= n; j += 4) {
      ch = decode(getchar());
      a[i][j - 3] = (ch & 8) >> 3, a[i][j - 2] = (ch & 4) >> 2,
               a[i][j - 1] = (ch & 2) >> 1, a[i][j] = ch & 1;
    }
  }
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++) {
      if (a[i][j]) {
        ind[j]++;
      } else {
        ind[i]++;
      }
    }
  for (int i = 1; i <= n; i++)
    if (ind[i] == 0) {
      que.push(i);
    }
  int cur = n;
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    cur--;
    ans += (w + 1ll) * cur;
    for (int v = 1; v <= n; v++)
      if (a[u][v] && v != u) {
        ind[v]--;
        if (ind[v] == 0) {
          que.push(v);
        }
      }
  }
  if (cur == 0) {
    printf("%I64d\n", ans);
    return 0;
  }
  int u = 0;
  for (int i = 1; i <= n; i++)
    if (u == 0 || ind[i] > ind[u]) {
      u = i;
    }
  for (int i = 1; i <= n; i++)
    if (ind[i]) {
      if (u == i || a[i][u]) {
        s1.push_back(i);
      } else {
        s2.push_back(i);
      }
    }
  sort(s1.begin(), s1.end(), cmp);
  sort(s2.begin(), s2.end(), cmp);
  ans += 3ll * s1.size() * s2.size() + s1.size() * (s1.size() - 1ll) / 2ll +
         s2.size() * (s2.size() - 1ll) / 2ll;
  for (int i = 0; i < s1.size(); i++) {
    ind[s1[i]] -= i;
  }
  for (int i = 0; i < s1.size(); i++)
    for (int j = 0; j < i; j++) {
      ans += ind[s1[i]] == ind[s1[j]] ? 3ll : 2ll;
    }
  for (int i = 0; i < s2.size(); i++) {
    ind[s2[i]] -= i;
  }
  for (int i = 0; i < s2.size(); i++)
    for (int j = 0; j < i; j++) {
      ans += ind[s2[i]] == ind[s2[j]] ? 3ll : 2ll;
    }
  printf("%I64d\n", ans);
  return 0;
}
