#include<bits/stdc++.h>
using namespace std;
int mat[1010][1010];
void bfs(int x, int y,int boja,int n,int m)
{
    mat[x][y]=boja;
    if(mat[x+1][y]==0 && x+1<=n){bfs(x+1,y,boja,n,m);}
    if(mat[x-1][y]==0 && x-1>=1){bfs(x-1,y,boja,n,m);}
    if(mat[x][y+1]==0 && y+1<=m){bfs(x,y+1,boja,n,m);}
    if(mat[x][y-1]==0 && y-1>=1){bfs(x,y-1,boja,n,m);}
}
int main()
{
    int n,m,k,x,y,boja=1;
    cin>>n>>m>>k;
    for(int i=0;i<k;i++)
    {
        cin>>x>>y;
        mat[x][y]=-1;
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(mat[i][j]==0){bfs(i,j,boja,n,m);boja+=1;}
        }
    }
    cout<<boja-1;
    return 0;
}