c, n = gets.split.map(&:to_i)
a = (1..n).map { gets.chomp.reverse.to_i(2) }
f = -> a {
    a.all? {|x|
        x == sprintf("%0#{n}b", x).reverse.to_i(2)
    }
}
g = -> a {
    (0..(n-2)/2).all? {|i| a[i] == a[n-i-1]}
}
k = 0
c.times do
    gets.to_i.times do
        y, x = gets.split.map(&:to_i)
        a[y-1] ^= (1 << (x - 1))
    end
    k += 1 if f[a] && g[a]
end
p k
