#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef string str;
#define down_queue(x) priority_queue<x>
#define up_queue(x) priority_queue<x, vector<x>, greater<x>>
#define vec(x) vector<x>
ll gcd(ll a,ll b){while(b){ll tmp = a % b;a = b;b = tmp;}return a;}
ll lcm(ll a,ll b){return a / gcd(a,b) * b;}
#define rep(x, y) for(int x = 0; x < (y); x++)
#define rrep(x, y) for(int x = (y); x >= 0; x--)
#define REP(x, z, y) for(int x = (z); x < (y); x++)
#define RREP(x, z, y) for(int x = (y); x >= (z); x--)
#define all(x) x.begin(), x.end()
#define split_str(str, sp_word) istringstream stream(str); string res; for(int cnt = 0; getline(stream,res,sp_word); cnt++)
#define digit(x) ((int)log10((double)(x)) + 1)
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define out(x) for(auto i : x) cout << i << " "; cout << endl;
#define outln(x) for(auto i : x) cout << i << " " << endl;
#define vec_cpy(to,from) copy(all(to),back_inserter(from))
#define ary_cpy(to,from) memcpy(to, from, sizeof from)
#define MOD = 1000000007; 
#ifdef DEBUG
#define debug_echo(e) cout << "|" << __LINE__ << "| " << e << endl
#define debug_var(e) cout << "|" << __LINE__ << "| " << #e << ": " << e << endl
#define debug_echo_dp(dp, N) { cout << "[ "; rep(cnt, N) cout << dp[cnt] << " ";  cout << "]" << endl; }
#else
#define debug_echo(e)
#define debug_var(e)
#define debug_echo_dp(dp, N)
#endif

int main(){
  int C,n,d,r,c,ans = 0; cin >> C >> n;
  int harf = n / 2;
  int h_cnt = 0;
  int w_cnt = 0;
  vec(bool) h[harf],w[n],h_goal(n,true),w_goal(harf,true);
  str p[n];

  rep(i,n) cin >> p[i];

  //????????????????????¨
  rep(i,harf)
    rep(j,n){
      h[i].pb(p[i][j] == p[n - i - 1][j]);
      if(p[i][j] != p[n - i - 1][j]) h_cnt++;
    }

  //?¨??????????????????¨
  rep(i,n)
    rep(j,harf){
      w[i].pb(p[i][j] == p[i][n - j - 1]);
      if(p[i][j] != p[i][n - j - 1]) w_cnt++;
    }

  if(w_cnt == 0 && h_cnt == 0) ans++;

  rep(i,C - 1){
    cin >> d;
    rep(i,d){
      cin >> r >> c;
      r--; c--;

      if(h[(r >= harf ? n - r - 1 : r)][c]) h_cnt++;
      else h_cnt--;

      if(w[r][(c >= harf ? n - c - 1 : c)]) w_cnt++;
      else w_cnt--;

      //????????¨???????????????????????¢
      h[(r >= harf ? n - r - 1 : r)][c] = !h[(r >= harf ? n - r - 1 : r)][c];
      w[r][(c >= harf ? n - c - 1 : c)] = !w[r][(c >= harf ? n - c - 1 : c)];
    } 

    if(h_cnt == 0 && w_cnt == 0) ans++;
  }

  cout << ans << endl;
}