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

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

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

int32 C, N;
bool c[1000][1000] = {}, ok[500][500] = {};

int32 tov(int32 x){
	return min(x, N-x-1);
}

bool ok_mass(int32 y, int32 x){
	return (c[y][x] == c[y][N-x-1] && c[y][x] == c[N-y-1][x] && c[y][x] == c[N-y-1][N-x-1]);
}

int main(void){
	int32 out = 0;
	cin >> C >> N;
	REP(i, N){
		string s;
		cin >> s;
		REP(j, N){
			c[i][j] = s[j]-'0';
		}
	}
	REP(i, N/2){
		REP(j, N/2){
			if(ok_mass(i, j))
				ok[i][j] = 1;
			else
				out++;
		}
	}
	int32 cnt = out == 0;
	REP(i, C-1){
		int32 D;
		cin >> D;
		REP(j, D){
			int32 y, x;
			cin >> y >> x; y--; x--;
			c[y][x] ^= 1;
			if(!ok[tov(y)][tov(x)]){
				if(ok_mass(tov(y), tov(x))){
					out--;
					ok[tov(y)][tov(x)] = 1;
				}
			}else{
				ok[tov(y)][tov(x)] = 0;
				out++;
			}
		}
		if(out == 0) cnt++;
	}
	cout << cnt << endl;
}

