Scaling a squared grid maintaining its boundary limits

Hi there, i am trying to solve a problem using GH, but i don’t know how. I have a grid of squares. I want to scale this grid in a way that it maintains the same boundary limits, but it scales each square so that each space between square is all the same width. So i probably have to scale the squares and adjust their positions so that the overall length remains the same and the spaces between the squares are equal, but i could find a good way to do that in a parametric way. The inputs i want to give are: first the width and height of the overall bounds. Second, how many squares in x and y direction. Third, the gap size between each square. Then the size of each square will adjust depending on each of these 3 inputs. I don’t know if I was clear enough in the problem i want to solve. I tried to solve the problem but still could not adjust the position of each square so it has the same gap between them. I will upload what i tried to do.
EstudoSperficeAcustica.gh (23.4 KB)

Should the bounding box always be square aswell?.. As long as the internal squares remain square, and the outer bounds can be freely adjusted, it is a geometric impossibility that there will be a single spacing value for both horizontal and vertical.

Furthermore, if you can set the number of squares in a direction and the spacing in between them, it is possible that the sum of the product of the spacings between the squares will be larger than the set boundry dimensions.

Hey Volker, thanks a lot for your reply. You are correct, i think i missed some info and wasn’t able to explain it very well. The bounding box does not need to be in a square shape, depending on the inputs it will become a rectangle. The smaller “squares” inside, they may also become rectangles if the number of "squares in the x direction is different than in the y direction. About the spacing, it does not need to be the same in the vertical and horizontal, what i want is that the distribution of the squares and the gaps are distributed evenly. The example I uploaded i was able to scale each square by a scaling factor, so it starts with no gap and as i scale down the squares that gap will form. What i was not able to do is the even distribution os these squares. I will upload a picture to try to explain as well. I was almost there, but it still has some flaws, i dont know if i over complicated my algorithm to try to achive what i want. I hope this could clarify my doubt. Thanks again!!!

In case you do not have a solution yet, here is one.

I recorded myself working on the Grasshopper script. The videos are too large to upload here. Look in your messages for links to them later.

EstudoSperficeAcustica 2.gh (48.4 KB)

Hey Volker! Thanks again for your reply. I made progress try by my own, but I still had some little bugs in my solution. I really want to thank you for trying to solve this problem as well. Your solution helped me a lot. Its what I wanted, the only thing I will change is that I do not need the spacing between the squares and the bounding limit, but I think I know how to change that in your algorithm, I will try it out. I also want to thank you for the videos you sent me. It is really nice to follow your approach and it gives me other insights as well. Your awesome!!! Thanks again!!!