Random reduce results filtering

hello everyone, I am trying to generate shapes by randomly reducing a number of squares from a grid of squares.
However, my goal is to get only the cases in which the remaining squares are all connected by at least one vertex. Attached is a more explanatory photo.

Also if anyone knows of a way to calculate the number of possible combinations with these constraints.

I ask for your help because I have been trying for weeks but can’t find solutions (I am a beginner).

Also attached is the GH file.
Thank you for your help

Random Shape.gh (21.7 KB)
Immagine 2022-05-23 124040|690x309