#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

#define mt make_tuple
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sqr(x) ((ll)(x) * (x))

void bfs(vector<vector<ll>> &v, int a, int b, ll boja, int n, int m)
{
    queue <pii> q;
    q.push(mp(a, b));
    v[a][b] = boja;

    while(!q.empty())
    {
        pii trenutni = q.front();
        q.pop();
        v[trenutni.fi][trenutni.se] = boja;

        int i = trenutni.fi;
        int j = trenutni.se;

        if(i - 1 >= 1)
        {
            if(v[i - 1][j] == LLONG_MAX)
            {
                q.push(mp(i - 1, j));
            }
        }

        if(i + 1 <= n)
        {
            if(v[i + 1][j] == LLONG_MAX)
            {
                q.push(mp(i + 1, j));
            }
        }

        if(j - 1 >= 1)
        {
            if(v[i][j - 1] == LLONG_MAX)
            {
                q.push(mp(i, j - 1));
            }
        }

        if(j + 1 <= m)
        {
            if(v[i][j + 1] == LLONG_MAX)
            {
                q.push(mp(i, j + 1));
            }
        }
    }
}

int main()
{
    int n, m, k;

    cin >> n >> m >> k;

    vector<vector<ll>> v(n + 1);

    for(int i = 0; i <= n; i++)
    {
        v[i].resize(m + 1);

        for(int j = 0; j <= m; j++)
        {
            v[i][j] = LLONG_MAX;
        }
    }

    for(int i = 0; i < k; i++)
    {
        int a, b;

        cin >> a >> b;
        v[a][b] = 0;
    }

    ll trenutnaBoja = 0;

    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            if(v[i][j] != LLONG_MAX)
            {
                continue;
            }

            else
            {
                bfs(v, i, j, ++trenutnaBoja, n, m);
            }
        }
    }

    cout << trenutnaBoja << "\n";

    return 0;
}