Most Stones Removed with Same Row or Column
MediumLeetCodeUnion-Find (DSU)
Approach hintTry it first, then revealHide
Treat each stone as an edge joining its row and its column, offsetting column ids so the two kinds never collide. Every connected group can be reduced to a single stone, so subtract the number of groups.