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

namespace ConsoleApp19
{
    
    class Program
    {
        static void Main(string[] args)
        {
            int N, M,K,i,j,k1,k2,boja=2,menjase=0,b=0;
            
            string[] ulaz = Console.ReadLine().Split(' ');
            N = Convert.ToInt32(ulaz[0]);
            M = Convert.ToInt32(ulaz[1]);
            K = Convert.ToInt32(ulaz[2]);
            
            int[,] mat = new int[N,M];
            
            for (i = 0; i < K; i++)
            {
                ulaz = Console.ReadLine().Split(' ');
                k1 = Convert.ToInt32(ulaz[0]);
                k2 = Convert.ToInt32(ulaz[1]);
                mat[k1-1,k2-1] = 1;
            }
            if (mat[0,0]!=1)
            {
                boja++;
            }
            for (i = 0; i < N; i++)
            {
                for (j = 0; j < M; j++)
                {
                    
                    if (mat[i, j] != 1)
                    {
                        mat[i, j] = boja; 
                    }
                    if (mat[i, j] == 1 || j == M - 1)
                    {
                        boja++;
                    }
                }
            }
            int[] tboja = new int[boja];
            boja--;
            for (i = 1; i < N; i++)
            {
                for (j = 0; j < M; j++)
                {
                    if (mat[i - 1, j] < mat[i, j] && mat[i - 1, j]!=1) 
                    {
                        menjase = mat[i, j];
                        mat[i, j] = mat[i - 1, j];
                        boja--;
                        for (int z = 0; z < M; z++)
                        {
                            if (mat[i,z]==menjase)
                            {
                                mat[i, z] = mat[i - 1, j];
                            }
                        }
                    }
                    if (mat[i - 1, j] > mat[i, j] && mat[i, j] != 1)
                    {
                        menjase = mat[i - 1, j];
                        boja--;
                        for (int x = 0; x < N; x++)
                        {
                            for (int z = 0; z < M; z++)
                            {
                                if (mat[x, z] == menjase)
                                {
                                    mat[x, z] = mat[i, j];
                                }
                            }
                        }
                    }
                }
            }
            boja = 0;
            i = 0;
            j = 0;
            while (i<N)
            {

                if (mat[i, j] != 1)
                {
                    tboja[b] = mat[i, j];
                    b++;
                    boja++;
                    break;
                }
                if (j==M-1)
                {
                    i++;
                    j = 0;
                }
                else
                {
                    j++;
                }
                
            }
            int aga=0;
            for (i = 0; i < N; i++)
            {
                for (j = 0; j < M; j++)
                {
                    for (int x = 0; x < b; x++)
                    {
                        if (mat[i, j] == tboja[x] && mat[i,j]!=1)
                        {
                            aga++;
                        }
                    }
                    if (mat[i,j]!=1 && aga==0)
                    {
                        tboja[b] = mat[i, j];
                        b++;
                        boja++;
                        
                    }
                    aga = 0;
                }
            }
           
            Console.WriteLine("{0}", b);
            
        }
    }
}