#include "bits/stdc++.h"
#define in std::cin
#define out std::cout
#define rep(i,N) for(int i=0;i<N;++i)
typedef long long int LL;

int C, N, asum, bsum, ans;

int main()
{
	in >> C >> N;
	std::vector<std::vector<int>>p(N, std::vector<int>(N));
	std::vector<std::vector<int>>A(N, std::vector<int>(N / 2));
	std::vector<std::vector<int>>B(N / 2, std::vector<int>(N));
	rep(i, N)
	{
		std::string temp;
		in >> temp;
		rep(j, N) p[i][j] = temp[j] - '0';
	}
	rep(i, N) rep(j, N) A[i][std::min(j, N - j - 1)] ^= p[i][j];
	rep(i, N) rep(j, N) B[std::min(i, N - i - 1)][j] ^= p[i][j];
	rep(i, N) rep(j, N / 2) asum += A[i][j];
	rep(i, N / 2) rep(j, N) bsum += B[i][j];
	if (asum == 0 && bsum == 0) ++ans;
	rep(loop, C - 1)
	{
		int num, x, y;
		in >> num;
		rep(i, num)
		{
			in >> x >> y;
			--x; --y;
			A[x][std::min(y, N - y - 1)] ^= 1;
			B[std::min(x, N - x - 1)][y] ^= 1;
			if (A[x][std::min(y, N - y - 1)] == 0) --asum;
			else ++asum;
			if (B[std::min(x, N - x - 1)][y] == 0) --bsum;
			else ++bsum;
		}
		if (asum == 0 && bsum == 0) ++ans;
	}
	out << ans << std::endl;
	return 0;
}

