program da;
var a:array[1..1000,1..1000] of integer;
    s:array [1..1000,1..2] of integer;
    i,m,n,k,v,t,br,p1,p2,h:integer;
begin

read(m,n);
read(h);
for v:=1 to m do
    for k:=1 to n do
        a[v,k]:=0;
for i:=1 to h do
    begin
    read(v,k);
    a[v,k]:=1;
    end;
t:=1;

for v:=1 to m do
    for k:=1 to n do
        if a[v,k]=0 then
            begin
            br:=1;
            s[1,1]:=v;
            s[1,2]:=k;
            t:=t+1;
            while br>0 do
                begin
                p1:=s[br,1];
                p2:=s[br,2];
                a[p1,p2]:=t;
                br:=br-1;
                
                if p1>1 then
                    if a[p1-1,p2]=0 then
                        begin
                        br:=br+1;
                        s[br,1]:=p1-1;
                        s[br,2]:=p2;
                        end;
                if p1<m then
                    if a[p1+1,p2]=0 then
                        begin
                        br:=br+1;
                        s[br,1]:=p1+1;
                        s[br,2]:=p2;
                        end;
               if p2>1 then
                    if a[p1,p2-1]=0 then
                        begin
                        br:=br+1;
                        s[br,1]:=p1;
                        s[br,2]:=p2-1;
                        end;
               if p2<n then
                    if a[p1,p2+1]=0 then
                        begin
                        br:=br+1;
                        s[br,1]:=p1;
                        s[br,2]:=p2+1;
                        end;
                end;
            
            
            end;


write(t-1);

end.