module Graphical
	require 'set'

	def reachable_from(s, max_depth = nil)
		reached = Set[s]
		queue = [s]
		depth = 0
		loop {
			break if max_depth && depth >= max_depth
			next_queue = []
			until queue.empty?
				u = queue.shift
				each_out_connection_of(u){|v, _|
					next if reached.include?(v)
					reached << v
					next_queue << v
				}
			end
			break if next_queue.empty?
			queue = next_queue
			depth += 1
		}
		return reached
	end

	def bfs(start)
		raise "start must be Enumerable" if !start.kind_of?(Enumerable)
		reached = Set[*start.to_a]
		queue = []
		start.each{|s| queue << s}
		dist = {}
		loop.with_index {|_, depth|
			next_queue = []
			until queue.empty?
				u = queue.shift
				dist[u] = depth
				each_out_connection_of(u){|v, _|
					next if reached.include?(v)
					reached << v
					next_queue << v
				}
			end
			break if next_queue.empty?
			queue = next_queue
		}
		return dist
	end

	def dijkstra(start)
		raise "start must be Enumerable" if !start.kind_of?(Enumerable)
		h = Heap.new
		reached = Set[*start.to_a]
		start.each{|s| h.add(s, 0)}

		dist = {}
		until h.empty?
			u, key = h.del_min
			return key if block_given? && yield(u, key)
			dist[u] = key
			each_out_connection_of(u){|v, l|
				d = dist[u] + l
				if reached.include?(v)
					h.update_key(v, d)
				else
					h.add(v, d)
					reached << v
				end
			}
		end
		block_given? ? nil : dist
	end

	def bidir_dijkstra(s, t)
		f_dist = {}
		b_dist = {}

		f_heap = Heap.new
		b_heap = Heap.new

		f_heap.add(s, 0)
		b_heap.add(t, 0)

		f_reached = Set[s]
		b_reached = Set[t]

		f_current_dist = b_current_dist = 0
		best = Float::INFINITY

		loop {
			if !f_heap.empty?
				u, key = f_heap.del_min
				f_dist[u] = key
				f_current_dist = key
				each_out_connection_of(u){|v, l|
					d = f_dist[u] + l
					if b_dist[v]
						path_len = d + b_dist[v]
						best = path_len if path_len < best
					end
					if f_reached.include?(v)
						f_heap.update_key(v, d)
					else
						f_heap.add(v, d)
						f_reached << v
					end
				}
			end

			if !b_heap.empty?
				u, key = b_heap.del_min
				b_dist[u] = key
				b_current_dist = key
				each_in_connection_of(u){|v, l|
					d = b_dist[u] + l
					if f_dist[v]
						path_len = d + f_dist[v]
						best = path_len if path_len < best
					end
					if b_reached.include?(v)
						b_heap.update_key(v, d)
					else
						b_heap.add(v, d)
						b_reached << v
					end
				}
			end

			return best if f_current_dist + b_current_dist >= best
			return nil if f_heap.empty? && b_heap.empty?
		}
	end

	def a_star(start)
		raise "a_star need termination condition" unless block_given?
		h = Heap.new
		reached = Set[start]
		h.add(start, [a_star_heuristic(start), 0])

		dist = {}
		until h.empty?
			u, (e, d) = h.del_min
			return d if yield(u, dist)
			dist[u] = d
			each_out_connection_of(u){|v, l|
				d = dist[u] + l
				e = d + a_star_heuristic(v)
				if reached.include?(v)
					h.update_key(v, [e, d])
				else
					h.add(v, [e, d])
					reached << v
				end
			}
		end
		block_given? ? nil : dist
	end

	#make_variations :bidir_dijkstra

	def prim(start)
		h = Heap.new
		h.add(start, 0)
		reached = Set[start]
		tree_len = 0

		until h.empty?
			u, key = h.del_min
			tree_len += key

			each_connection_of(u){|v, d|
				if reached.include?(v)
					h.update_key(v, d)
				else
					h.add(v, d)
					reached << v
				end
			}
		end
		tree_len
	end
end

module Grid
	def each_neighbour(u)
		y, x = u
		[[-1,0],[1,0],[0,-1],[0,1]].each{|dy, dx|
			i, j = y+dy, x+dx
			yield [i, j] if (0..self.size-1) === i && (0..self[0].size-1) === j
		}
	end
end

require 'set'

loop {
	w, h = gets.split.map &:to_i
	break if w == 0
	
	used_x = Set[0, w]
	used_y = Set[0, h]
	
	n = gets.to_i
	rects = []
	n.times {
		rects << gets.split.map(&:to_i)
		x1, y1, x2, y2 = rects[-1]
		used_x << x1
		used_x << x2
		used_y << y1
		used_y << y2
	}
	
	x_map = {}
	used_x.to_a.sort.each_with_index{|x, i| x_map[x] = i}
	y_map = {}
	used_y.to_a.sort.each_with_index{|y, i| y_map[y] = i}
	
	a = (0..y_map.size-2).map{ [0] * (x_map.size-1) }
	
	rects.each{|x1, y1, x2, y2|
		(y_map[y1]..y_map[y2]-1).each{|i|
			(x_map[x1]..x_map[x2]-1).each{|j|
				a[i][j] = 1
			}
		}
	}
	
	a.extend Graphical
	a.extend Grid
	def each_out_connection_of(u)
		each_neighbour(u){|v|
			yield [v, 1] if self[v[0]][v[1]] == 0
		}
	end
	
	count = 0
	(0..y_map.size-2).each{|i|
		(0..x_map.size-2).each{|j|
			if a[i][j] == 0	
				count += 1
				a.reachable_from([i,j]).each{|u, v|
					
					a[u][v] = 1
				}
			end	
		}
	}
	p count
}

Measured memory_bytes: 33892