#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std;
void bfs(long long int sti,long long int stj,long long int**a,long long int **o,long long int n,long long int m,long long int& br)
{
    o[sti][stj]=1;
    queue<pair<long long int,long long int> > q;
    q.push({sti,stj});
    while(!q.empty())
    {
        pair<long long int,long long int> cur=q.front();

        if(cur.second-1 > 0 && o[cur.first][cur.second-1]==0)   {q.push({cur.first,cur.second-1});   o[cur.first][cur.second-1]=o[cur.first][cur.second]+1;}
        if(cur.second+1<m && o[cur.first][cur.second+1]==0)     {q.push({cur.first,cur.second+1});   o[cur.first][cur.second-1]=o[cur.first][cur.second]+1;}
        if(cur.first+1<n && o[cur.first+1][cur.second]==0)  {q.push({cur.first+1,cur.second});   o[cur.first+1][cur.second]=o[cur.first][cur.second]+1;}
        if(cur.first-1>0 && o[cur.first-1][cur.second]==0)   {q.push({cur.first-1,cur.second});  o[cur.first-1][cur.second]=o[cur.first][cur.second]+1;} 
           

        q.pop();
    }
    br++;
}

int main()
{
    long long int n, m, k;
    
    scanf("%lld%lld%lld",&n,&m,&k);
    long long int** a=(long long int**)malloc(n*sizeof(long long int*));
    for(long long int i=0;i<n;i++)
        a[i]=(long long int*)calloc(m,sizeof(long long int));

    long long int** ob=(long long int**)malloc(n*sizeof(long long int*));
    for(long long int i=0;i<n;i++)
        ob[i]=(long long int*)calloc(m,sizeof(long long int));
        
    for(long long int i=0;i<k;i++)
    {
        long long int x,y;
        scanf("%lld%lld",&x,&y);
        a[x-1][y-1]=-1;
        ob[x-1][y-1]=-1;
    }
    
    
    long long int br=0;
    for(long long int i=0;i<n;i++)
        for(long long int j=0;j<m;j++)
        if(ob[i][j]==0)
            bfs(i,j,a,ob,n,m,br);
            
    printf("%lld",br-1);
    return 0;
}