Comments
+1

You are forgetting that there has to be at least one south magnet in every row and every column. In your solution, there is no south magnet in neither row 1 nor column 1. Also, both of these have two segments of black cells, so answer should be -1.

If you are interested only in the count of unique characters of a string x, it suffices to do the following:

sort(x.begin(), x.end());
int unique_chars_cnt = unique(x.begin(), x.end()) - x.begin();