All pizza boxes have the same size. The boxes are stacked into one pile per cell of an n by m grid, and every pile has a different height. Seen from the front, the grid shows the height of the tallest pile in each column. Seen from the side, it shows the height of the tallest pile in each row.
Find the largest number of boxes you can remove while both views stay the same. Figure I.1(a) is a grid of heights and Figure I.1(b) is the two views of that grid. Figure I.2 is what remains after removing as many boxes as possible from Figure I.1(a). Each number in a figure is the number of boxes stacked in that cell.

Figure I.1. (a) Grid of heights and (b) the corresponding views.

Figure I.2. Grid of heights after removing boxes.
Write a program that computes the maximum number of boxes that can be removed without changing the original front view and side view.