#K5081. Row with Maximum 1s

    ID: 28947 Type: Default 1000ms 256MiB

Row with Maximum 1s

Row with Maximum 1s

You are given a matrix composed exclusively of 0s and 1s. Your task is to find the row with the maximum number of 1s. In the event that there are multiple rows with the same maximum count, return the index of the first such row. If no row contains any 1s, or if the input is invalid, output None.

The input is provided via standard input (stdin). The first line contains an integer n representing the number of rows in the matrix. This is followed by n lines, each consisting of space-separated integers (either 0 or 1) representing a row of the matrix.

Note: Your solution should carefully validate the input. If any of the values in the matrix are not 0 or 1, or if the input format does not adhere to the specified format, then the program should output None.

The problem can be mathematically summarized as follows:

Let \( M \) be an \( n \times m \) matrix where \( M_{ij} \in \{0,1\} \). Find the smallest index \( i \) such that \( \sum_{j=1}^{m} M_{ij} = \max_{1 \le k \le n} \sum_{j=1}^{m} M_{kj} \), provided this maximum is greater than 0. Otherwise, output None.

inputFormat

The first line of input contains a single integer n indicating the number of rows in the matrix. The following n lines each contain a row of the matrix with space-separated integers. Each integer is guaranteed to be 0 or 1. If n is zero, it represents an empty matrix.

outputFormat

Output a single line containing the 0-based index of the row that has the maximum number of 1s. If no row contains a 1 or if the input is invalid, output None.

## sample
3
0 1 1
1 1 1
0 0 0
1