#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<string>
#include<map>
using namespace std;
int coaster[1005][1005]={0};
int main(){
  int c,n;
  cin>>c>>n;
  int ts=0,ans=0;
  for(int i=0;i<n;i++){
    string s;
    cin>>s;
    for(int j=0;j<n;j++) coaster[i][j]=(s[j]=='0'?0:1);
  }
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      if(coaster[i][j]!=coaster[i][(n-1)-j])  ts++;
      if(coaster[i][j]!=coaster[(n-1)-i][j])  ts++;
    }
  }
  ts/=2;
  if(ts==0) ans++;
  for(int q=1;q<c;q++){
    int m;
    cin>>m;
    for(int i=0;i<m;i++){
      int x,y;
      cin>>x>>y;
      x--;
      y--;
      coaster[x][y]=(coaster[x][y]+1)%2;
      if(coaster[x][y]==coaster[x][(n-1)-y]) {
        ts--;
      }
      else{
        ts++;
      }
      if(coaster[x][y]==coaster[(n-1)-x][y])  ts--;
      else ts++;
    }
    if(ts==0){
      ans++;
    }
  }
  cout<<ans<<endl;
}

