#include <bits/stdc++.h>
int color = 2;
int N,M,K;
int table[1000][1000];
void oboji(int a,int b,int table[][1000]){
table[a][b] = color;
if(a!=0){
    if(table[a-1][b]== 0)
        oboji(a-1,b,table);
}
if(a!=N-1){
    if(table[a+1][b]==0)
        oboji(a+1,b,table);
}
if(b!=0){
    if(table[a][b-1]==0)
        oboji(a,b-1,table);
}
if(b!=M-1){
    if(table[a][b+1]==0)
        oboji(a,b+1,table);
}

}
int main(){


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

for(int i = 0; i < N; ++i){
    for(int j = 0; j < M; ++j){
        table[i][j] = 0;
    }
}

int a,b;

for(int i = 0; i < K; ++i){
    scanf("%d%d",&a,&b);
    table[a-1][b-1] = 1;
}
for(int i = 0; i < N; ++i){
    for(int j = 0; j < M; ++j){
        if(table[i][j] == 0){
            oboji(i,j,table);
            color++;
        }
    }
}
/*for(int i = 0; i < N; ++i){
    for(int j = 0; j < M; ++j){
        printf("%d ",table[i][j]);
    }
    printf("\n");
*/
printf("%d",color-2);
return 0;
}