


class MonoImage(private val state: Array<BooleanArray>){
    val size: Int = state.size
    private var mNotSymmetricPixelCount: Int
    private val mPixels: Array<Array<Pixel>>
    init{
        mPixels = Array(size / 2){Array(size / 2){Pixel()}}
        mNotSymmetricPixelCount = 0
        for (i in 0 until size / 2){
            for (j in 0 until size / 2){
                mPixels[i][j].topLeft = state[i][j]
                mPixels[i][j].topRight = state[i][size - 1 - j]
                mPixels[i][j].bottomLeft = state[size - 1 - i][j]
                mPixels[i][j].bottomRight = state[size - 1 - i][size - 1 - j]
                if (!mPixels[i][j].isSymmetric) ++mNotSymmetricPixelCount
            }
        }
    }
    val isSymmetric: Boolean
        get() = mNotSymmetricPixelCount == 0
    private fun pixel(row: Int, column: Int): Pixel {
        return when{
            row in 0 until size / 2 && column in 0 until size / 2 -> mPixels[row][column]
            row in 0 until size / 2 -> mPixels[row][size - column - 1]
            column in 0 until size / 2 -> mPixels[size - row - 1][column]
            else -> mPixels[size - row - 1][size - column - 1]
        }
    }
    fun flip(row: Int, column: Int): Unit {
        if (!pixel(row - 1, column - 1).isSymmetric) --mNotSymmetricPixelCount
        when{
            (row - 1) in 0 until size / 2 && (column - 1) in 0 until size / 2 -> pixel(row - 1, column - 1).flipTopLeft()
            (row - 1) in 0 until size / 2 -> pixel(row - 1, column - 1).flipTopRight()
            (column - 1) in 0 until size / 2 -> pixel(row - 1 , column - 1).flipBottomLeft()
            else -> pixel(row - 1, column - 1).flipBottomRight()
        }
        if (!pixel(row - 1, column - 1).isSymmetric) ++mNotSymmetricPixelCount

    }
    class Pixel(var topLeft: Boolean = false, var topRight: Boolean = false, var bottomLeft: Boolean = false, var bottomRight: Boolean = false){
        val isSymmetric: Boolean
            get() = (topLeft == topRight) && (topLeft == bottomLeft) && (bottomLeft == bottomRight)
        fun flipTopLeft(): Unit {topLeft = !topLeft}
        fun flipTopRight(): Unit {topRight = !topRight}
        fun flipBottomLeft(): Unit {bottomLeft = !bottomLeft}
        fun flipBottomRight(): Unit {bottomRight = !bottomRight}
    }

}
fun main(args:Array<String>): Unit {
    val (c, n) = readLine()!!.trim().split(' ').map(String::toInt)
    val image = MonoImage(Array(n){readLine()!!.trim().toCharArray().map{it == '1'}.toBooleanArray()})
    var count = if (image.isSymmetric) 1 else 0
    repeat(c - 1){
        repeat(readLine()!!.trim().toInt()){
            val (row, column) = readLine()!!.trim().split(' ').map(String::toInt)
            image.flip(row, column)
        }
        if (image.isSymmetric) ++count
    }
    println(count)
}
