If someone can put this into stuff people can understand, that'd be great, but I'm not very good with code. So I'm just going to describe it.

n = size of one dimension in square grid
x = number of possible combinations in bounding box
y = number of bounding boxes

To find number of combinations in any given grid,
start with the highest size bounding box possible, in which possibilities will equal $$ y \times n^2 $$

For the next bounding box size, increase y by 1 and decrease n by 1. So like this:

$$ y+1 \times (n - 1)^2 $$

Keep adding and subtracting from one side until n = 1. Then, add all the products up.