#include <bits/stdc++.h>
const int maxn = 1 << 9;
using namespace std;
int rd() {
  int x = 0, f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) x = (x << 3) + (x << 1) + c - '0', c = getchar();
  return x * f;
}
int k, n, x[101], y[101], t;
long long a[maxn][maxn], b[maxn][maxn];
int main() {
  scanf("%d", &k);
  n = 1 << k;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) scanf("%lld", &a[i][j]);
  scanf("%d", &t);
  for (int i = 1; i <= t; i++) scanf("%d %d", &x[i], &y[i]);
  for (int i = t; i >= 1; i--)
    x[i] = (x[i] + n - x[1]) % n, y[i] = (y[i] + n - y[1]) % n;
  for (int i = 1; i <= k; i++) {
    for (int J = 0; J < n; J++)
      for (int K = 0; K < n; K++)
        for (int l = 1; l <= t; l++)
          b[J][K] ^= a[(J + n - x[l]) % n][(K + n - y[l]) % n];
    memcpy(a, b, sizeof(b));
    memset(b, 0, sizeof(b));
    for (int l = 1; l <= t; l++) x[l] = (x[l] << 1) % n, y[l] = (y[l] << 1) % n;
  }
  int ans = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      if (a[i][j]) ans++;
  printf("%d\n", ans);
  return 0;
}
