Looking for help in an algorythm to remove groups of elements from a 3D Array

You will need to have 3 nested loops to iterate over the 3 dimensions n, p, and q of your matrix, starting from the min-coord point (0, 0, 0). Saying DP is your dynamic programming matrix so that DP[i][j][k] stores the coordinates of the min-coord point of prism ending at coordinates (i, j, k), you have to find an induction relation between DP[i][j][k] and DP[s][u][v] with s <= i, u <= j and v <= k. The relation is kinda straightforward for 2D, not as much for 3D. Once you have this, you’ll need to backtrack your DP from the max-coord and immediately remove prisms from DP to avoid duplicates.