#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
const int INF=1e9,MOD=1e9+7,ohara=1e6+10;
const ll LINF=1e18;
using namespace std;
    
#define rep(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define rrep(i,a,b) for(int i=(a);i<(b);i++)
#define rrrep(i,a,b) for(int i=(a);i>=(b);i--)
#define all(v) (v).begin(), (v).end()
#define Size(n) (n).size()
#define Cout(x) cout<<(x)<<endl
#define doublecout(a) cout<<fixed<<setprecision(15)<<a<<endl;
#define Cerr(x) cerr<<(x)<<endl
#define fi first
#define se second
#define P pair<ll,ll> 
#define m_p make_pair
#define V vector<ll> 
#define U_MAP unordered_map<ll,ll>
#define pq priority_queue<ll>
#define rpq priority_queue<ll,vector<ll>,greater<ll>>
#define p_b push_back
    
ll n,cnt,ans,a,b,c,d,tmp,tmpp,m,h,w,x,y,sum,pos,k;
ld doua;
int dy[]={1,0,-1,0};
int dx[]={0,1,0,-1};
//int dy[]={-1,0,1,-1,1,-1,0,1};
//int dx[]={-1,-1,-1,0,0,1,1,1};
string alph("abcdefghijklmnopqrstuvwxyz");
char s[2000][2000];
bool fl;
struct edge{int to,cost;};
ll dat[2000][2000];
    
//-------------------------↓↓↓↓↓↓------------------------
    
//------ 自分を天才だと信じる ------
    
int main(void){
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    
    cin>>c>>n;
    rrep(i,1,n+1)rrep(j,1,n+1)cin>>s[i][j];
    int ok=1;
    int judge=1;
    ll want=0;
    rrep(i,1,n/2+1){
        rrep(j,1,n/2+1){
            ok=1;
            if(s[i][j]!=s[i][n-j+1])ok=0;
            else if(s[i][j]!=s[i+(n/2-i)*2+1][j])ok=0;
            else if(s[i][j]!=s[i+(n/2-i)*2+1][j+(n/2-j)*2+1])ok=0;
            else if(s[i][j+(n/2-j)*2+1]!=s[i+(n/2-i)*2+1][j])ok=0;
            else if(s[i][j+(n/2-j)*2+1]!=s[i+(n/2-i)*2+1][j+(n/2-j)*2+1])ok=0;
            else if(s[i+(n/2-i)*2+1][j]!=s[i+(n/2-i)*2+1][j+(n/2-j)*2+1])ok=0;
            if(!ok){
                judge=0;
                want++;
            }
        }
    }
    if(judge)ans=1;
    while(1){
        if(c==1)break;
         c--;
        cin>>d;
        rep(i,d){
            cin>>a>>b;
            if(a>n/2)tmp=n-a+1;
            else tmp=a;
            if(b>n/2)tmpp=n-b+1;
            else tmpp=b;

            int ok1=1;
            if(s[tmp][tmpp]!=s[tmp][tmpp+(n/2-tmpp)*2+1])ok1=0;
            else if(s[tmp][tmpp]!=s[tmp+(n/2-tmp)*2+1][tmpp])ok1=0;
            else if(s[tmp][tmpp]!=s[tmp+(n/2-tmp)*2+1][tmpp+(n/2-tmpp)*2+1])ok1=0;
            else if(s[tmp][tmpp+(n/2-tmpp)*2+1]!=s[tmp+(n/2-tmp)*2+1][tmpp])ok1=0;
            else if(s[tmp][tmpp+(n/2-tmpp)*2+1]!=s[tmp+(n/2-tmp)*2+1][tmpp+(n/2-tmpp)*2+1])ok1=0;
            else if(s[tmp+(n/2-tmp)*2+1][tmpp]!=s[tmp+(n/2-tmp)*2+1][tmpp+(n/2-tmpp)*2+1])ok1=0;

            if(s[a][b]=='1')s[a][b]='0';
            else s[a][b]='1';

            int ok2=1;
           if(s[tmp][tmpp]!=s[tmp][tmpp+(n/2-tmpp)*2+1])ok2=0;
            else if(s[tmp][tmpp]!=s[tmp+(n/2-tmp)*2+1][tmpp])ok2=0;
            else if(s[tmp][tmpp]!=s[tmp+(n/2-tmp)*2+1][tmpp+(n/2-tmpp)*2+1])ok2=0;
            else if(s[tmp][tmpp+(n/2-tmpp)*2+1]!=s[tmp+(n/2-tmp)*2+1][tmpp])ok2=0;
            else if(s[tmp][tmpp+(n/2-tmpp)*2+1]!=s[tmp+(n/2-tmp)*2+1][tmpp+(n/2-tmpp)*2+1])ok2=0;
            else if(s[tmp+(n/2-tmp)*2+1][tmpp]!=s[tmp+(n/2-tmp)*2+1][tmpp+(n/2-tmpp)*2+1])ok2=0;

            if(ok1==ok2)continue;
            if(ok1==0)want--;
            else want++;
        }
        if(want==0)ans++;
    }
    Cout(ans);
    return 0;
}


Measured memory_bytes: 5184