#include <bits/stdc++.h>
using namespace std;
using cat = long long;
template <int K, typename T>
inline __attribute__((optimize("Ofast")))
__attribute__((target("arch=core-avx2"))) void
XOR(T* __restrict A, const T B[1 << K]) {
  for (int i = 0; i < (1 << K); i++) A[i] ^= B[i];
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  int K;
  cin >> K;
  int N = 1 << K;
  vector<vector<cat> > A(N, vector<cat>(1 << 9));
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) cin >> A[i][j];
  int C;
  cin >> C;
  vector<int> X(C), Y(C);
  for (int i = 0; i < C; i++) {
    int x, y;
    cin >> x >> y;
    X[i] = x - 1, Y[i] = y - 1;
  }
  for (int i = 0; i < K; i++) {
    vector<vector<cat> > A_nw(2 * N, vector<cat>(1 << 10, 0));
    for (int j = 0; j < C; j++)
      for (int k = 0; k < N; k++) XOR<9>(&(A_nw[X[j] + k][Y[j]]), A[k].data());
    for (int k = 0; k < N; k++)
      for (int l = 0; l < N; l++)
        A[k][l] =
            A_nw[k][l] ^ A_nw[k + N][l] ^ A_nw[k][l + N] ^ A_nw[k + N][l + N];
    for (int j = 0; j < C; j++)
      X[j] = (2 * X[j]) & (N - 1), Y[j] = (2 * Y[j]) & (N - 1);
  }
  int ans = 0;
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      if (A[i][j] != 0) ans++;
  cout << ans << "\n";
  return 0;
}
