#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll MOD = 1e9+7;
const int N = 1e5+5;
const int dirx[]={-1,0,1,0};
const int diry[]={0,1,0,-1};

int n,m,k;
pair<int,int>a[N];
int par[N];
int sz[N];
int ans;
map<pair<int,int>,int>mp;
vector<int>v;
int b[1005][1005];
bool vis[1005][1005];

bool can(int x,int y){
    return x>=1&&x<=n&&y>=1&&y<=m&&b[x][y]!=-1&&!vis[x][y];
}

void dfs(int x,int y){
    vis[x][y]=true;
    for(int i=0;i<4;i++){
        if(can(x+dirx[i],y+diry[i])){
            dfs(x+dirx[i],y+diry[i]);
        }
    }
}

bool cmp(const pair<int,int> &fi,const pair<int,int> &se){
    if(fi.first!=se.first)return fi.first<se.first;
    return fi.second<se.second;
}

int Find(int x){
    if(par[x]==x)return par[x];return par[x]=Find(par[x]);
}

void Merge(int fi,int se){

    int fii=Find(fi);
    int see=Find(se);
    if(fii!=see){
        if(sz[fii]<sz[see])swap(fii,see);
        par[see]=fii;
        sz[fii]+=sz[see];
    }
}

void con(int x,int y){
    int parl=-1,parr=-1;
    int newx=-1,newy=-1;
    if(mp[{x,y-1}])newx=x,newy=y-1;
    else if(mp[{x-1,y-1}])newx=x-1,newy=y-1;
    else if(mp[{x-1,y}])newx=x-1,newy=y;

    if(x==1||y==1)newx=0,newy=0;

    if(newx!=-1){
        parl=Find(mp[{newx,newy}]);
    }

    newx=-1;
    newy=-1;
    if(mp[{x,y+1}])newx=x,newy=y+1;
    else if(mp[{x-1,y+1}])newx=x-1,newy=y+1;
    else if(mp[{x-1,y}])newx=x-1,newy=y;

    if(x==1||y==m)newx=0,newy=0;

    if(newx!=-1){
        parr=Find(mp[{newx,newy}]);
    }
    if(x==3&&y==5){
        cout<<parl<<" "<<parr<<" DSDADS \n";
    }
    if(parl==parr&&parl!=-1){
        Merge(mp[{x,y}],mp[{newx,newy}]);
        if(!mp[{x-1,y}]&&x>=2){
            ans++;
            cout<<"sad"<<" "<<x<<" "<<y<<"\n";
        }
    }else{
        if(parl!=-1)Merge(mp[{x,y}],parl);
        else if(parr!=-1)Merge(mp[{x,y}],parr);
    }

}

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>n>>m>>k;
    for(int i=1;i<=k;i++){
        cin>>a[i].first>>a[i].second;
    }

    if(n<=1000&&m<=1000){

        for(int i=1;i<=k;i++)b[a[i].first][a[i].second]=-1;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(!vis[i][j]&&b[i][j]!=-1){
                    dfs(i,j);
                    ans++;
                }
            }
        }
        cout<<ans;
        return 0;

    }



    sz[0]=1;
    par[0]=0;
    mp[{0,0}]=0;

    for(int i=1;i<=k;i++){
        mp[{a[i].first,a[i].second}]=i;
        sz[i]=1;
        par[i]=i;
    }

    sort(a+1,a+1+k,cmp);
    for(int i=1;i<=k;i++){
        if(a[i].first==1){
            Merge(mp[{a[i].first,a[i].second}],0);
            con(a[i].first,a[i].second);
        }else if(a[i].second==1){
            Merge(mp[{a[i].first,a[i].second}],0);
            con(a[i].first,a[i].second);
        }else if(a[i].second==m){
            Merge(mp[{a[i].first,a[i].second}],0);
            con(a[i].first,a[i].second);
        }else if(a[i].first==n){
            Merge(mp[{a[i].first,a[i].second}],0);
            con(a[i].first,a[i].second);
        }else{
            con(a[i].first,a[i].second);
        }
        if(a[i].first==n)v.push_back(a[i].second);
    }

    for(int i=1;i<v.size();i++){
        if(Find(mp[{n,a[i-1].second}])==Find(mp[{n,a[i].second}]))ans++;
    }
    cout<<ans+1;
    return 0;
}


/*
4 4 5
1 1
1 3
2 2
3 3
4 4
*/


/*
11 12 47
1 1
1 2
1 7
1 8
1 11
1 12
2 3
2 4
2 6
2 7
2 9
2 10
3 4
3 5
4 11
4 12
5 4
5 5
5 11
5 12
6 3
6 6
6 7
6 10
6 12
7 3
7 4
7 5
7 6
7 10
8 4
8 10
8 11
8 12
9 7
9 8
9 10
9 11
10 3
10 5
10 7
10 9
10 10
11 2
11 6
11 8
11 10


*/