region merging is n^2, which is ruining the algorithm - TODO: introduce disjoint set forest