#include <bits/stdc++.h>
using namespace std;
namespace cxcyl {
long long k, n, t, f[15][1024][1024], a[2][1024][1024], ans;
inline long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  while (c > -1 && c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  if (c == -1) return 0;
  while (c >= '0' && c <= '9') x = x * 10 + c - 48, c = getchar();
  return x * f;
}
inline long long mo(long long x) { return x >= n ? x - n : x; }
inline long long main() {
  k = read();
  n = 1ll << k;
  for (long long i = 0; i < n; ++i)
    for (long long j = 0; j < n; ++j) a[1][i][j] = read();
  t = read();
  for (long long i = 0; i < t; ++i) {
    long long x = read(), y = read();
    f[0][x - 1][y - 1] = 1;
  }
  for (long long i = 0; i < k; ++i) {
    if (i) {
      for (long long x = 0; x < n; ++x)
        for (long long y = 0; y < n; ++y)
          if (f[i - 1][x][y])
            for (long long x1 = 0; x1 < n; ++x1)
              for (long long y1 = 0; y1 < n; ++y1)
                f[i][mo(x + x1)][mo(y + y1)] ^=
                    f[i - 1][x][y] * f[i - 1][x1][y1];
    }
    memset(a[i & 1], 0, sizeof(a[i & 1]));
    for (long long x1 = 0; x1 < n; ++x1)
      for (long long y1 = 0; y1 < n; ++y1) {
        assert(f[i][x1][y1] == 1 || f[i][x1][y1] == 0);
        if (f[i][x1][y1])
          for (long long x = 0; x < n; ++x)
            for (long long y = 0; y < n; ++y)
              a[i & 1][mo(x + x1)][mo(y + y1)] ^=
                  a[~i & 1][x][y] * f[i][x1][y1];
      }
  }
  for (long long i = 0; i < n; ++i)
    for (long long j = 0; j < n; ++j) ans += (bool)a[(k - 1) & 1][i][j];
  cout << ans << endl;
  return 0;
}
}  // namespace cxcyl
signed main() { return cxcyl::main(); }
