Hi @karen7, thank you for asking. I have modified the code in my solution, it will be better for understanding this time:

each time we call uf.union(int p, int q), we do count++, so when we call uf.count() it will return how many islands are already connected so far;
each time when we got a position, let's say positions[i], after we mark an island in this position, it means we have marked i + 1 islands right now;
then we can get how many disconnected islands are there in the matrix by calculating i + 1 - uf.count().

In the original version, we do count-- when we call uf.union(int p, int q), so each time we got a negative number to represent how many islands are connected when we call uf.count(), which makes we need to use i + 1 + uf.count() to get how many disconnected islands are there.