#include <iostream>
#include <vector>
using namespace std;

int n, m, k;
int xaxis, yaxis;
int result = 0;
typedef vector<vector<int>> intMatrix;

 void flood(int x, int y, intMatrix& map) {
     if (map[x][y] == 0) {
         map[x][y] = 1;

         flood(x, y + 1, map);
         flood(x, y - 1, map);
         flood(x + 1, y, map);
         flood(x - 1, y, map);

     }

     return;
 }


int main() {
    cin >> n >> m >> k;

    vector<int> x(n+2);
    intMatrix map(m+2, x);
    
    for (int i = 0; i <= n+1; i++) {
        map[0][i] = 1;
        map[m+1][i] = 1;
    }

    for (int i = 0; i <= m + 1; i++) {
        map[i][0] = 1;
        map[i][n+1] = 1;
        
    }



    for (int l = 0; l < k; l++) {
        cin >> yaxis >> xaxis;
        map[xaxis][yaxis] = 1;
    }

    for (int k = 1; k < m + 1; k++) {
        for (int l = 1; l < n + 1; l++) {
            if (map[k][l] == 0) {
                flood(k, l, map);
                result++;
           }
        }
    }


    cout << result;
}