C, N = gets.split.map &:to_i
p = N.times.map{gets.chomp}

bilateral = Array.new(N){Array.new(N/2)}
diphycercal = Array.new(N/2){Array.new(N)}
b = 0
d = 0
(N/2).times do |i|
  N.times do |j|
    bilateral[j][i] = p[j][i] == p[j][N-1-i]
    diphycercal[i][j] = p[i][j] == p[N-1-i][j]
    b += 1 unless bilateral[j][i]
    d += 1 unless diphycercal[i][j]
  end
end

ans = 0
ans += 1 if b.zero? && d.zero?

(C-1).times do
  gets.to_i.times do
    r, c = gets.split.map &:to_i
    r -= 1
    c -= 1
    rr = r < N/2 ? r : N - 1 - r
    cc = c < N/2 ? c : N - 1 - c
    b += (bilateral[r][cc] ? 1 : -1)
    d += (diphycercal[rr][c] ? 1 : -1)
    bilateral[r][cc] = !bilateral[r][cc]
    diphycercal[rr][c] = !diphycercal[rr][c]
  end
  ans += 1 if b.zero? && d.zero?
end
p ans