#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> PA;
char p[1005][1005];
int main(){
  int c,n,d,a,b;
  scanf("%d %d",&c,&n);
  for(int i=0;i<n;i++){
    scanf("%s",p[i]);
  }
  
  int joge=0,sayu=0;
  for(int i=0;i<n/2;i++){
    for(int j=0;j<n;j++){
      if(p[i][j] != p[n-1-i][j]) joge++;
    }
  }
  
  for(int i=0;i<n;i++){
    for(int j=0;j<n/2;j++){
      if(p[i][j] != p[i][n-1-j]) sayu++;
    }
  }
  
  int sum = 0;
  if(joge == 0 && sayu == 0) sum++;

  for(int i=0;i<c-1;i++){
    scanf("%d",&d);
    for(int j=0;j<d;j++){
      scanf("%d %d",&a,&b);
      a--;
      b--;
      if(p[a][b] == '0') p[a][b] = '1';
      else if(p[a][b] == '1') p[a][b] = '0';
      
      if(p[a][b] == p[n-1-a][b]) joge--;
      else if(p[a][b] != p[n-1-a][b]) joge++;
      
      if(p[a][b] == p[a][n-1-b]) sayu--;
      else if(p[a][b] != p[a][n-1-b]) sayu++;
      
    }
    if(joge == 0 && sayu == 0) sum++;
  }
  printf("%d\n",sum);
  return(0);
}

