Generating all combinations of numbers in N x M arrays