c, n = gets.split.map(&:to_i)
a = (1..n).map { gets.split("").map(&:to_i) }

d = 0
hdiff = Array.new(n).map { Array.new(n, 0) }
vdiff = Array.new(n).map { Array.new(n, 0) }
(0..n-1).each do |i|
    (0..n-1).each do |j|
        if a[i][j] != a[i][n-j-1]
            hdiff[i][j] = 1
            d += 1
        end
        if a[i][j] != a[n-i-1][j]
            vdiff[i][j] = 1
            d += 1
        end
    end
end

k = (d == 0 ? 1 : 0)
(c - 1).times do
    gets.to_i.times do
        i, j = gets.split.map(&:to_i)
        i -= 1
        j -= 1
        if hdiff[i][j] == 1
            hdiff[i][j] = hdiff[i][n-j-1] = 0
            d -= 2
        else
            hdiff[i][j] = hdiff[i][n-j-1] = 1
            d += 2
        end
        if vdiff[i][j] == 1
            vdiff[i][j] = vdiff[n-i-1][j] = 0
            d -= 2
        else
            vdiff[i][j] = vdiff[n-i-1][j] = 1
            d += 2
        end
    end
    k += 1 if d == 0
end
p k


