#include <bits/stdc++.h>
long long n, K, ans;
std::vector<std::vector<long long> > A, B, O;
long long read() {
  char c = getchar();
  long long ans = 0;
  bool flag = true;
  while (!isdigit(c)) flag &= (c != '-'), c = getchar();
  while (isdigit(c)) ans = ans * 10 + c - '0', c = getchar();
  return flag ? ans : -ans;
}
void Write(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x < 10)
    putchar(x + '0');
  else
    Write(x / 10), putchar(x % 10 + '0');
}
long long min(long long x, long long y) { return x < y ? x : y; }
long long max(long long x, long long y) { return x > y ? x : y; }
void _min(long long &x, long long y) {
  if (x > y) x = y;
}
void _max(long long &x, long long y) {
  if (x < y) x = y;
}
std::vector<std::vector<long long> > operator*(
    std::vector<std::vector<long long> > A,
    std::vector<std::vector<long long> > B) {
  std::vector<std::vector<long long> > C = O;
  for (long long i2 = 0; i2 < n; i2++)
    for (long long j2 = 0; j2 < n; j2++) {
      if (!B[i2][j2]) continue;
      for (long long i1 = 0; i1 < n; i1++)
        for (long long j1 = 0; j1 < n; j1++)
          C[(i1 + i2) % n][(j1 + j2) % n] ^= A[i1][j1];
    }
  return C;
}
int main() {
  K = read(), n = (1 << K);
  O.resize(n);
  for (long long i = 0; i < n; i++) O[i].resize(n);
  A = B = O;
  for (long long i = 0; i < n; i++)
    for (long long j = 0; j < n; j++) A[i][j] = read();
  for (long long i = 0, u, v, t = read(); i < t; i++)
    u = read(), v = read(), B[--u][--v] = 1;
  for (long long i = 0; i < K; i++) A = A * B, B = B * B;
  for (long long i = 0; i < n; i++)
    ans +=
        std::count_if(A[i].begin(), A[i].end(), [&](long long x) { return x; });
  return Write(ans), 0;
}
