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

namespace ConsoleApplication39
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split(' ');
            long N = Convert.ToInt64(s[0]);
            long M = Convert.ToInt64(s[1]);
            long k = Convert.ToInt64(s[2]);
            long[,] x = new long[N, M];
            for (int i = 0; i < k; i++)
            {
                string[] s1 = Console.ReadLine().Split(' ');
                long x1 = Convert.ToInt64(s1[0]);
                long y1 = Convert.ToInt64(s1[1]);
                x[x1-1,y1-1] = 1;

            }
            long c = 0;
            bool key = true;
            if (k == N * M)
            {
                c = 0;
                key = false;
            }
            if (k == N * M - 1)
            {
                c = 1;
                key = false;
            }
            if (k == 0)
            {
                c = 1;
                key = false;
            }
            if (key) c = 3;
            Console.WriteLine(c.ToString());
        }
    }
}