#include <bits/stdc++.h>
using namespace std;
inline long long getint() {
  long long num = 0, flag = 1;
  char c;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-') flag = -1;
  while (c >= '0' && c <= '9') num = num * 10 + c - 48, c = getchar();
  return num * flag;
}
int n, k, t, ans;
long long F[2][1005][1005];
int now = 1, pre;
int X[1005], Y[1005];
inline void solve() {
  memset(F[now], 0, sizeof F[now]);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      for (int k = 1; k <= t; k++)
        F[now][(i + X[k]) & (n - 1)][(j + Y[k]) & (n - 1)] ^= F[pre][i][j];
  for (int i = 1; i <= t; i++)
    X[i] = (X[i] << 1) & (n - 1), Y[i] = (Y[i] << 1) & (n - 1);
}
int main() {
  k = getint();
  n = 1 << k;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) F[pre][i][j] = getint();
  t = getint();
  for (int i = 1; i <= t; i++) X[i] = getint() - 1, Y[i] = getint() - 1;
  while (k--) solve(), swap(now, pre);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      if (F[pre][i][j]) ans++;
  printf("%d\n", ans);
}
