import io import sys from collections import defaultdict, deque, Counter from itertools import permutations, combinations, accumulate from heapq import heappush, heappop from bisect import bisect_right, bisect_left from math import gcd import math from sys import stdout _INPUT = """\ 6 10 10 5 2 1 4 2 3 3 3 6 3 6 2 10 10 2 2 3 3 6 3 1 5 4 4 1 1 1 2 1 3 1 4 """ def input(): return sys.stdin.readline()[:-1] def solve(test): H,W,N,D=map(int,input().split()) A=[list(map(int,input().split())) for _ in range(N)] B=[A[i][0]*10**6+A[i][1] for i in range(N)] C={B[i]:i for i in range(N)} G=[[] for _ in range(N)] for i in range(N): for j in range(-D,D+1): for k in range(-(D-abs(j)),(D-abs(j))+1): if (j==0 and k==0) or A[i][0]+j<=0 or A[i][0]+j>H or A[i][1]+k<=0 or A[i][1]+k>W: continue if (A[i][0]+j)*10**6+A[i][1]+k in C: G[i].append(C[(A[i][0]+j)*10**6+A[i][1]+k]) d=defaultdict(int) for i in range(N): if len(G[i])==0: for j in range(-D,D+1): for k in range(-(D-abs(j)),(D-abs(j))+1): if (j==0 and k==0) or A[i][0]+j<=0 or A[i][0]+j>H or A[i][1]+k<=0 or A[i][1]+k>W: continue d[(A[i][0]+j)*10**6+A[i][1]+k]=1 inf=10**9 depth=[inf]*N now=0 for i in range(N): if depth[i]!=inf or len(G[i])==0: continue tmp=set() depth[i]=now Q=deque([i]) while Q: v=Q.popleft() for j in range(-D,D+1): for k in range(-(D-abs(j)),(D-abs(j))+1): if (j==0 and k==0) or A[v][0]+j<=0 or A[v][0]+j>H or A[v][1]+k<=0 or A[v][1]+k>W: continue tmp.add((A[v][0]+j)*10**6+A[v][1]+k) for u in G[v]: if depth[u]==inf: depth[u]=now Q.append(u) now+=1 for x in tmp: if x in d: d[x]-=1 else: d[x]=0 if H*W>100*N: flg=1 else: flg=0 for i in range(1,H+1): for j in range(1,W+1): if i*10**6+j not in d: flg=1 break if len(d)==0: d[0]=0 if flg==1: print(now+min(min(d.values()),0),now+max(max(d.values()),0)) else: print(now+min(d.values()),now+max(d.values())) def random_input(): from random import randint,shuffle N=randint(1,10) M=randint(1,N) A=list(range(1,M+1))+[randint(1,M) for _ in range(N-M)] shuffle(A) return (" ".join(map(str, [N,M]))+"\n"+" ".join(map(str, A))+"\n")*3 def simple_solve(): return [] def main(test): if test==0: solve(0) elif test==1: sys.stdin = io.StringIO(_INPUT) case_no=int(input()) for _ in range(case_no): solve(0) else: for i in range(1000): sys.stdin = io.StringIO(random_input()) x=solve(1) y=simple_solve() if x!=y: print(i,x,y) print(*[line for line in sys.stdin],sep='') break #0:提出用、1:与えられたテスト用、2:ストレステスト用 main(0)