#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <queue>
typedef  long long ll;
using namespace std;

void bfs(ll i, ll j, ll** a, ll** o, ll n, ll m)
{
    queue <pair<ll,ll> > que;
    que.push({i,j});
    o[i][j]=1;
    while (!que.empty())
    {
        pair <int,int> cur=que.front();
        ll x=cur.first,y=cur.second;

        if (x-1>=0 && a[x-1][y]!=0 && o[x-1][y]==0) { que.push({x-1,y}); o[x-1][y]=o[x][y]+1; }
        if (x+1<n && a[x+1][y]!=0 && o[x+1][y]==0) { que.push({x+1,y}); o[x+1][y]=o[x][y]+1; }
        if (y-1>=0 && a[x][y-1]!=0 && o[x][y-1]==0) { que.push({x,y-1}); o[x][y-1]=o[x][y]+1; }
        if (y+1<m && a[x][y+1]!=0 && o[x][y+1]==0) { que.push({x,y+1}); o[x][y+1]=o[x][y]+1; }


        que.pop();
    }
}

int main()
{
    ll br=0;
    ll n,m,k, w,e;
    ll ajf;
    cin >> n;
    cin >> m;
    cin >> k;

    ll** a=new ll*[n];
    ll **o=new ll *[n];
    for (int i=0;i<n;i++)
    {
        a[i]=(ll*)calloc(m,sizeof(ll));
        o[i]=(ll*)calloc(m,sizeof(ll));
    }

    for(ll i=0;i<k;i++)
    {
        cin >> w;
        cin >> e;
        w=w-1;
        e=e-1;
        a[w][e]=1;
    }


    for (ll i=0;i<n;i++)
    {
        for (ll j=0;j<m;j++)
        {
            if(a[i][j]==1)
               a[i][j]=0;
               else
                a[i][j]=1;
        }
    }


      ll x,y;
      for (ll i=0;i<n;i++)
      {
        for (ll j=0;j<m;j++)
        {
            if((o[i][j]==0)&&(a[i][j]!=0))
            {
                 x=i;
                 y=j;
               bfs(x,y,a,o,n,m);
               br=br+1;;
            }

        }

      }

    cout << br;
    return 0;
}