#include <bits/stdc++.h>
using namespace std;
const int N = 1 << 9;
using ll = long long;
int n, kk, vv;
ll a[N][N], b[N][N];
int x[N], y[N];
int main() {
  ios::sync_with_stdio(false);
  cin >> kk;
  n = 1 << kk;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) cin >> a[i][j];
  cin >> vv;
  for (int i = 0; i < vv; i++) cin >> x[i] >> y[i];
  for (int i = vv - 1; i >= 0; --i) x[i] -= x[0], y[i] -= y[0];
  for (int t = 0; t < kk; t++) {
    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 < vv; q++) {
          int nx = (i + x[q]) & (n - 1), ny = (j + y[q]) & (n - 1);
          b[nx][ny] ^= 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 < vv; i++)
      x[i] = (x[i] * 2) & (n - 1), y[i] = (y[i] * 2) & (n - 1);
  }
  int ans = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) ans += !!a[i][j];
  cout << ans << endl;
  return 0;
}
