#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

#define pb push_back
#define rep(i, n) for(int i = 0; i < (n); i++)
#define dep(i, a, n) for(int i = a; i >= (n); i--)
#define FOR(i, a, n) for(int i = a; i < (n); i++)
#define mod (ll)(1e9+7)
#define int ll
#define INF 100000000

__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

ll c, n, w, x, y, result = 0;
char p[1001][1001];
ll d[1001][1001];
bool a[1001][1001];

bool ch(int x, int y) {
  if(d[x][y] == d[n-1-x][y] && d[x][y] == d[x][n-1-y] && d[x][y] == d[n-1-x][n-1-y]) return true;
  return false;
}

signed main() {
	cin >> c >> n;
  rep(i, n) rep(j, n) cin >> p[i][j];
  ll tmp = 0;
  rep(i, n) rep(j, n) d[i][j] = p[i][j] - '0';
  rep(i, n / 2) rep(j, n / 2) {
    if(ch(i, j)) a[i][j] = true;
    else {
      a[i][j] = false;
      tmp++;
    }
  }
  if(tmp == 0) result++;
  rep(i, c - 1) {
    cin >> w;
    rep(j, w) {
      cin >> x >> y;
      x--, y--;
      if(d[x][y] == 1) d[x][y] = 0;
      else d[x][y] = 1;
      if(x >= n / 2) x = n - 1 - x;
      if(y >= n / 2) y = n - 1 - y;
      if(ch(x, y) != a[x][y]) {
        if(ch(x, y)) tmp--;
        else tmp++;
      }
      a[x][y] = ch(x, y);
    }
    if(tmp == 0) result++;
  }
  cout << result << endl;
	return 0;
}
