#include <bits/stdc++.h>
using namespace std;
int type[8005], in[8005], inp[8005], inq[8005];
bool a[8005][8005];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= (n >> 2); j++) {
      char ch = getchar();
      while ((!isalpha(ch)) && (!isdigit(ch))) ch = getchar();
      int t;
      if (isdigit(ch))
        t = ch - '0';
      else
        t = 10 + ch - 'A';
      for (int k = 0; k < 4; k++) {
        a[i][4 * j - k] = ((t >> k) & 1);
        if (a[i][4 * j - k]) in[4 * j - k]++;
      }
    }
  int cnt = 0;
  long long ans = 0;
  while (1) {
    int t = 0;
    for (int i = 1; i <= n; i++)
      if ((!in[i]) && (!type[i])) {
        t = i;
        break;
      }
    if (!t) break;
    cnt++;
    ans += 1ll * (614 * n + 1) * (n - cnt);
    type[t] = 3;
    for (int i = 1; i <= n; i++) in[i] -= a[t][i];
  }
  if (cnt == n) {
    printf("%lld", ans);
    return 0;
  }
  int mx = 0;
  for (int i = 1; i <= n; i++)
    if (in[i] > in[mx]) mx = i;
  for (int i = 1; i <= n; i++)
    if (!type[i]) type[i] = ((a[i][mx] || i == mx) ? 1 : 2);
  for (int i = 1; i <= n; i++)
    if (type[i] == 1)
      for (int j = 1; j <= n; j++)
        if (type[j] == 2 && a[j][i]) inq[i]++;
  for (int i = 1; i <= n; i++)
    if (type[i] == 2)
      for (int j = 1; j <= n; j++)
        if (type[j] == 1 && a[j][i]) inp[i]++;
  for (int i = 1; i <= n; i++)
    if (type[i] != 3)
      for (int j = i + 1; j <= n; j++)
        if (type[j] != 3) {
          if (type[i] == 1 && type[j] == 1)
            ans += 3 + (inq[i] == inq[j]);
          else if (type[i] == 2 && type[j] == 2)
            ans += 3 + (inp[i] == inp[j]);
          else
            ans += 3;
        }
  printf("%lld", ans);
  return 0;
}
