#include <bits/stdc++.h>
using namespace std;
long long a[555][555], b[555][555];
int c[555][2];
int main() {
  int n, m, i, j, t, k, all;
  scanf("%d", &n);
  all = (1 << n) - 1;
  for (i = 0; i <= all; i++)
    for (j = 0; j <= all; j++) scanf("%lld", &a[i][j]);
  scanf("%d", &m);
  for (i = 1; i <= m; i++)
    scanf("%d%d", &c[i][0], &c[i][1]), c[i][0]--, c[i][1]--;
  for (t = 0; t < n; t++) {
    for (i = 0; i <= all; i++)
      for (j = 0; j <= all; j++)
        if (a[i][j])
          for (k = 1; k <= m; k++)
            b[(i + c[k][0]) & all][(j + c[k][1]) & all] ^= a[i][j];
    for (i = 0; i <= all; i++)
      for (j = 0; j <= all; j++) a[i][j] = b[i][j], b[i][j] = 0;
    for (k = 1; k <= m; k++)
      c[k][0] = (c[k][0] << 1) & all, c[k][1] = (c[k][1] << 1) & all;
  }
  int ans = 0;
  for (i = 0; i <= all; i++)
    for (j = 0; j <= all; j++)
      if (a[i][j]) ans++;
  printf("%d", ans);
}
