#include<cstdio>
using namespace std;
long long a[10000][10000],n,m,br;

void p(long i,long j)
{
    a[i][j]=br;
    if (a[i+1][j]==0) p(i+1,j);
    if (a[i-1][j]==0) p(i-1,j);
    if (a[i][j+1]==0) p(i,j+1);
    if (a[i][j-1]==0) p(i,j-1);
    return;
}

int main()
{
    long long k;
    scanf("%lld%lld%lld",&n,&m,&k);
    br=1;
    for (long long i=1;i<=n;i++)
        for (long long j=1;j<=m;j++) a[i][j]=0;
    for (long long i=0;i<=n+1;i++)
    {
        a[i][0]=-1;
        a[i][m+1]=-1;
    }
    for (long long i=1;i<=m;i++)
    {
        a[0][i]=-1;
        a[n+1][i]=-1;
    }
    long long c,d;
    for (long long i=1;i<=k;i++)
    {
        scanf("%lld%lld",&c,&d);
        a[c][d]=1;
    }
    for (long long i=1;i<=n;i++)
        for (long long j=1;j<=m;j++)
        {
            if (a[i][j]==0)
                {
                    br++;
                    p(i,j);
                }
        }
    printf("%lld",br-1);
    return 0;
}