#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;
}
const int N = 1005;
int n, k, b;
long long a[N][N], c[N][N];
int x[N], y[N];
int main() {
  k = read(), b = 1 << k;
  for (int i = (0); i <= (b - 1); i++)
    for (int j = (0); j <= (b - 1); j++) a[i][j] = read();
  n = read();
  for (int i = (1); i <= (n); i++) x[i] = read(), y[i] = read();
  for (int d = (0); d <= (k - 1); d++) {
    memset(c, 0, sizeof(c));
    for (int i = (0); i <= (b - 1); i++)
      for (int j = (0); j <= (b - 1); j++)
        for (int t = (1); t <= (n); t++)
          c[(i + (x[t] << d)) & (b - 1)][(j + (y[t] << d)) & (b - 1)] ^=
              a[i][j];
    swap(a, c);
  }
  int c = 0;
  for (int i = (0); i <= (b - 1); i++)
    for (int j = (0); j <= (b - 1); j++)
      if (a[i][j]) c++;
  cout << c << endl;
  return 0;
}
