#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
using namespace std;

struct Info{
	Info(int arg_row,int arg_col){
		row = arg_row;
		col = arg_col;
	}
	int row,col;
};

 int main(){

	 int C,N,count = 0,not_num,not_num_work,D,tmp_row,tmp_col;
	 scanf("%d %d",&C,&N);

	 char** base_table = new char*[N];
	 bool** check = new bool*[N/2];

	 for(int i = 0; i < N; i++){
		 base_table[i] = new char[N+1];
	 }

	 for(int i = 0; i < N/2; i++){
		 check[i] = new bool[N/2];
	 }

	 for(int i = 0; i < N; i++){
		 scanf("%s",base_table[i]);
	 }

	 not_num = 0;
	 for(int row_div = 0; row_div <= (N/2)-1;row_div++){
		 for(int col_div = 0; col_div <= (N/2)-1;col_div++){
			 if(base_table[row_div][col_div] == base_table[row_div][N-1-col_div] &&
				base_table[row_div][col_div] == base_table[N-1-row_div][col_div] &&
				base_table[row_div][col_div] == base_table[N-1-row_div][N-1-col_div]){
				 check[row_div][col_div] = true;
			 }else{
				 check[row_div][col_div] = false;
				 not_num++;
			 }
		 }
	 }

	 if(not_num == 0)count++;

	 if(C == 1){
		 printf("%d\n",count);
		 return 0;
	 }

	 stack<Info> ChangeLog;
	 not_num_work = not_num;

	 for(int loop = 1; loop <= C-1; loop++){

		 scanf("%d",&D);

		 for(int i = 0; i < D; i++){
			 scanf("%d %d",&tmp_row,&tmp_col);
			 ChangeLog.push(Info(tmp_row,tmp_col));

			 if(base_table[ChangeLog.top().row-1][ChangeLog.top().col-1] == '1'){
				 base_table[ChangeLog.top().row-1][ChangeLog.top().col-1] = '0';
			 }else{
				 base_table[ChangeLog.top().row-1][ChangeLog.top().col-1] = '1';
			 }

			 if(ChangeLog.top().row-1 >= N/2){
				 if(ChangeLog.top().col-1 >= N/2){
					 if(check[N-1-(ChangeLog.top().row-1)][N-1-(ChangeLog.top().col-1)] == true){
						 check[N-1-(ChangeLog.top().row-1)][N-1-(ChangeLog.top().col-1)] = false;
						 not_num_work++;
					 }else{
						 if(base_table[N-1-(ChangeLog.top().row-1)][N-1-(ChangeLog.top().col-1)] == base_table[N-1-(ChangeLog.top().row-1)][N-1-(N-1-(ChangeLog.top().col-1))] &&
							base_table[N-1-(ChangeLog.top().row-1)][N-1-(ChangeLog.top().col-1)] == base_table[N-1-(N-1-(ChangeLog.top().row-1))][N-1-(ChangeLog.top().col-1)] &&
							base_table[N-1-(ChangeLog.top().row-1)][N-1-(ChangeLog.top().col-1)] == base_table[N-1-(N-1-(ChangeLog.top().row-1))][N-1-(N-1-(ChangeLog.top().col-1))]){
							 check[N-1-(ChangeLog.top().row-1)][N-1-(ChangeLog.top().col-1)] = true;
							 not_num_work--;
						}else{
							 //Do nothing
						 }
					 }
				 }else{ //??????
					 if(check[N-1-(ChangeLog.top().row-1)][ChangeLog.top().col-1] == true){
						 check[N-1-(ChangeLog.top().row-1)][ChangeLog.top().col-1] = false;
						 not_num_work++;
					 }else{
						 if(base_table[N-1-(ChangeLog.top().row-1)][ChangeLog.top().col-1] == base_table[N-1-(ChangeLog.top().row-1)][N-1-(ChangeLog.top().col-1)] &&
							base_table[N-1-(ChangeLog.top().row-1)][ChangeLog.top().col-1] == base_table[N-1-(N-1-(ChangeLog.top().row-1))][ChangeLog.top().col-1] &&
							base_table[N-1-(ChangeLog.top().row-1)][ChangeLog.top().col-1] == base_table[N-1-(N-1-(ChangeLog.top().row-1))][N-1-(ChangeLog.top().col-1)]){
							 check[N-1-(ChangeLog.top().row-1)][ChangeLog.top().col-1] = true;
							 not_num_work--;
						 }else{
							 //Do nothing
						 }
					 }
				 }
			 }else{
				 if(ChangeLog.top().col-1 >= N/2){
					 if(check[ChangeLog.top().row-1][N-1-(ChangeLog.top().col-1)] == true){
						 check[ChangeLog.top().row-1][N-1-(ChangeLog.top().col-1)] = false;
						 not_num_work++;
					 }else{
						 if(base_table[ChangeLog.top().row-1][N-1-(ChangeLog.top().col-1)] == base_table[ChangeLog.top().row-1][N-1-(N-1-(ChangeLog.top().col-1))] &&
							base_table[ChangeLog.top().row-1][N-1-(ChangeLog.top().col-1)] == base_table[N-1-(ChangeLog.top().row-1)][N-1-(ChangeLog.top().col-1)] &&
							base_table[ChangeLog.top().row-1][N-1-(ChangeLog.top().col-1)] == base_table[N-1-(ChangeLog.top().row-1)][N-1-(N-1-(ChangeLog.top().col-1))]){
							 check[ChangeLog.top().row-1][N-1-(ChangeLog.top().col-1)] = true;
							 not_num_work--;
						 }else{
							 //Do nothing
						 }
					 }
				 }else{ //??????
					 if(check[ChangeLog.top().row-1][ChangeLog.top().col-1] == true){
						 check[ChangeLog.top().row-1][ChangeLog.top().col-1] = false;
						 not_num_work++;
					 }else{
						 if(base_table[ChangeLog.top().row-1][ChangeLog.top().col-1] == base_table[ChangeLog.top().row-1][N-1-(ChangeLog.top().col-1)] &&
							base_table[ChangeLog.top().row-1][ChangeLog.top().col-1] == base_table[N-1-(ChangeLog.top().row-1)][ChangeLog.top().col-1] &&
							base_table[ChangeLog.top().row-1][ChangeLog.top().col-1] == base_table[N-1-(ChangeLog.top().row-1)][N-1-(ChangeLog.top().col-1)]){
							 check[ChangeLog.top().row-1][ChangeLog.top().col-1] = true;
							 not_num_work--;
					     }else{
							 //Do nothing
						 }
					 }
				 }
			 }
		 }
		 if(not_num_work == 0)count++;
	 }

	 printf("%d\n",count);


	 return 0;
}

Measured memory_bytes: 11676