#include <bits/stdc++.h>

using namespace std;

#define int long long 
#define INF 1e9
#define LINF 1e18

typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<int, P> Pi;
typedef pair<P, P> PP;

const int MOD = 1e9 + 7;
const int dy[] = { 0, 0, 1, -1 };
const int dx[] = { 1, -1, 0, 0 };

template <class T> void chmin(T& a, const T& b) { a = min(a, b); }
template <class T> void chmax(T& a, const T& b) { a = max(a, b); }

int C, N;
string s[1001];
bool flag[500][500];

bool check(int y, int x) {
    return (s[y][x] == s[N - y - 1][x] && s[y][x] == s[y][N - x - 1] && s[y][x] == s[N - y - 1][N - x - 1]);
}

signed main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> C >> N;
    for(int i  = 0; i < N; ++i) cin >> s[i];
    int res = 0, cnt = 0;
    for(int i = 0; i < N / 2; ++i) {
        for(int j = 0; j < N / 2; ++j) {
            if(!check(i, j)) ++cnt;
            else flag[i][j] = true;
        }
    }
    if(cnt == 0) ++res;
    for(int i = 0; i < C - 1; ++i) {
        int d;
        cin >> d;
        for(int j = 0; j < d; ++j) {
            int y, x;
            cin >> y >> x;
            --y; --x;
            s[y][x] = (s[y][x] == '0') ? '1' : '0';
            if(y >= N / 2) y = N - y - 1;
            if(x >= N / 2) x = N - x - 1;
            if(check(y, x) && !flag[y][x]) {
                flag[y][x] = true;
                --cnt;
            }
            else if(!check(y, x) && flag[y][x]) {
                flag[y][x] = false;
                ++cnt;
            }
        }
        if(cnt == 0) ++res;
    }
    cout << res << endl;
    return 0;
}

