#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <vector>

#define MAXK 100000

using namespace std;

struct Cpolje
{
    int a,b;
};

bool manji(Cpolje a,Cpolje b)
{
    if(a.a==b.a)
        return a.b<b.b;
    return a.a<b.a;
}

void ispis(Cpolje a[],int k)
{
    for(int i=0;i<k;i++)
        cout<<a[i].a<<" "<<a[i].b<<endl;
}

void ispis(vector <int> &v)
{
    for(int i=0;i<v.size();i++)
        cout<<v[i]<<" ";
    cout<<endl;
}

void make(vector <int> &l,vector <int> &d,int &j,Cpolje x[],int m,vector <int> &b)
{
    int i=1,red=j,poc=1;
    while(x[j].a==x[red].a)
    {
        while(x[j].a==x[red].a&&x[j].b==poc){
            j++;
            poc++;
        }
        if(poc<=m){
            l.push_back(poc);
            if(x[j].a==x[red].a)
                d.push_back(x[j].b-1);
            else
                d.push_back(m);
            poc=x[j].b;
            b.push_back(0);
        }
    }
}

void oboji(vector <vector <int> > &l,vector <vector <int> > &d,vector <vector <int> > &b,int br,int a,int bb,int n);

void boja(vector <vector <int> > &l,vector <vector <int> > &d,vector <vector <int> > &b,int br,int a,int bb,int red,int n)
{
    int j=lower_bound(d[red].begin(),d[red].end(),a)-d[red].begin();
    for(;j<l[red].size()&&l[red][j]<=bb;j++)
    {
        if(b[red][j]==0)
            oboji(l,d,b,br,red,j,n);
    }

}

void oboji(vector <vector <int> > &l,vector <vector <int> > &d,vector <vector <int> > &b,int br,int a,int bb,int n)
{
    b[a][bb]=br;
    if(a>0)
        boja(l,d,b,br,l[a][bb],d[a][bb],a-1,n);
    if(a<l.size()-1)
    boja(l,d,b,br,l[a][bb],d[a][bb],a+1,n);
}

int glavna(int n,int m,int k,Cpolje x[])
{
    vector <int> r1,r;
    r1.push_back(x[0].a);
    for(int i=1;i<k;i++)
        if(x[i].a!=r1[r1.size()-1])
            r1.push_back(x[i].a);
    //ispis(r1);
    if(r1[0]!=1)
        r.push_back(1);
    for(int i=1;i<=r1.size();i++)
    {
        r.push_back(r1[i-1]);
        while(i<r1.size()&&r1[i]==r1[i-1]+1)
        {
            r.push_back(r1[i]);
            i++;
        }
        if(r1[i-1]+1<=n)
            r.push_back(r1[i-1]+1);
    }
    //ispis(r);
    vector < vector <int> > l(r.size()),d(r.size()),b(r.size());
    int j=0;
    for(int i=0;i<r.size();i++)
    {
        //cout<<r[i]<<endl;
        if(r[i]==x[j].a)
            make(l[i],d[i],j,x,m,b[i]);
        else
        {
            l[i].push_back(1);
            d[i].push_back(m);
            b[i].push_back(0);
        }
        //ispis(l[i]);
        //ispis(d[i]);
    }
    int br=0;
    for(int i=0;i<r.size();i++)
        for(int j=0;j<l[i].size();j++)
            if(b[i][j]==0){
                br++;
                oboji(l,d,b,br,i,j,n);
            }
    /*cout<<endl;
    for(int i=0;i<b.size();i++)
        ispis(b[i]);*/

    return br;
}

int main()
{
    int n,m,k;
    Cpolje x[MAXK];
    cin>>n>>m>>k;
    for(int i=0;i<k;i++)
        scanf("%d %d",&x[i].a,&x[i].b);
    sort(x,x+k,manji);
    /*cout<<endl;
    ispis(x,k);*/
    cout<< glavna(n,m,k,x);

    return 0;
}