#include <bits/stdc++.h>

using namespace std;

int A[10000][10000];
int boja = 1;
int N,M,K;
void Oboji(int x,int y)
{
    A[x][y] = boja;
    //Ako izadje prestani
    if(x == 0 || y == 0)
        return;

    // Rekurzivno boji susedna
    if(x+1 <= N && A[x+1][y] == 0)
    {
        Oboji(x+1,y);
    }
    if(y+1 <= M && A[x][y+1] == 0)
    {
        Oboji(x,y+1);
    }
    if(x-1 >= 0 && A[x-1][y] == 0)
    {
        Oboji(x-1,y);
    }
    if(y-1 >= 0 && A[x][y-1] == 0)
    {
        Oboji(x,y-1);
    }

}


int main()
{

    scanf("%d %d %d",&N,&M,&K);

    for(int i = 0;i<K;i++)
    {
        int x,y;
        scanf("%d %d",&x,&y);
        A[x][y] = 1;
    }

    for(int i = 1;i<=N;i++)
    {
        for(int j = 1;j<=M;j++)
        {
            if(A[i][j] == 0)
            {
                // Oboji ovo i susedna
                boja++;
                Oboji(i,j);
            }
        }
    }

    printf("%d",boja-1);

    return 0;
}