Given a rectangle with dimensions N*M and an integer K. You divide this rectangle into smaller sub-rectangles such that the given conditions are satisfied:
• Sub-rectangles must be parallel to the axis of the larger rectangle with dimensions N*M
• Every sub-rectangle has at least one edge on the larger rectangle edge. Informally, there is no sub-rectangle that is surrounded by other sub-rectangles.
For a sub-rectangle with area S, the cost of this sub-rectangle is (S-K)²
Calculate the minimum total cost to divide the larger rectangle into smaller sub-rectangles.