#K94732. Sorted Grid Verification
Sorted Grid Verification
Sorted Grid Verification
You are given an n × m grid consisting of digits. Your task is to determine whether the grid is sorted in a non-decreasing order both row‐wise and column‐wise.
A grid is considered sorted if all rows are in non-decreasing order and all columns are in non-decreasing order. Formally, if we denote the grid as \( G \) where \( G_{i,j} \) represents the digit in the \( i\)-th row and \( j\)-th column, the following must hold:
- For every row \( i \) and every \( j \) such that \( 1 \leq j < m \), \( G_{i,j} \leq G_{i,j+1} \).
- For every column \( j \) and every \( i \) such that \( 1 \leq i < n \), \( G_{i,j} \leq G_{i+1,j} \).
Output "YES" if the grid meets both conditions; otherwise, output "NO".
inputFormat
The first line contains two integers \( n \) and \( m \) (the number of rows and columns respectively).
Then follow \( n \) lines, each containing a string of \( m \) digits representing a row of the grid.
Input is to be read from standard input (stdin).
outputFormat
Output a single line containing "YES" if the grid is sorted as described, otherwise "NO".
Output should be written to standard output (stdout).
## sample3 3
123
456
789
YES
</p>