#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define SIZE 100005

typedef pair<int,int> P;

int W,H;
int K,num_Cell;
int diff_row[8] = {-1,-1,-1,0,0,1,1,1},diff_col[8] = {-1,0,1,-1,1,-1,0,1};
int boss[6*SIZE],height[6*SIZE];
map<P,bool> MAP;
map<P,int> ID;
vector<P> V,LEFT_UP,RIGHT_UNDER,CANDIDATE;


bool rangeCheck(int row,int col){

    return row >= 0 && row <= H+1 && col >= 0 && col <= W+1;
}

int get_boss(int id){
    if(boss[id] == id)return id;
    else{
        return boss[id] = get_boss(boss[id]);
    }
}

void unite(int x,int y){
    int boss_x = get_boss(x);
    int boss_y = get_boss(y);

    if(boss_x == boss_y)return;

    //高さが高い方に吸収する
    if(height[x] > height[y]){

        boss[boss_y] = boss_x;

    }else if(height[x] < height[y]){

        boss[boss_x] = boss_y;

    }else{ //height[x] == height[y]

        boss[boss_y] = boss_x;
        height[x]++;
    }
}

int main(){

    scanf("%d %d %d",&W,&H,&K);

    num_Cell = 0;

    int x,y;

    for(int loop = 0; loop < K; loop++){

        scanf("%d %d",&x,&y);
        //外側をダミーセルで覆う
        x++;
        y++;

        MAP[make_pair(x,y)] = true;
        ID[make_pair(x,y)] = num_Cell++;
        V.push_back(make_pair(x,y));
    }

    //猫を左下に見た時、左上方向のダミーセルを列挙する
    for(int x = 0; x <= W-2; x++){

        MAP[make_pair(x,H+1)] = true;
        ID[make_pair(x,H+1)] = num_Cell++;
        LEFT_UP.push_back(make_pair(x,H+1));
    }
    for(int y = H; y >= 3; y--){

        MAP[make_pair(0,y)] = true;
        ID[make_pair(0,y)] = num_Cell++;
        LEFT_UP.push_back(make_pair(0,y));
    }

    //右下方向
    for(int y = 0; y <= H-2; y++){

        MAP[make_pair(W+1,y)] = true;
        ID[make_pair(W+1,y)] = num_Cell++;
        RIGHT_UNDER.push_back(make_pair(W+1,y));
    }
    for(int x = 3; x <= W; x++){

        MAP[make_pair(x,0)] = true;
        ID[make_pair(x,0)] = num_Cell++;
        RIGHT_UNDER.push_back(make_pair(x,0));
    }

    for(int i = 0; i < num_Cell; i++){

        boss[i] = i;
        height[i] = 0;
    }

    //左上方向のダミーセルの連結
    for(int i = 1; i < LEFT_UP.size(); i++){

        int left_id = ID[LEFT_UP[i-1]];
        int right_id = ID[LEFT_UP[i]];
        unite(left_id,right_id);
    }

    //右下方向のダミーセルの連結
    for(int i = 1; i < RIGHT_UNDER.size(); i++){

        int left_id = ID[RIGHT_UNDER[i-1]];
        int right_id = ID[RIGHT_UNDER[i]];
        unite(left_id,right_id);
    }

    //実セルの連結
    for(int i = 0; i < V.size(); i++){
        for(int a = 0; a < 8; a++){

            int adj_row = V[i].second+diff_row[a];
            int adj_col = V[i].first+diff_col[a];

            auto at = MAP.find(make_pair(adj_col,adj_row));

            if(at == MAP.end())continue;

            int my_id = ID[V[i]];
            int adj_id = ID[make_pair(adj_col,adj_row)];

            unite(my_id,adj_id);
        }
    }

    int LEFT_UP_id = ID[make_pair(0,H+1)];
    int RIGHT_UNDER_id = ID[make_pair(W+1,0)];

    if(get_boss(LEFT_UP_id) == get_boss(RIGHT_UNDER_id)){ //最初から遮断されている場合

        printf("0\n");
        return 0;
    }

    //追加1つで遮断できるか調べる
    for(int i = 0; i < V.size(); i++){
        for(int a = 0; a < 8; a++){

            int adj_row = V[i].second+diff_row[a];
            int adj_col = V[i].first+diff_col[a];

            auto at = MAP.find(make_pair(adj_col,adj_row));

            if(at != MAP.end())continue; //ダミーセル、または岩が既に置いてあったらSKIP

            CANDIDATE.push_back(make_pair(adj_col,adj_row)); //架空のセル
        }
    }

    sort(CANDIDATE.begin(),CANDIDATE.end());
    CANDIDATE.erase(unique(CANDIDATE.begin(),CANDIDATE.end()),CANDIDATE.end());

    for(int i = 0; i < CANDIDATE.size(); i++){

        bool FLG_LU = false,FLG_RD = false;

        for(int a = 0; a < 8; a++){

            int adj_row = CANDIDATE[i].second+diff_row[a];
            int adj_col = CANDIDATE[i].first+diff_col[a];

            if(!rangeCheck(adj_row,adj_col))continue;

            auto at = MAP.find(make_pair(adj_col,adj_row));

            if(at == MAP.end())continue;

            int adj_id = ID[make_pair(adj_col,adj_row)];


            if(get_boss(adj_id) == get_boss(LEFT_UP_id)){

                FLG_LU = true;

            }else if(get_boss(adj_id) == get_boss(RIGHT_UNDER_id)){

                FLG_RD = true;
            }
        }

        if(FLG_LU && FLG_RD){
            printf("1\n");
            return 0;
        }
    }

    printf("2\n");

    return 0;
}



Measured memory_bytes: 81532