Just keep a running total of the number of rows you have filled so-far, and place the next matrix starting from the next row.
This assumes you always want to place the next underneath the previous. If you want to place as many as possible to the right that fit inside the width, and then move down to the next available row, then the logic would be a little different: in that case the next available row is your starting row plus the maximum number of rows of what you just placed across.