#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
using namespace std;
const int MOD = 1e9 + 7;
const int MX = 2e5 + 5;
const long long INF = 1e18;
const long double PI = acos((long double)-1);
const int xd[4] = {1, 0, -1, 0}, yd[4] = {0, 1, 0, -1};
template <class T>
bool ckmin(T& a, const T& b) {
  return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
  return a < b ? a = b, 1 : 0;
}
int pct(int x) { return __builtin_popcount(x); }
int bit(int x) { return 31 - __builtin_clz(x); }
int cdiv(int a, int b) { return a / b + !(a < 0 || a % b == 0); }
template <class A>
void re(complex<A>& c);
template <class A, class B>
void re(pair<A, B>& p);
template <class A>
void re(vector<A>& v);
template <class A, size_t SZ>
void re(array<A, SZ>& a);
template <class T>
void re(T& x) {
  cin >> x;
}
void re(double& d) {
  string t;
  re(t);
  d = stod(t);
}
void re(long double& d) {
  string t;
  re(t);
  d = stold(t);
}
template <class H, class... T>
void re(H& h, T&... t) {
  re(h);
  re(t...);
}
template <class A>
void re(complex<A>& c) {
  A a, b;
  re(a, b);
  c = {a, b};
}
template <class A, class B>
void re(pair<A, B>& p) {
  re(p.first, p.second);
}
template <class A>
void re(vector<A>& x) {
  for (auto& a : x) re(a);
}
template <class A, size_t SZ>
void re(array<A, SZ>& x) {
  for (auto& a : x) re(a);
}
template <class A, class B>
string to_string(pair<A, B> p);
template <class A>
string to_string(complex<A> c) {
  return to_string(make_pair(c.real(), c.imag()));
}
string to_string(bool b) { return b ? "true" : "false"; }
string to_string(char c) {
  string second = "";
  second += c;
  return second;
}
string to_string(string second) { return second; }
string to_string(const char* second) { return (string)second; }
string to_string(vector<bool> v) {
  bool fst = 1;
  string res = "{";
  for (int i = (0); i < ((int)v.size()); ++i) {
    if (!fst) res += ", ";
    fst = 0;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}
template <size_t SZ>
string to_string(bitset<SZ> b) {
  string res = "";
  for (int i = (0); i < (SZ); ++i) res += char('0' + b[i]);
  return res;
}
template <class T>
string to_string(T v) {
  bool fst = 1;
  string res = "{";
  for (const auto& x : v) {
    if (!fst) res += ", ";
    fst = 0;
    res += to_string(x);
  }
  res += "}";
  return res;
}
template <class A, class B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <class A>
void pr(A x) {
  cout << to_string(x);
}
template <class H, class... T>
void pr(const H& h, const T&... t) {
  pr(h);
  pr(t...);
}
void ps() { pr("\n"); }
template <class H, class... T>
void ps(const H& h, const T&... t) {
  pr(h);
  if (sizeof...(t)) pr(" ");
  ps(t...);
}
void DBG() { cerr << "]" << endl; }
template <class H, class... T>
void DBG(H h, T... t) {
  cerr << to_string(h);
  if (sizeof...(t)) cerr << ", ";
  DBG(t...);
}
void setIn(string second) { freopen(second.c_str(), "r", stdin); }
void setOut(string second) { freopen(second.c_str(), "w", stdout); }
void unsyncIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}
void setIO(string second = "") {
  unsyncIO();
  if ((int)second.size()) {
    setIn(second + ".in"), setOut(second + ".out");
  }
}
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
int n, in[8000], IN[8000];
bitset<8000> adj[8000];
long long ans;
int con(char c) {
  if ('0' <= c && c <= '9') return c - '0';
  return c - 'A' + 10;
}
int bit(char a, int b) { return con(a) & (1 << (3 - b)); }
void deal(vector<int> a, vector<int> b) {
  for (auto& q : a)
    for (auto& w : a)
      if (adj[q][w]) IN[w]++;
  sort(begin(a), end(a), [](int x, int y) { return IN[x] < IN[y]; });
  vector<int> c0(n);
  for (auto& q : a) {
    int tmp = 0;
    for (auto& w : b)
      if (adj[q][w]) tmp++;
    c0[tmp]++;
  }
  for (int i = (0); i < (n); ++i) ans += c0[i] * (c0[i] - 1) / 2;
}
int main() {
  setIO();
  re(n);
  for (int i = (0); i < (n); ++i) {
    string second;
    re(second);
    for (int j = (0); j < (n); ++j) {
      adj[i][j] = bit(second[j / 4], j % 4);
    }
  }
  for (int i = (0); i < (n); ++i)
    for (int j = (0); j < (n); ++j)
      if (adj[i][j]) in[j]++;
  queue<int> q;
  for (int i = (0); i < (n); ++i)
    if (!in[i]) q.push(i);
  long long bad = 614 * n + 1;
  int lef = n;
  while ((int)q.size()) {
    int x = q.front();
    q.pop();
    lef--;
    ans += bad * lef;
    for (int i = (0); i < (n); ++i)
      if (adj[x][i])
        if (!(--in[i])) q.push(i);
  }
  if (lef) {
    ans += 3 * lef * (lef - 1) / 2;
    int x = 0;
    for (int i = (0); i < (n); ++i)
      if (in[i] > in[x]) x = i;
    vector<int> a, b;
    for (int i = (0); i < (n); ++i)
      if (in[i]) {
        if (i == x || adj[i][x])
          a.push_back(i);
        else
          b.push_back(i);
      }
    deal(a, b);
    deal(b, a);
  }
  ps(ans);
}
