Due to the success of TWICE, JYP Entertainment has earned countless money and emerged as the biggest entertainment firm by market capitalization. Therefore, the boss, JYP, has decided to create a new nation and has appointed you to provide a design diagram.

The new nation consists of n cities and some roads between them. JYP has given some restrictions:

  * To guarantee efficiency while avoiding chaos, for any 2 different cities A and B, there is exactly one road between them, and it is one-directional. There are no roads connecting a city to itself.

  * The logo of rivaling companies should not appear in the plan, that is, there does not exist 4 distinct cities A,B,C,D , such that the following configuration occurs.

<image>

JYP has given criteria for your diagram. For two cities A,B, let dis(A,B) be the smallest number of roads you have to go through to get from A to B. If it is not possible to walk from A to B, dis(A,B) = 614n. Then, the efficiency value is defined to be the sum of dis(A,B) for all ordered pairs of distinct cities (A,B).

Note that dis(A,B) doesn't have to be equal to dis(B,A).

You have drawn a design diagram that satisfies JYP's restrictions. Find the sum of dis(A,B) over all ordered pairs of cities (A,B) with A≠ B.

Note that the input is given in compressed form. But even though it is compressed, you'd better use fast input.

Input

The first line contains a single integer n (4 ≤ n ≤ 8000, n ≡ 0 \pmod{4}) — the number of cities.

A binary matrix is encrypted in the following format. Each of n next lines contains n/4 one-digit hexadecimal numbers (that is, these numbers can be represented either as digits from 0 to 9 or as uppercase Latin letters from A to F). Binary representation of each of these numbers denotes next 4 elements of the matrix in the corresponding row. For example, if the number B is given, then the corresponding elements are 1011, and if the number is 5, then the corresponding elements are 0101.

After you obtain the decrypted binary matrix, the j-th character of the i-th row is 1 if the one-directional road between cities i and j is directed from i to j, and 0 otherwise. It is guaranteed that the graph satisfies the restrictions mentioned above.

Output

Output one integer, representing the sum of dis(A,B) over all ordered pairs of cities (A,B) with A≠ B.

Examples

Input


4
7
2
1
4


Output


7380


Input


8
7F
3F
1F
0C
06
03
11
18


Output


88464

Note

The first example corresponds to the matrix:

\begin{matrix} 0111 \\\ 0010 \\\ 0001 \\\ 0100 \\\ \end{matrix}

Which corresponds to this graph:

<image>

dis(1,2)=dis(1,3)=dis(1,4)=dis(2,3)=dis(3,4)=dis(4,2)=1

dis(2,4)=dis(4,3)=dis(3,2)=2

dis(2,1)=dis(3,1)=dis(4,1)=2456

Therefore the answer for the diagram is 7380.
#include <bits/stdc++.h>
using namespace std;
long long read() {
  long long x = 0, f = 0;
  char ch = getchar();
  while (!isdigit(ch)) f = ch == '-', ch = getchar();
  while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  return f ? -x : x;
}
void ckmax(int &x, int y) {
  if (x < y) x = y;
}
void ckmin(int &x, int y) {
  if (x > y) x = y;
}
const int N = 8005;
int n;
int g[N][N];
char s[N];
long long val = 0;
int in[N];
int main() {
  n = read();
  for (int i = (0); i <= (n - 1); i++) {
    scanf("%s", s);
    for (int j = (0); j <= (n / 4 - 1); j++) {
      int v = '0' <= s[j] && s[j] <= '9' ? s[j] - '0' : s[j] - 'A' + 10;
      for (int t = (0); t <= (3); t++) g[i][j * 4 + (3 - t)] = v >> t & 1;
    }
    for (int j = (0); j <= (n - 1); j++) in[j] += g[i][j];
  }
  val = n * 614;
  long long ans = 0;
  {
    queue<int> q;
    for (int i = (0); i <= (n - 1); i++)
      if (!in[i]) q.push(i);
    while (!q.empty()) {
      int x = q.front();
      q.pop();
      for (int i = (0); i <= (n - 1); i++)
        if (g[x][i]) {
          if (!--in[i]) q.push(i);
        }
    }
  }
  int cnt = n;
  for (int i = (0); i <= (n - 1); i++)
    if (!in[i]) ans += (val + 1) * --cnt;
  int x = -1;
  for (int i = (0); i <= (n - 1); i++)
    if (x == -1 || in[i] > in[x]) x = i;
  if (!in[x]) return cout << ans << endl, 0;
  vector<int> P, Q;
  for (int i = (0); i <= (n - 1); i++)
    if (in[i]) {
      if (i == x || g[i][x])
        P.push_back(i);
      else
        Q.push_back(i);
    }
  sort(P.begin(), P.end(), [&](int a, int b) { return g[a][b]; });
  sort(Q.begin(), Q.end(), [&](int a, int b) { return g[a][b]; });
  ans += 3LL * P.size() * Q.size();
  ans += 1LL * P.size() * (P.size() - 1) / 2;
  ans += 1LL * Q.size() * (Q.size() - 1) / 2;
  for (int i = (0); i <= ((int)P.size() - 1); i++) in[P[i]] -= i;
  for (int i = (0); i <= ((int)Q.size() - 1); i++) in[Q[i]] -= i;
  for (int i = (0); i <= ((int)P.size() - 1); i++)
    for (int j = (i + 1); j <= ((int)P.size() - 1); j++)
      ans += in[P[i]] == in[P[j]] ? 3LL : 2LL;
  for (int i = (0); i <= ((int)Q.size() - 1); i++)
    for (int j = (i + 1); j <= ((int)Q.size() - 1); j++)
      ans += in[Q[i]] == in[Q[j]] ? 3LL : 2LL;
  cout << ans << endl;
  return 0;
}
