#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  vector<vector<bool>> adj(N, vector<bool>(N));
  {
    for (int i = 0; i < N; i++) {
      string s;
      cin >> 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)));
        }
      }
    }
  }
  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;
  reverse(inds.begin(), inds.end());
  while (N && outDeg[inds[N - 1]] == N - 1) {
    ans += int64_t(UNREACHABLE + 1) * (N - 1);
    N--;
    inds.pop_back();
  }
  reverse(inds.begin(), inds.end());
  for (int i = 0; i < N; i++) {
    ans += 3 * (N - 1) - 2 * outDeg[inds[i]] -
           outDeg[inds[(i + outDeg[inds[i]]) % N]];
  }
  cout << ans << '\n';
  return 0;
}
