#include <stdio.h>
#include <set>
#include <utility>
#define rep(i, n) for(int i = 0; i < n; ++i)
using namespace std;

int main(void) {
  int c, n, i, j, k, ans = 0;
  scanf("%d%d", &c, &n);
  int m = n / 2;
  set< pair<int, int> > s;
  char p[n][n + 10], num[m][m];
  rep(i, n) scanf("%s", p[i]);
  rep(i, n) rep(j, n) p[i][j] -= '0';
  rep(i, m) rep(j, m) num[i][j] = 0;
  rep(i, n) rep(j, n){
    if(p[i][j]) {
      int t = i, r = j;
      if(i >= m) t = n - i - 1;
      if(j >= m) r = n - j - 1;
      num[t][r]++;
    }
  }
  rep(i, m) rep(j, m) if(num[i][j] && num[i][j] != 4) {
    pair<int,int> p = make_pair(i, j);
    s.insert(p);
  }
  if(s.empty()) ans++;
  rep(i, c - 1) {
    int d;
    scanf("%d", &d);
    rep(j, d) {
      int r, c, ss = 0;
      scanf("%d%d", &r, &c);
      r--, c--;
      if(p[r][c]) {
        ss = 1;
        p[r][c] = 0;
      } else {
        p[r][c] = 1;
      }
      if(r >= m) r = n - r - 1;
      if(c >= m) c = n - c - 1;
      pair<int, int> p = make_pair(r, c);
      if(ss) {
        num[r][c]--;
      } else {
        num[r][c]++;
      }
      set< pair<int, int> >::iterator it = s.find(p);
      if(num[r][c] && num[r][c] != 4) {
        if(it == s.end()) s.insert(p);
      } else {
        if(it != s.end()) s.erase(it);
      }
    }
    if(s.empty()) ++ans;
  }
  printf("%d\n", ans);
  return 0;
}
