#include <bits/stdc++.h>
using namespace std;
auto c2 = [](int64_t a) -> int64_t {
  assert(a >= 0);
  return a * (a - 1) / 2;
};
int main() {
  int N;
  scanf("%d", &N);
  vector<vector<bool>> adj(N, vector<bool>(N));
  {
    char* s = new char[N / 4 + 10];
    for (int i = 0; i < N; i++) {
      scanf("%s", s);
      for (int j = 0; j < N / 4; j++) {
        char c = s[j];
        assert(('0' <= c && c <= '9') || ('A' <= c && c <= 'F'));
        int v = ('0' <= c && c <= '9') ? (c - '0') : (10 + (c - 'A'));
        assert(0 <= v && v < 16);
        for (int z = 0; z < 4; z++) {
          adj[i][4 * j + z] = bool(v & (1 << (3 - z)));
        }
      }
    }
    delete[] s;
  }
  const int UNREACHABLE = 614 * N;
  vector<int> outDeg(N);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      outDeg[i] += adj[i][j];
    }
  }
  int pivot = int(min_element(outDeg.begin(), outDeg.end()) - outDeg.begin());
  vector<int> inds(N);
  iota(inds.begin(), inds.end(), 0);
  sort(inds.begin(), inds.end(), [&](int a, int b) -> bool {
    if (adj[pivot][a] != adj[pivot][b]) return adj[pivot][b];
    return adj[a][b];
  });
  int64_t ans = 0;
  int st = 0;
  while (st < N && outDeg[inds[st]] == N - 1 - st) {
    ans += int64_t(UNREACHABLE + 1) * (N - 1 - st);
    st++;
  }
  int M = N - st;
  for (int i = st; i < N; i++) {
    ans += 3 * (N - 1 - st) - 2 * outDeg[inds[i]] -
           outDeg[inds[st +
                       (i - st + M - 1 - (N - 1 - st - outDeg[inds[i]])) % M]];
  }
  printf("%" PRId64 "\n", ans);
  return 0;
}
