#C9850. Minimum Operations to Transform Grid to All Ones

    ID: 53989 Type: Default 1000ms 256MiB

Minimum Operations to Transform Grid to All Ones

Minimum Operations to Transform Grid to All Ones

You are given a grid with dimensions (n) (number of rows) and (m) (number of columns), where each cell contains a non-negative integer. Your task is to determine the minimum number of operations required to transform the grid, based on a specific rule. According to the rule, the answer is computed as follows:

[ \text{operations} = \begin{cases} \max(\text{grid}) + 1, & \text{if } \max(\text{grid}) > 0 \ 1, & \text{if } \max(\text{grid}) = 0 \end{cases} ]

For example, if the grid is:

  • [[0, 0], [0, 0]] then \(\max = 0\) and the answer is 1.
  • [[0, 1, 0], [0, 0, 0], [0, 0, 0]] then \(\max = 1\) and the answer is 2.
  • [[1, 1, 1], [1, 1, 1], [1, 1, 1]] then \(\max = 1\) and the answer is 2.
  • [[0, 0, 0], [1000, 0, 0], [0, 0, 0]] then \(\max = 1000\) and the answer is 1001.
  • [[3, 2, 1], [4, 5, 6], [7, 8, 9]] then \(\max = 9\) and the answer is 10.

Your solution should read the grid from standard input and output the result to standard output.

inputFormat

The input is given in the following format:

The first line contains two integers (n) and (m), separated by a space, where (n) is the number of rows and (m) is the number of columns.

This is followed by (n) lines, each containing (m) space-separated non-negative integers representing the grid.

outputFormat

Output a single integer denoting the minimum number of operations required, computed as (\max(\text{grid}) + 1) if the maximum value is greater than 0, otherwise output 1.## sample

2 2
0 0
0 0
1