#include <cstdio>
#include <vector>
#include <queue>

int main()
{
	int N, M, K;
	scanf("%d%d%d", &N, &M, &K);
	std::vector<std::vector<int> > A(N+2, std::vector<int>(M+2, 0));
	std::vector<std::vector<bool> > bA(N+2, std::vector<bool>(M+2, false));
	for(int i = 0, j = N + 2, k = M + 1; i < j; i++)
	{
		bA[i][0] = true;
		bA[i][k] = true;
	}
	for(int i = 0, j = M + 2, k = N + 1; i < j; i++)
	{
		bA[0][i] = true;
		bA[k][i] = true;
	}
	int r, c;
	for(int i = 0; i < K; i++)
	{
		scanf("%d%d", &r, &c);
		bA[r][c] = true;
	}
	int boja = 1;
	for(r = 1; r <= N; r++) for(c = 1; c <= M; c++) if(!bA[r][c] && A[r][c] == 0)
	{
		std::queue<int> red;
		red.push(r);
		red.push(c);
		bA[r][c] = true;
		A[r][c] = boja;
		int x, y, tx, ty;
		int smerX[] = {-1, 0, 1, 0};
		int smerY[] = {0, -1, 0, 1};
		while(!red.empty())
		{
			y = red.front(); red.pop();
			x = red.front(); red.pop();
			for(int i = 0; i < 4; i++)
			{
				tx = x + smerX[i];
				ty = y + smerY[i];
				if(!bA[ty][tx])
				{
					red.push(ty);
					red.push(tx);
					A[ty][tx] = boja;
					bA[ty][tx] = true;
				}
			}
		}
		boja++;
	}
	printf("%d", boja-1);
	return 0;
}