#include <bits/stdc++.h>
using namespace std;
int n;
int indegree[8010];
int type[8010];
int d[8010];
long long ans = 0;
bool adjacencyMat[8010][8010];
char s[8010];
queue<int> q;
map<char, string> hexToBinDict;
int main() {
  hexToBinDict['0'] = "0000";
  hexToBinDict['1'] = "0001";
  hexToBinDict['2'] = "0010";
  hexToBinDict['3'] = "0011";
  hexToBinDict['4'] = "0100";
  hexToBinDict['5'] = "0101";
  hexToBinDict['6'] = "0110";
  hexToBinDict['7'] = "0111";
  hexToBinDict['8'] = "1000";
  hexToBinDict['9'] = "1001";
  hexToBinDict['A'] = "1010";
  hexToBinDict['B'] = "1011";
  hexToBinDict['C'] = "1100";
  hexToBinDict['D'] = "1101";
  hexToBinDict['E'] = "1110";
  hexToBinDict['F'] = "1111";
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s", s);
    for (int j = 0; j < n / 4; j++) {
      string subline = hexToBinDict[s[j]];
      for (int k = 0; k < 4; k++) {
        int bit = subline[k] - '0';
        adjacencyMat[i][4 * j + k] = bit;
        if (bit == 1) indegree[4 * j + k]++;
      }
    }
  }
  for (int i = 0; i < n; i++)
    if (!indegree[i]) q.push(i);
  int tmp = n;
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    tmp--;
    type[now] = 3;
    ans += (614ll * n + 1) * tmp;
    for (int i = 0; i < n; i++)
      if (adjacencyMat[now][i]) {
        indegree[i]--;
        if (!indegree[i]) q.push(i);
      }
  }
  if (!tmp) return printf("%lld\n", ans), 0;
  int maxIndegreePosition = -1;
  int maxIndegree = 0;
  for (int i = 0; i < n; i++)
    if (indegree[i] > maxIndegree) {
      maxIndegree = indegree[i];
      maxIndegreePosition = i;
    }
  for (int i = 0; i < n; i++)
    if (type[i] != 3)
      type[i] =
          (i == maxIndegreePosition || adjacencyMat[i][maxIndegreePosition])
              ? 1
              : 2;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      if (i != j && type[i] != 3 && type[j] != 3 && type[i] != type[j] &&
          adjacencyMat[i][j])
        d[j]++;
  for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++)
      if (type[i] != 3 && type[j] != 3)
        ans += 3 + (type[i] == type[j] && d[i] == d[j]);
  printf("%lld\n", ans);
}
