#include <bits/stdc++.h>

using namespace std;

//void  print(vector<vector<int>> a,int m, int n)
//{
//    for(int i = 1; i<= n; i++)
//    {
//        for(int j = 1; j<=m; j++)
//            cout << a[i][j] << "  ";
//        cout << endl;
//    }
//}

vector<vector<int>> color(vector<vector<int>> a,int i, int j,int m, int n)
 {
     queue<int> qx;
     queue<int> qy;
     qx.push(i);
     qy.push(j);
     a[i][j] = 2;
     while(!qx.empty())
     {
         i = qx.front();
         j = qy.front();
         qx.pop();
         qy.pop();
         if(i>1 && a[i-1][j]==0)
         {
             qx.push(i-i);
             qy.push(j);
             a[i-1][j] = 2;
         }
         if(j>1 && a[i][j-1]==0)
         {
             qx.push(i);
             qy.push(j-1);
             a[i][j-1] = 2;
         }
         if(i<n && a[i+1][j]==0)
         {
             qx.push(i+1);
             qy.push(j);
             a[i+1][j] = 2;
         }
         if(j<m && a[i][j+1]==0)
         {
             qx.push(i);
             qy.push(j+1);
             a[i][j+1] = 2;
         }
     }
    return a;
}

int countColors(vector<vector<int > > a, int m, int n)
{
    int br = 0;
    for(int j = 1; j <= m; j++)
    {
        for(int i = 1; i <= n; i++)
        {
            if(a[i][j]==0)
            {
                a = color(a,i,j,m,n);
  //              print(a,m,n);
                br++;
            }
        }
    }
    return br;

}

int main()
{
    int n, m, k;
    scanf("%d",&m);
    scanf("%d",&n);
    scanf("%d",&k);
    vector<vector<int > > a( n+1  , vector<int> (m+1, 0));
    for(int i = 0; i < k; i++)
    {
        int x,y;
        scanf("%d",&x);
        scanf("%d",&y);
        a[x][y] = 1;
    }
    printf("%d",countColors(a,m,n));
    return 0;
}