#include <bits/stdc++.h>
using namespace std;
const int N = 512, K = 200;
int n, M, x[K], y[K], ans, m;
long long a[N][N], b[N][N];
int main() {
  scanf("%d", &n);
  M = 1 << n;
  for (int i = (0); i <= (M - 1); ++i)
    for (int j = (0); j <= (M - 1); ++j) scanf("%lld", &a[i][j]);
  scanf("%d", &m);
  for (int i = (1); i <= (m); ++i) scanf("%d%d", x + i, y + i);
  for (int i = (m); i >= (1); --i)
    x[i] = x[i] < x[1] ? x[i] - x[1] + M : x[i] - x[1],
    y[i] = y[i] < y[1] ? y[i] - y[1] + M : y[i] - y[1];
  for (int i = (1); i <= (n); ++i) {
    for (int j = (0); j <= (M - 1); ++j)
      for (int k = (0); k <= (M - 1); ++k)
        for (int p = (1); p <= (m); ++p)
          b[(j + x[p]) & (M - 1)][(k + y[p]) & (M - 1)] ^= a[j][k];
    for (int j = (0); j <= (M - 1); ++j)
      for (int k = (0); k <= (M - 1); ++k) a[j][k] = b[j][k], b[j][k] = 0;
    for (int j = (1); j <= (m); ++j)
      x[j] = (x[j] << 1) & (M - 1), y[j] = (y[j] << 1) & (M - 1);
  }
  for (int i = (0); i <= (M - 1); ++i)
    for (int j = (0); j <= (M - 1); ++j) ans += !!a[i][j];
  printf("%d\n", ans);
  return 0;
}
