import java.util.*

data class Mask(val x1:Int, val y1:Int, val x2:Int, val y2:Int){
    constructor(vararg nums:Int):this(nums[0], nums[1], nums[2], nums[3]){}
}
class CoordinateCompresser(coordinates: IntArray){
    private var mArray = coordinates.distinct().sorted().toIntArray()
    operator fun get(coord:Int):Int {
        return mArray.binarySearch(coord)
    }
}

class Map(val height:Int, val width:Int, val masks:Iterator<Mask>){
    private val mArray = Array(height){Array(width){0}}.apply {
        masks.forEach { (x1, y1, x2, y2) ->
            this[y1][x1] += 1
            if (x2 in 0 until width) this[y1][x2] += -1
            if (y2 in 0 until height) this[y2][x1] += -1
            if (x2 in 0 until width && y2 in 0 until height) this[y2][x2] += 1
        }
        for (y in 0 until height){
            for (x in 1 until width){
                this[y][x] += this[y][x - 1]
            }
        }
        for (y in 1 until height){
            for (x in 0 until width){
                this[y][x] += this[y - 1][x]
            }
        }
    }
    private var mAreaCount:Int? = null
    fun areaCount():Int{
        if (mAreaCount == null) mAreaCount = calAreaCount()
        return mAreaCount!!
    }
    private fun calAreaCount():Int {
        var count = 0
        for (x in 0 until width)
            for (y in 0 until height){
                when(mArray[y][x]){
                    0 ->{
                        ++count
                        fillArea(x, y)
                    }
                }
            }
        return count
    }
    private fun fillArea(x:Int, y:Int):Unit{
        val stack = Stack<Point>().apply { push(Point(x, y)) }
        while (stack.size != 0){
            for ((a, b) in stack.pop().neighbor()){
                if (a in 0 until width && b in 0 until height && mArray[b][a] == 0){
                    mArray[b][a] = 1
                    stack.push(Point(a, b))
                }
            }
        }
    }
    private data class Point(val x:Int, val y:Int){
        fun neighbor():List<Point> = listOf(Point(x + 1, y), Point(x - 1, y), Point(x, y + 1), Point(x, y - 1))
    }
}
fun solve(w:Int, h:Int):Unit{
    if (h == 0 && w == 0) return

    Array(readLine()!!.toInt()){
        Mask(*(readLine()!!.split(' ').map(String::toInt).toIntArray()))
    }.let{
        val xCompresser = CoordinateCompresser(it.flatMap {mask -> listOf(mask.x1, mask.x2) }.plus(0).plus(w).toIntArray())
        val yCompresser = CoordinateCompresser(it.flatMap { mask -> listOf(mask.y1, mask.y2)}.plus(0).plus(h).toIntArray())
        println(Map(yCompresser[h], xCompresser[w], it.map{mask -> Mask(xCompresser[mask.x1], yCompresser[mask.y1], xCompresser[mask.x2], yCompresser[mask.y2])}.iterator()).areaCount())
    }
    val (nw, nh) = readLine()!!.split(' ').map(String::toInt)
    solve(nw, nh)
}
fun main(args:Array<String>):Unit{
    val (w, h) = readLine()!!.split(' ').map(String::toInt)
    solve(w, h)
}
