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

namespace ConsoleApp6
{
    class Program
    {
        public static int[,] matrix;

        public static void colorSegment(int i, int j, int N, int M, int b)
        {


            matrix[i, j] = b;
            if (i + 1 < N && matrix[i + 1, j] == 0)
            {

                colorSegment(i + 1, j, N, M, b);
            }
            if (j + 1 < M && matrix[i, j + 1] == 0)
            {

                colorSegment(i, j + 1, N, M, b);
            }
            if (i - 1 > 0 && matrix[i - 1, j] == 0)
            {

                colorSegment(i - 1, j, N, M, b);
            }
            if (j - 1 > 0 && matrix[i, j - 1] == 0)
            {

                colorSegment(i, j - 1, N, M, b);
            }

        }

        static void Main(string[] args)
        {
            int N, M, K;

            string temp = Console.ReadLine();
            N = Convert.ToInt32(temp.Split()[0]);
            M = Convert.ToInt32(temp.Split()[1]);
            K = Convert.ToInt32(temp.Split()[2]);

            matrix = new int[N, M];

            for (int i = 0; i < K; i++)
            {
                temp = Console.ReadLine();
                matrix[(Convert.ToInt32(temp.Split()[0])) - 1, (Convert.ToInt32(temp.Split()[1])) - 1] = -1;
            }
            int b = 1;

            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < M; j++)
                {
                    if (matrix[i, j] == 0)
                    {
                        if (matrix[i, j] != -1)
                        {
                            colorSegment(i, j, N, M, b);
                            ++b;
                        }
                    }
                }
            }
            Console.WriteLine(b - 1);
        }
    }
}