//Leto je, a tebi u srcu je hladno
//Ubila si mene tako mladog
//Leto je, a ja pijan lutam ovim gradom
//Tuzno moje leto, pevaj sa mnom
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define ldb long double
#define mt make_tuple
#define pii pair<int,int>
#define fi first
#define se second
int n,m,k;
vector<pii> pts;
int ans;
void input(){
	scanf("%i %i %i",&n,&m,&k);
	pts.clear();pts.resize(k);
	for(auto &p:pts)scanf("%i %i",&p.first,&p.second);
}
void output(){
	printf("%i\n",ans);
}
pii operator-(pii a,pii b){return mp(a.first-b.first,a.second-b.second);}
pii operator+(pii a,pii b){return mp(a.first+b.first,a.second+b.second);}
bool operator==(pii a,pii b){return a.fi==b.fi && a.se==b.se;}
pii mv[8]={{-1,0},{0,-1},{-1,1},{-1,-1},{1,-1},{1,0},{0,1},{1,1}};
vector<pair<int,int>> work;
vector<vector<int>> E;
vector<int> was;
int edge;
void DFS(int u){
	was[u]=1;
	for(int v:E[u])if(!was[v])DFS(v);
	if(pts[u-1].fi==1 || pts[u-1].fi==n || pts[u-1].se==1 || pts[u-1].se==m)edge=1;
}
void Solve(){
	work.clear();E.clear();was.clear();
	E.resize(pts.size()+1);
	was.resize(pts.size()+1);
	function<void(int,int)> AddEdge=[&](int u,int v){E[u].pb(v);E[v].pb(u);};
	int tsz=0;
	sort(pts.begin(),pts.end());
	function<int(pii)> blk=[&](pii p){int i=lower_bound(pts.begin(),pts.end(),p)-pts.begin();if(i==pts.size() || !(pts[i]==p))return 0;else return i+1;};
	for(auto p:pts){
		work.pb(p),work.pb(p-mp(1,0)),work.pb(p-mp(0,1)),work.pb(p-mp(1,1));
		for(int i=0;i<4;i++)if(blk(p+mv[i]))AddEdge(blk(p),blk(p+mv[i]));
	}
	work.pb(mp(n,m));
	sort(work.begin(),work.end());
	work.resize(unique(work.begin(),work.end())-work.begin());
	function<bool(pii)> isb=[&](pii p){return p.fi<1 || p.se<1 || p.fi>n || p.se>m || blk(p);};
	int cmp=0,bal=0;
	for(auto p:work){
		int blp=blk(p);
		if(blp && !was[blp])edge=0,cmp++,DFS(blp),cmp-=edge;
		if(!isb(p) && isb(p+mp(1,0)) && isb(p+mp(0,1)))bal++;
		if(!isb(p) && !isb(p+mp(1,0)) && !isb(p+mp(0,1)) && isb(p+mp(1,1)))bal--;
	}
	ans=bal+cmp;
}
void Run(){
	input();
	Solve();
	output();
}
int bans;
int bwas[1050][1050];
vector<pair<int,int>> bmv={{1,0},{0,1},{-1,0},{0,-1}};
void BDFS(int i,int j){
	bwas[i][j]=1;
	for(auto a:bmv){
		int x=i+a.fi;
		int y=j+a.se;
		if(x>=1 && x<=n && y>=1 && y<=m && !bwas[x][y])BDFS(x,y);
	}
}
void BruteForce(){
	for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)bwas[i][j]=0;
	for(auto p:pts)bwas[p.fi][p.se]=1;
	bans=0;
	for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)if(!bwas[i][j])BDFS(i,j),bans++;
}
void RunBrute(){
	input();
	BruteForce();
	printf("%i\n",bans);
}
mt19937 rng(7777777);
void MakeTestCase(){
	n=rng()%100000+1;
	m=rng()%100000+1;
	k=100000;
	pts.clear();
	pts.resize(k);
	for(int i=0;i<k;i++)pts[i]={rng()%n+1,rng()%m+1};
	sort(pts.begin(),pts.end());
	pts.erase(unique(pts.begin(),pts.end()),pts.end());
	k=pts.size();
}
bool Compare(){
	return ans==bans;
}
void Print(){
	printf("%i %i %i\n",n,m,k);
	for(auto p:pts)printf("%i %i\n",p.fi,p.se);
	printf("ans : %i\n",ans);
	printf("bans: %i\n",bans);
}
double GetTime(){return (ldb)clock()/CLOCKS_PER_SEC;}
double s_time,e_time,d_time;
void Start(){s_time=GetTime();}
void Stop(){e_time=GetTime();d_time=e_time-s_time;}
void Test(){
	cout<<fixed<<setprecision(4);
	int work=5;
	double max_time=0;
	bool speed_only=1;
	for(int i=1;i<=work;i++){
		MakeTestCase();
		if(!speed_only)BruteForce();
		Start();
		Solve();
		Stop();
		if(!speed_only && !Compare()){
			Print();
			return;
		}
		max_time=max(max_time,d_time);
		cout<<i<<":OK, max time so far="<<max_time<<"\n";
	}
	cout<<":D\n";
}
int main(){
	Run();
	return 0;
}