Honestly, it's almost impossible to make a good solution to this without console.log. We need some way to inspect the undocumented grids and rectangles you've given us.
Why? All you need to know is the grid width\height, and whether a grid coord is empty. All of which is shown in the example.
My solution required marking each coord as non-empty when I dropped a rect on it. I was able to hack something that worked (non-zero-len string stuffed into the structure), but I'd much rather know what the structure was instead of guessing.