1.Call methods when absolutely necessary
the first step to solving it is to look at all of your calls to WeightedQuickUnionUF (WQUUF) objects.I suspect that the most calls are in Percolation.open(int, int) method.A good way to decrease the number of method calls is to restrict the conditions. A method should only be called if absolutely necessary. In other words, don't open a site if it is already open.