#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long f = 1, x = 0;
  char s = getchar();
  while (s < '0' || s > '9') {
    if (s == '-') f = -1;
    s = getchar();
  }
  while (s >= '0' && s <= '9') {
    x = x * 10 + s - 48;
    s = getchar();
  }
  return f * x;
}
const int N = 1 << 11;
int x[N], y[N];
long long a[N][N], b[N][N];
int main() {
  int k = read();
  int n = 1 << k;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) a[i][j] = read();
  int t = read();
  for (int i = 0; i < t; i++) x[i] = read(), y[i] = read();
  for (int i = 1; i < t; i++) x[i] -= x[0], y[i] -= y[0];
  x[0] = y[0] = 0;
  for (int _ = 0; _ < k; _++) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) b[i][j] = 0;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        for (int q = 0; q < t; q++) {
          int xx = (i + x[q]) & (n - 1), yy = (j + y[q]) & (n - 1);
          b[xx][yy] ^= a[i][j];
        }
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) a[i][j] = b[i][j];
    for (int i = 0; i < t; i++)
      x[i] = (2 * x[i]) & (n - 1), y[i] = (2 * y[i]) & (n - 1);
  }
  int ans = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) ans += (a[i][j] > 0);
  printf("%d\n", ans);
  return 0;
}
