#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
const int maxn = 8000 + 10, N = 1e3 + 90, SQ = 150, base = 1999, mod = 1e9 + 7,
          INF = 1e9 + 1, lg = 25;
const long double eps = 1e-4;
int n, cmp[maxn], oo[maxn], ii[maxn], inp[maxn], inq[maxn], cnt1[maxn],
    cnt2[maxn];
string res[maxn];
vector<int> ver, input[maxn];
bool mark[maxn][maxn], bad[maxn], vis1[maxn], vis2[maxn];
queue<int> q;
inline string to_bin(int x) {
  string res = "";
  for (int i = 3; i >= 0; --i) {
    res += (x & (1 << i) ? "1" : "0");
  }
  return res;
}
inline void set_maxi_in() {
  int mx = ver[0];
  for (int i : ver) {
    if ((int)input[i].size() > (int)input[mx].size()) {
      mx = i;
    }
  }
  cmp[mx] = 1;
  for (int i : input[mx]) {
    cmp[i] = 1;
  }
  for (int i : ver) {
    if (cmp[i] != 1) cmp[i] = 2;
  }
}
inline void set_in_out() {
  for (int i : ver) {
    if (cmp[i] == 1) {
      for (auto u : input[i]) {
        if (cmp[u] == 2) {
          inq[i]++;
        }
      }
    } else if (cmp[i] == 2) {
      for (auto u : input[i]) {
        if (cmp[u] == 1) {
          inp[i]++;
        }
      }
    }
  }
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;
    for (int j = 0; j < n / 4; ++j) {
      if (s[j] >= '0' && s[j] <= '9') {
        res[i] += to_bin(s[j] - '0');
      } else {
        int val = 10 + (s[j] - 'A');
        res[i] += to_bin(val);
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (res[i][j] == '1') {
        oo[i]++;
        ii[j]++;
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    if (!ii[i]) {
      q.push(i);
    }
  }
  long long remain = n, ans = 0;
  while ((int)q.size()) {
    int v = q.front();
    q.pop();
    remain--;
    ans += (614ll * n + 1) * remain;
    bad[v] = 1;
    for (int i = 0; i < n; ++i) {
      ii[i]--;
      if (ii[i] == 0) {
        q.push(i);
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    if (!bad[i]) {
      ver.push_back(i);
    }
  }
  if (!(int)ver.size()) {
    cout << ans << '\n';
    return 0;
  }
  for (int i : ver) {
    for (int j : ver) {
      if (res[i][j] == '1') {
        input[j].push_back(i);
      }
    }
  }
  set_maxi_in();
  set_in_out();
  long long num1 = 0, num2 = 0, bef = 0;
  for (int i : ver) {
    if (cmp[i] == 1) {
      cnt1[inq[i]]++;
      num1++;
    } else if (cmp[i] == 2) {
      cnt2[inp[i]]++;
      num2++;
    }
  }
  for (int i : ver) {
    if (cmp[i] == 1 && !vis1[inq[i]]) {
      ans += (1ll * 2 * cnt1[inq[i]] * (cnt1[inq[i]] - 1));
      bef += (1ll * cnt1[inq[i]] * (cnt1[inq[i]] - 1)) / 2;
      vis1[inq[i]] = 1;
    } else if (cmp[i] == 2 && !vis2[inp[i]]) {
      ans += (1ll * 2 * cnt2[inp[i]] * (cnt2[inp[i]] - 1));
      bef += (1ll * cnt2[inp[i]] * (cnt2[inp[i]] - 1)) / 2;
      vis2[inp[i]] = 1;
    }
  }
  long long tot = (1ll * (int)ver.size() * ((int)ver.size() - 1)) / 2;
  tot -= bef;
  ans += (tot * 3);
  cout << ans << '\n';
}
