class unif: def __init__(self,n): self.pare=[-1]*n self.size=[1]*n def root(self,x): while self.pare[x]!=-1: x=self.pare[x] return x def unite(self,u,v): rootu=self.root(u) rootv=self.root(v) if rootu!=rootv: if self.size[rootu]>=self.size[rootv]: self.pare[rootv]=rootu self.size[rootu]+=self.size[rootv] else: self.pare[rootu]=rootv self.size[rootv]+=self.size[rootu] def same(self,s,t): return self.root(s)==self.root(t) H,W,N,D=map(int,input().split()) C=set() A=[{} for i in range(2*(H+1))] T={} L=[] for i in range(N): x,y=map(int,input().split()) L.append((x,y)) T[x*10**10+y]=i Z=unif(N) for i in range(N): x,y=L[i][:] for dx in range(-D,D+1): k=D-abs(dx) for dy in range(-k,k+1): x2,y2=x+dx,y+dy if x2<=0 or x2>=H+1 or y2<=0 or y2>=W+1: continue w=x2*10**10+y2 if w in T: pos=T[w] Z.unite(i,pos) count=0 b=set() for i in range(N): p=Z.root(i) if Z.size[p]>1: b.add(p) count=len(b) h=[] u=[0]*N t=[0]*N for i in range(N): p=Z.root(i) k=Z.size[p] u[i]=p t[i]=k ans1=10**10 ans2=-10**10 used=0 D2=[set() for i in range(4*10**6)] e={} f=[0]*(4*10**6) for i in range(N): x,y=L[i][:] for dx in range(-D,D+1): k=D-abs(dx) for dy in range(-k,k+1): x2,y2=x+dx,y+dy if x==x2 and y==y2: continue if x2<=0 or x2>=H+1 or y2<=0 or y2>=W+1: continue n=t[i] pos=u[i] w=x2*10**10+y2 if not w in e: e[w]=used D2[used].add(pos) used+=1 if w in T: f[used-1]=0 else: f[used-1]=1 else: p=e[w] D2[p].add(pos) if used