Simple segmentation of objects in binary image with some parts connected



I want to segment the binary image like the example above, but there are some times connection between different squares which result in a connected big contour which is not something I want. I tried erode the original image so that they are not connected anymore, but I’m having some trouble in my application now so I don’t want to erode the original image or the object anymore. Is there any simple way that I can make this work?
Thank you!