#include <iostream>
#include <cstdlib>

void floodFill(int *arr, int width, int height, int x, int y, int color) {
	if (arr[y * width + x] == 0) {
		arr[y * width + x] = color;
		if (x > 0)
			floodFill(arr, width, height, x - 1, y, color);
		if (x < width - 1)
			floodFill(arr, width, height, x + 1, y, color);
		if (y > 0)
			floodFill(arr, width, height, x, y - 1, color);
		if (y < height - 1)
			floodFill(arr, width, height, x, y + 1, color);
	}
}

int main() {
	std::ios_base::sync_with_stdio(false);
	
	int width, height, n;
	std::cin >> width >> height >> n;

	int *arr = (int*)calloc(width * height, sizeof(int));

	for (int i = 0; i < n; ++i) {
		int x, y;
		std::cin >> x >> y;
		arr[(y - 1) * width + x - 1] = 1;
	}

	int color = 1;
	for (int x = 0; x < width; x++)
		for (int y = 0; y < height; y++)
			if (arr[y * width + x] == 0)
				floodFill(arr, width, height, x, y, ++color);

	std::cout << --color; //because color starts at 1
	/*
	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++)
			std::cout << arr[y * width + x] << " ";
		std::cout << "\n\n";
	}
	//*/
	delete[] arr;
	return 0;
}