#include <bits/stdc++.h>

#define ll long long
#define pb push_back
#define endl "\n"

using namespace std;

int a[1005][1005];
int boja=1;
int h,w,k;

bool valid(int x, int y) {
    return (x<h && x>=0 && y<w && y>=0);
}

void boji(int x, int y)
{
    if(!valid(x,y))
        return;
    if(a[x][y])
        return;
    a[x][y]=boja;
    boji(x-1,y);
    boji(x,y+1);
    boji(x+1,y);
    boji(x,y-1);
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0);
    memset(a,0,sizeof(a));

    cin >> h >> w >> k;

    if(h>1000 || w>1000) {
        cout << "1" << endl;
        return 0;
    }

    for(int i=0;i<k;i++) {
        int x,y; cin >> x >> y;
        a[x-1][y-1]=-1;
    }

    for(int i=0;i<h;i++) {
        for(int j=0;j<w;j++) {
            if(!a[i][j]) {
                boji(i,j);
                boja++;
            }
        }
    }

    cout << boja-1 << endl;

    return 0;
}