#include <iostream>
#include <vector>

std::pair<int, int> get_point(int i, int j, int d, int N)
{
    std::pair<int, int> p[] = {
        {i, N - j - 1},
        {N - i - 1, j},
        {N - i - 1, N - j - 1}
    };
    return p[d];
}

int main()
{
    int C, N;
    std::cin >> C >> N;
    
    int count = 0;
    std::vector<std::vector<int>> p(N, std::vector<int>(N));
    
    for (int i = 0; i < N; i++) {
        std::string in;
        std::cin >> in;
        for (int j = 0; j < N; j++) {
            p[i][j] = in[j] - '0';
            count += 6;
        }
    }

    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            for (int k = 0; k < 3; k++) {
                auto np = get_point(i, j, k, N);
                int ni = np.first, nj = np.second;
                if (p[i][j] == p[ni][nj]) {
                    count -= 2;
                }
            }
        }
    }

    int res = count == 0;
    for (int i = 0; i < C - 1; i++) {
        int D;
        std::cin >> D;
        for (int j = 0; j < D; j++) {
            int r, c;
            std::cin >> r >> c;
            r--; c--;
            p[r][c] = p[r][c] ^ 1;
            for (int k = 0; k < 3; k++) {
                auto np = get_point(r, c, k, N);
                int ni = np.first, nj = np.second;
                if (p[r][c] == p[ni][nj]) {
                    count -= 4;
                } else {
                    count += 4;
                }
            }            
        }

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

    std::cout << res << std::endl;

    return 0;
}