#include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 998244353; // constexpr int MOD = 1000000007; constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1}; constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1}; constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1}; template inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; } template inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << fixed << setprecision(20); } } iosetup; struct UndoableUnionFind { explicit UndoableUnionFind(const int n) : data(n, -1) {} int root(const int ver) const { return data[ver] < 0 ? ver : root(data[ver]); } bool unite(int u, int v) { u = root(u); history.emplace_back(u, data[u]); v = root(v); history.emplace_back(v, data[v]); if (u == v) return false; if (data[u] > data[v]) std::swap(u, v); data[u] += data[v]; data[v] = u; return true; } bool is_same(const int u, const int v) const { return root(u) == root(v); } int size(const int ver) const { return -data[root(ver)]; } void undo() { for (int i = 0; i < 2; ++i) { data[history.back().first] = history.back().second; history.pop_back(); } } void snapshot() { history.clear(); } void rollback() { while (!history.empty()) undo(); } private: std::vector data; std::vector> history; }; int main() { int h, w, n, d; cin >> h >> w >> n >> d; map, int> stars; REP(i, n) { int x, y; cin >> x >> y; --x, --y; stars[{x, y}] = i; } UndoableUnionFind union_find(n + 1); set> cand; for (const auto [xy, id] : stars) { const auto [x, y] = xy; FOR(dy, -d, d + 1) FOR(dx, -d, d + 1) { const int nx = x + dx, ny = y + dy; if (abs(dx) + abs(dy) > d || nx < 0 || nx >= h || ny < 0 || ny >= w) continue; if (const auto it = stars.find({nx, ny}); it != stars.end()) union_find.unite(id, it->second); cand.emplace(nx, ny); } } for (const auto [x, y] : stars | views::keys) cand.erase({x, y}); int cur = 0; REP(i, n + 1) { if (union_find.root(i) == i && union_find.size(i) >= 2) ++cur; } union_find.snapshot(); int mn = INF, mx = 0; for (const auto& [rx, ry] : cand) { int tmp = cur; FOR(dy, -d, d + 1) FOR(dx, -d, d + 1) { const int nx = rx + dx, ny = ry + dy; if (abs(dx) + abs(dy) > d || nx < 0 || nx >= h || ny < 0 || ny >= w) continue; if (const auto it = stars.find({nx, ny}); it != stars.end() && union_find.unite(n, it->second)) { --tmp; union_find.undo(); if (union_find.size(n) == 1) ++tmp; if (union_find.size(it->second) == 1) ++tmp; assert(union_find.unite(n, it->second)); } } chmin(mn, tmp); chmax(mx, tmp); union_find.rollback(); } if (n + cand.size() < h * w) { chmin(mn, cur); chmax(mx, cur); } cout << mn << ' ' << mx << '\n'; return 0; }