#include <bits/stdc++.h>
using namespace std;
template <typename T>
void debug_out(T t) {
  cerr << t;
}
template <typename A, typename B>
void debug_out(pair<A, B> u) {
  cerr << "(" << u.first << " " << u.second << ")";
}
template <typename T>
void debug_out(vector<T> t) {
  int sz = t.size();
  for (int i = 0; i < sz; i++) {
    debug_out(t[i]);
    if (i != sz - 1) cerr << ", ";
  }
}
template <typename T>
void debug_out(vector<vector<T>> t) {
  int sz = t.size();
  for (int i = 0; i < sz; i++) {
    debug_out(t[i]);
    if (i != sz - 1) cerr << endl;
  }
}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  debug_out(H);
  cerr << " ";
  debug_out(T...);
}
template <typename T>
void debug_out(set<T> a) {
  vector<T> _a;
  for (T t : a) _a.push_back(t);
  debug_out(_a);
}
int n;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
vector<string> g;
vector<int> deg;
vector<int> was;
long long res;
long long pos;
int edge(int x, int y) {
  char c = g[x][y / 4];
  int to = (c >= '0' && c <= '9' ? (c - '0') : (c - 'A' + 10));
  return ((to >> ((((3 - y) % 4) + 4) % 4)) & 1);
}
void sort(vector<int> &id, int l, int r) {
  if (l == r) return;
  int cur = r;
  for (int i = r; i > l; i--) {
    if (edge(id[l], id[i])) {
      swap(id[i], id[cur]);
      cur--;
    }
  }
  swap(id[l], id[cur]);
  if (l < cur) sort(id, l, cur - 1);
  if (cur < r) sort(id, cur + 1, r);
}
void solve(vector<int> id, vector<int> cp) {
  if (id.empty()) return;
  vector<int> from, to;
  int root = -1;
  for (int i : id) {
    if (root == -1 || cp[i] < cp[root]) root = i;
  }
  for (int i = 0; i < n; i++) {
    if (edge(root, i)) to.push_back(i);
  }
  for (int i = 0; i < n; i++) {
    if (i == root || edge(i, root)) from.push_back(i);
  }
  if (!to.empty()) sort(to, 0, (int)to.size() - 1);
  sort(from.begin(), from.end(), [&](int x, int y) { return edge(x, y); });
  int sz = to.size();
  reverse(from.begin(), from.end());
  reverse(to.begin(), to.end());
  vector<int> l(sz), r(sz);
  vector<int> ret(n, -1);
  for (int i = 0; i < sz; i++) {
    if (i > 0) l[i] = l[i - 1];
    while (l[i] < (int)from.size() && edge(from[l[i]], to[i])) l[i]++;
    int lo = l[i], hi = from.size();
    if (lo == hi) {
      r[i] = lo;
      continue;
    }
    while (hi - lo > 1) {
      int mid = (hi + lo) >> 1;
      if (!edge(from[mid], to[i]))
        lo = mid;
      else
        hi = mid;
    }
    r[i] = lo;
    ret[l[i]] = max(ret[l[i]], r[i]);
  }
  set<int> cur;
  sz = from.size();
  for (int i = sz - 1; i > -1; i--) {
    if (!was[from[i]]) {
      was[from[i]] = 1;
      int left = deg[root] - (deg[from[i]] - i);
      res += 2 * left;
      pos -= left;
      if (i < sz - 1) {
        while (cur.size() && *cur.begin() <= ret[i + 1]) cur.erase(cur.begin());
      }
      int cnt = sz - 1 - i - cur.size();
      res += 2 * cnt;
      pos -= cnt;
    }
    cur.insert(i);
  }
  vector<int> nxt;
  vector<int> cxt(n);
  for (int i = 0; i < (int)to.size(); i++) {
    if (!was[to[i]]) {
      nxt.push_back(to[i]);
      cxt[to[i]] = i;
    }
  }
  solve(nxt, cxt);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  g.resize(n);
  deg.resize(n);
  was.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> g[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      deg[i] += edge(i, j);
    }
  }
  res = n * (n - 1) / 2;
  pos = n * (n - 1) / 2;
  int root = 0;
  for (int i = 0; i < n; i++) {
    if (deg[i] < deg[root]) root = i;
  }
  vector<int> cur;
  for (int i = 0; i < n; i++) {
    if (edge(i, root)) cur.push_back(i);
  }
  sort(cur.begin(), cur.end(), [&](int x, int y) { return edge(x, y); });
  int cnt = 0;
  int sz = cur.size();
  for (int i = 0; i < sz; i++) {
    if (deg[cur[i]] == deg[root] + sz - i) {
      cnt++;
    } else {
      break;
    }
  }
  res += 614LL * n * (cnt * (n - cnt) + cnt * (cnt - 1) / 2);
  pos -= cnt * (n - cnt) + cnt * (cnt - 1) / 2;
  vector<int> id(n);
  iota(id.begin(), id.end(), 0);
  solve(id, deg);
  res += pos * 3;
  cout << res;
  return 0;
}
