#include<stdio.h>
#include <iostream> 
#include <iterator> 
#include <map>


#define c(R,C) (R*c+C)
#define gc(R,C) t[I2=(R*c+C)]
#define g(R,C) t[R*c+C]

//#define DEBUG

struct T{
    char type;
    int s; //root block
  
  T(){
      type = 'w';
      s = -1;
  }
};


T *t;
int r,c,b; //r = rows, c = columns, b = blacks
int I,I2; //index u t
int ss;
std::map<int,int>::iterator ssi;
std::map<int,int> regions;

void draw(char ch = 'O', int x = -1, int y = -1){
    #ifdef DEBUG
    printf("\n");
    for(int xx=0;xx<r;xx++){
        for(int yy=0;yy<c;yy++){
            if(g(xx,yy).type == 'b')
            printf("B");
            else if(xx==x&&yy==y)
            printf("%c",ch);
            else
            printf("X");
        }
        printf("\n");
    }
    #endif
}

int cSrc(int x, int y){
    if(x<0||y<0||x>=r||y>=c) return -2; //out of bounds
    if(gc(x,y).type == 'b') return -3; //is black
    if(gc(x,y).s == -1) return -1; //has no source
    return 1;
}

void pro(int x,int yy, int i3, int y = 0);

inline void pr0(int x, int y, const char *str){
    regions.lower_bound(t[I2].s)->second--;
    #ifdef DEBUG
    printf(str,t[I2].s,regions.lower_bound(t[I2].s)->second);
    #endif
    ssi->second+=1;
    #ifdef DEBUG
    printf(str,ss,ssi->second);
    #endif
    t[I2].s = ss;
}

inline int pr(int x, int y, const char *str){
    if(cSrc(x,y) == 1 && t[I2].s != ss){
        pr0(x,y,str);
        pro(x,y,I2);
        draw('P',x,y);
    }else return 1; //else break
    return 0;
}

void pro(int x,int yy,int I3, int y){
    #ifdef DEBUG
    printf("pro %d %d %d\n",x,yy,I3);
    #endif
    
    pr0(x,yy,"p %d %d\n");
            
    
    for(y=yy+1;y<c;y++){
        if(pr(x,y,"py+ %d %d\n"))break;
    }
    for(y=yy-1;y>-1;y--){
        if(pr(x,y,"py- %d %d\n"))break;
    }
    y=yy;
    yy=x;
    for(x=yy+1;x<r;x++){
        if(pr(x,y,"px+ %d %d\n"))break;
    }
    for(x=yy-1;x>-1;x--){
        if(pr(x,y,"px- %d %d\n"))break;
    }
}

void src(int x, int y){
    I=c(x,y);
    
    ss = -1;
    
    //for(x++)for(y++)
    
    /*
    if(cSrc(x+1,y)==1){
        t[I].s=I2;
    }
    
    if(cSrc(x,y+1)==1){
        t[I].s=I2;
    }
    */
    if(cSrc(x,y-1)==1){
        ss=t[I2].s;
        t[I].s=ss;
        ssi = regions.lower_bound(ss);
        ssi->second+=1;
        #ifdef DEBUG
        printf("sy %d %d\n",ss,ssi->second);
        #endif
    }
    
    if(cSrc(x-1,y)==1){
        if(ss==-1){
            ss=t[I2].s;
            t[I].s=ss;
            ssi = regions.lower_bound(ss);
            ssi->second+=1;
            #ifdef DEBUG
            printf("sx %d %d\n",ss,ssi->second);
            #endif
        }else{
            //apply ss to neigbour
            //ssi applied
            pro(x-1,y,I2);
        }
    }
    
    
    if(ss==-1){
        t[I].s=I;
        ss = I;
        regions.insert(std::pair<int, int>(ss, 1));
        #ifdef DEBUG
        printf("i %d\n",ss);
        #endif
    }
}


int main()
{
    int x,y,i;
    
    scanf("%d %d %d",&r,&c,&b);
    
    t = new T[r*c];
    
    for(i=0;i<b;i++){
        scanf("%d %d",&x,&y);
        x--;
        y--;
        g(x,y).type = 'b';
        //printf("%u %u %c\n",x,y,g(x,y).type);
    }
    
    //draw
    draw();
    
    
    for(x=0;x<r;x++){
        for(y=0;y<c;y++){
            
            if(cSrc(x,y)==-1){
            draw('O',x,y);
            
            src(x,y);
            }
            
        }
    }
    
    int cnt = 0;
    for (auto it = regions.begin(); it != regions.end(); it++) { 
        #ifdef DEBUG
        printf("%d %d\n",it->first,it->second);
        #endif
        if(it->second>0)
            cnt++;
    }
    
    printf("%d",cnt);
    
    return 0;
}