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

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second

template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }

typedef long long ll;
typedef pair<int, int> pii;

const ll INF = 1ll<<29;
const ll MOD = 1000000007;
const bool debug = 0;

int C, N;
int maps[1123][1123];
bool bef[1123][1123];

int dcnt; // ???????????°

bool check(int x, int y) {
	bool now = maps[y][x];
	pii p1(y, x), p2(y, N - 1 - x), p3(N - 1 - y, N - 1 - x), p4(N - 1 - y, x);
	
	if (maps[p1.fi][p1.se] != now) return false;
	if (maps[p2.fi][p2.se] != now) return false;
	if (maps[p3.fi][p3.se] != now) return false;
	if (maps[p4.fi][p4.se] != now) return false;
	
	return true;
}

int main() {
	cin >> C >> N;
	REP(i, N) {
		string s; cin >> s;
		REP(j, N) maps[i][j] = s[j] - '0';
	}
	
	REP(i, N / 2) REP(j, N / 2) {
		bef[i][j] = check(j, i);
		if (!bef[i][j]) dcnt++;
	}
	
	int ans = 0;
	if (dcnt == 0) ans++;
	
	while (--C) {
		int D; cin >> D;
		
		REP(i, D) {
			int y, x;
			scanf("%d %d", &y, &x);
			y--; x--;
			
			maps[y][x] ^= 1;
			
			if (y >= N / 2) y = N - 1 - y;
			if (x >= N / 2) x = N - 1 - x;
			
			bool jdg = check(x, y);
			
			if (!jdg && bef[y][x]) {
				dcnt++;
				bef[y][x] = false;
			}
			if (jdg) {
				dcnt--;
				bef[y][x] = true;
			}
		}
		
		if (dcnt == 0) ans++;
	}
	
	cout << ans << endl;
	
	return 0;
}