How to make the rectangles form a single closed polyline (red dotted line). And how to make it possible to select internal lines (blue dotted line)
Like this…
How to determine the internal?
Region intersection