#include <iostream>
#include<cmath>
using namespace std;

int main()
{
    int redovi,kolone,crno;
    cin>>redovi>>kolone>>crno;
    int tabla[redovi][kolone];
    int koordinate[2][crno];
    for(int i=0;i<crno;i++)
    {
        for(int j=0; j<2; j++)
        {
            cin>>koordinate[i][j];
            }
        }
    if(crno+1==redovi*kolone) cout<<1;
    else if(redovi==kolone)
    {
        int a=0;
        int dijagonalna=0;
        int sredina=0;
        double sr=ceil(redovi/2);
        while(a<crno)
        {
            int j=0;
            if(koordinate[a][j]==koordinate[a][j+1])
            {
                dijagonalna++;
                if(koordinate[a][j]==sr)
                {
                    sredina++;
                }
            }
            a++;
        }
        int red1[redovi],kolona1[kolone],red2[redovi],kolona2[kolone];
        int p=0;
        for(int i=redovi-1; i>=redovi/2; i--)
        {
            red1[p]=i+1;
            p++;
        }
        for(int i=0; i<=kolone/2; i++)
        {
            kolona1[i]=i+1;
        }
        int p2=0;
        for(int i=(redovi/2)-1; i>=0; i--)
        {
            red2[p2]=i+1;
            p2++;
        }
        int s=0;
        for(int i=(kolone/2)+1;i<kolone;i++)
        {
            kolona2[s]=i+1;
            s++;
        }
        int b=0;
        int obrnutodij1=0,obrnutodij2=0;
        while(b<crno)
        {
            int j=0;
            if((koordinate[b][j]==red1[b])&&(koordinate[b][j+1]==kolona1[b])&&(koordinate[b][j]!=koordinate[b][j+1]))obrnutodij1++;
            b++;
        }
        int c=redovi-(redovi/2);
        int c1=0;
        while(c<crno)
        {
            int j=0;
            if((koordinate[c][j]==red2[c1])&&(koordinate[c][j+1]==kolona2[c1])&&(koordinate[c][j]!=koordinate[c][j+1]))obrnutodij2++;
            c++;
            c1++;
        }
        int ivice=0;
        int d=0;
        while(d<crno)
        {
            int j=0;
            if((koordinate[d][j]==1&&koordinate[d][j+1]==1)||(koordinate[d][j]==1&&koordinate[d][j+1]==redovi)||(koordinate[d][j]==redovi&&koordinate[d][j+1]==1)||(koordinate[d][j]==redovi&&koordinate[d][j+1]==redovi))ivice++;
            d++;
        }
        double polja=floor(redovi/2);
        if(dijagonalna==redovi)
        {
            if(crno==dijagonalna)cout<<2;
            else if((crno==dijagonalna+obrnutodij1||crno==dijagonalna+obrnutodij2)&&(polja==obrnutodij1||polja==obrnutodij2))
            {
                cout<<3;
            }
            else if((crno==dijagonalna+obrnutodij1+obrnutodij2)&&(polja==obrnutodij1&&polja==obrnutodij2))
            {
                cout<<4;
            }
            else if(crno==dijagonalna+ivice)
            {
                cout<<2;
            }
            else if(crno==dijagonalna+sredina)
            {
                cout<<2;
            }
            else cout<<3;
        }
        else
        {
            if(crno==obrnutodij1+obrnutodij2&&dijagonalna==0&&ivice==0)
            {
                if(redovi%2==0)
                {
                    cout<<2;
                }
                else cout<<1;
            }
            else if(crno==obrnutodij1+obrnutodij2+1&&sredina==1)
            {
                cout<<2;
            }
            else if(crno==ivice&&ivice==4)
            {
                cout<<1;
            }
            else cout<<2;
        }
    }
    else cout<<3;
    return 0;
}