#include <iostream>
using namespace std;
int main()
{
    int n , m , k, j , xx , yy , x , y;
    bool bx,by;
    bx = true;
    by = true;
    cin >> n >> m >> k;
    cin >> xx >> yy;
    for(int i = 1;i<k;i++)
    {
        cin >> x >> y;
        if(xx != x) bx = false;
        if(yy != y) by = false;
    }
    if(n==2 || m==2)
    {
        if(k==1) j=1;
        else j=2;
    }
    else
    {
        if(bx==true || by==true) j=1;
        else {
        if(k==2) j = 1;
        else j=2;
        }
    }
    cout << j << endl;
    return (0);
}