#C6497. Maximum Number of Visible Boxes
Maximum Number of Visible Boxes
Maximum Number of Visible Boxes
You are given a grid of dimensions \(M \times N\) where each cell contains a non-negative integer representing the height of a box in that cell. A cell with value 0 indicates there is no box. A box is visible from a row (or column) if its height is greater than zero, and only distinct positive heights count towards visibility. In other words, the number of visible boxes in a row (or column) is the number of distinct positive integers in that row (or column).
Your task is to compute the maximum number of visible boxes considering all rows and all columns of the grid.
More formally:
Given a grid \(A\) of size \(M \times N\), for each row \(i\) compute \(R_i = |\{ A[i][j] : A[i][j] > 0 \}|\) and for each column \(j\) compute \(C_j = |\{ A[i][j] : A[i][j] > 0 \}|\). Find and print the maximum value among all \(R_i\) and \(C_j\).
inputFormat
The first line contains two integers \(M\) and \(N\), separated by a space, representing the number of rows and columns respectively.
Each of the next \(M\) lines contains \(N\) integers separated by spaces representing the grid.
outputFormat
Output a single integer: the maximum number of visible boxes from any row or column.
## sample4 5
1 0 4 2 3
3 2 4 0 1
1 1 0 2 2
0 2 4 3 0
4