#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;

#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a) (a).begin(),(a).end()
#define pb emplace_back
#define INF (1e9+1)



class UF{
private:
    vector<int> par,rank,elm;
    vector<vector<int>> elmList;
public:
    int groups;
    
    UF(int __size):par(__size), rank(__size,0),elm(__size,1),groups(__size),elmList(__size){
        rep(i,__size)elmList[i] = vector<int>(1,i);
        rep(i,__size)par[i] = i;
    }
    int find(int x){
        if(par[x]==x){
            return x;
        }else{
            return par[x]=find(par[x]);
        }
    }
    
    void unite(int x,int y){
        x=find(x);
        y=find(y);
        if(x==y)return;
        
        groups--;
        if(rank[x]<rank[y]){
            par[x]=y;
            elm[y]+=elm[x];
            for(auto &e:elmList[x])elmList[y].pb(e);
        }else{
            par[y]=x;
            elm[x]+=elm[y];
            for(auto &e:elmList[y])elmList[x].pb(e);
            if(rank[x]==rank[y])rank[x]++;
        }
    }
    bool isSame(int x,int y){
        return find(x)==find(y);
    }
};


int main(){
    int w,h,k;
    cin>>w>>h>>k;
    vector<pii> v;
    rep(i,k){
        int a,b;
        cin>>a>>b;
        v.pb(pii(b,a));
    }
    map<pii,int> mp;
    rep(i,v.size()){
        mp[pii(v[i])]=i;
    }
    
    UF uf(k+2);
    assert(!uf.isSame(k,k+1));
    for(int x=1;x<w;x++){
        if( mp.count(pii(0,x))==0 )continue;
        int id = mp[pii(0,x)];
        uf.unite(k,id);
    }
    
    for(int y=0;y<h-1;y++){
        if( mp.count(pii(y,w-1))==0 )continue;
        int id = mp[pii(y,w-1)];
        uf.unite(k,id);
    }

    for(int x=0;x<w-1;x++){
        if( mp.count(pii(h-1,x))==0 )continue;
        int id = mp[pii(h-1,x)];
        uf.unite(k+1,id);
    }
    
    for(int y=1;y<h;y++){
        if( mp.count(pii(y,0))==0 )continue;
        int id = mp[pii(y,0)];
        uf.unite(k+1,id);
    }
    
    rep(i,v.size()){
        for(int dy=-1;dy<=1;dy++){
            for(int dx=-1;dx<=1;dx++){
                if(dy==0&&dx==0)continue;
                int ddy = v[i].first+dy;
                int ddx = v[i].second+dx;
                if(ddy<0||ddx<0||ddy>=h||ddx>=w)continue;
                if( mp.count(pii(ddy,ddx))==0 )continue;
                int id = mp[pii(ddy,ddx)];
                uf.unite(i,id);
            }
        }
    }
    
    
    if(uf.isSame(k,k+1)){
        cout<<0<<endl;
        return 0;
    }
    
    rep(i,k){
        for(int dy=-2;dy<=2;dy++){
            for(int dx=-2;dx<=2;dx++){
                if(max(abs(dx),abs(dy))<=1)continue;
                int ddy = v[i].first+dy;
                int ddx = v[i].second+dx;
                int id;
                if((ddy==-1&&ddx>=1)||(ddx==w&&ddy<h-1)){
                    id = k;
                }else if((ddy==h&&ddx<w-1)||(ddx==-1&&ddy>=1)){
                    id = k+1;
                }else{
                    if(ddy<0||ddx<0||ddy>=h||ddx>=w)continue;
                    if(mp.count(pii(ddy,ddx))==0)continue;
                    id = mp[pii(ddy,ddx)];
                }
//                if(i==0)cout<<id<<endl;
                int a = uf.find(i);
                int b = uf.find(id);
//                if(i==0)cout<<a<<" "<<b<<endl;
                if((a==uf.find(k)&&b==uf.find(k+1)) || (a==uf.find(k+1)&&b==uf.find(k))){
                    cout<<1<<endl;
                    return 0;
                }
            }
        }
    }
    cout<<2<<endl;
}

Measured memory_bytes: 17368