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

bool table[1000][1000];

int main() {

    int c,n;

    int count = 0;

    int ans = 0;

    cin >> c >> n;

    char ch;

    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            scanf(" %c",&ch);
            if(ch=='0'){
                table[i][j] = false;
            }
            else{
                table[i][j] = true;
            }
        }
    }

    for(int i=0;i<n/2;i++){
        for(int j=0;j<n/2;j++){
            if(table[i][j] != table[i][n-1-j] || table[i][j] != table[n-1-i][j] || table[i][j] != table[n-1-i][n-1-j]){
                count++;
            }
        }
    }

    if(count == 0){
        ans++;
    }

    for(int i=0;i<c-1;i++){
        int d;
        scanf(" %d",&d);

        for(int j=0;j<d;j++){
            int r,x;
            scanf(" %d %d",&r,&x);
            r--;x--;
            bool check = true;
            if(table[r][x] != table[r][n-1-x] || table[r][x] != table[n-1-r][x] || table[r][x] != table[n-1-r][n-1-x]){
                check = false;
            }
            table[r][x] = !table[r][x];
            if(table[r][x] != table[r][n-1-x] || table[r][x] != table[n-1-r][x] || table[r][x] != table[n-1-r][n-1-x]){
                if(check){
                    count++;
                }
            }
            else{
                count--;
            }
        }
        if(count == 0){
            ans++;
        }

    }

    cout << ans << endl;



    return 0;
}