#K36217. Maximum Mineral Extraction

    ID: 25706 Type: Default 1000ms 256MiB

Maximum Mineral Extraction

Maximum Mineral Extraction

You are given a grid with N rows and M columns. Each cell in the grid contains an integer representing the amount of minerals present. You can extract minerals using one of the following three strategies:

  1. Extract the minerals from a single block (cell).
  2. Extract all minerals from an entire row.
  3. Extract all minerals from an entire column.

Your task is to determine the maximum amount of minerals that can be extracted by applying exactly one of the above strategies.

Mathematically, if $$G$$ is the grid with dimensions $$N \times M$$, compute the value:

$$\max\left\{ \max_{i,j} G_{ij},\; \max_{i} \sum_{j=1}^{M} G_{ij},\; \max_{j} \sum_{i=1}^{N} G_{ij} \right\} $$

For example, for the grid

[ \begin{pmatrix} 1 & 2 & 3 \ 4 & 5 & 6 \ 7 & 8 & 9 \end{pmatrix} ]

The maximum extraction is 24 (by taking the entire third row).

inputFormat

The first line contains two integers $$N$$ and $$M$$ separated by a space. The following $$N$$ lines each contain $$M$$ integers representing the mineral amounts in each cell of the grid.

outputFormat

Print a single integer representing the maximum amount of minerals that can be extracted.## sample

3 3
1 2 3
4 5 6
7 8 9
24