program Zaplaty; type TDira = record x,y: LongInt; patch: boolean; end; const MaxD=1000000; movex:array[1..4] of integer=(-1,1,0,0); {doleva, doprava, ..., ...} movey:array[1..4] of integer=(0,0,-1,1); { ..., ..., nahoru, dolů} var diry: array[1..MaxD] of TDira; m,n,d: LongInt; {rozměry m n, počet děr} i,j,k:LongInt; vysledek: LongInt; f:array[1..MaxD] of LongInt; {fronta děr} fi,fcnt:LongInt; {index ve frontě, počet děr ve frontě} minx,miny,maxx,maxy:LongInt; procedure ReadInp; var x,y:LongInt; begin readln(m,n,d); for i:=1 to d do begin readln(y, x); diry[i].x:= x; diry[i].y:= y; diry[i].patch:= false; end; end; function Compare(a,b:TDira):LongInt; begin if a.x>b.x then Compare:=1 else if a.xb.y then Compare:=1 else if a.yl then qsort(l,i); if i+2

p); if Compare(pt,diry[m])=0 then find:=m else find:=-1; end; begin ReadInp; sort_diry; {Roztřídíme díry} vysledek:=0; for i:=1 to d do {Nahlédneme do každé díry a zalepíme ji} if not diry[i].patch then begin {díra není zazáplatována?} diry[i].patch:=true; minx:=diry[i].x; miny:=diry[i].y; maxx:=minx; maxy:=miny; fcnt:=1; {Díru dáme do fronty} f[1]:=i; fi:=1; while fi<=fcnt do begin {Projdeme díry ve frontě} for k:=1 to 4 do begin {Zkontrolujeme, jestli nemá díra sousedky} j:=find(diry[f[fi]].x+movex[k],diry[f[fi]].y+movey[k]); if j<>-1 then {našli jsme nějakou vedle sebe?} if not diry[j].patch then begin {přidej do fronty a rozšiř záplatu} inc(fcnt); f[fcnt]:=j; diry[j].patch:=true; if minx>diry[j].x then minx:=diry[j].x; if maxxdiry[j].y then miny:=diry[j].y; if maxy