using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void provera(int l, int h, int[,] a, int r)
        {
            if (l - 1 >= 0)
                if (a[l - 1, h] == 0)
                {
                    a[l - 1, h] = r;
                    provera(l - 1, h, a, r);
                }
            if (h - 1 >= 0)
                if (a[l, h - 1] == 0)
                {
                    a[l, h - 1] = r;
                    provera(l, h - 1, a, r);
                }
            if (l + 1 < a.GetLength(0))
                if (a[l + 1, h] == 0)
                {
                    a[l + 1, h] = r;
                    provera(l + 1, h, a, r);
                }
            if (h + 1 < a.GetLength(1))
                if (a[l, h + 1] == 0)
                {
                    a[l, h + 1] = r;
                    provera(l, h + 1, a, r);
                }
        }
        static void Main(string[] args)
        {
            string[] p = Console.ReadLine().Split();
            int n = Convert.ToInt32(p[0]);
            int m = Convert.ToInt32(p[1]);
            int[,] a = new int[n, m];
            for (int i = 0; i < Convert.ToInt32(p[2]); i++)
            {
                string[] k = Console.ReadLine().Split();
                a[Convert.ToInt32(k[0])-1, Convert.ToInt32(k[1])-1] = -1;
            }
            int r = 1;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    if (a[i, j] == 0)
                    {
                        provera(i, j, a, r);
                        r++;
                    }
                }
            }
            Console.WriteLine(r-1);
            Console.ReadLine();
        }
    }
}