#C1528. Lexicographically Sorted Grid

    ID: 44743 Type: Default 1000ms 256MiB

Lexicographically Sorted Grid

Lexicographically Sorted Grid

You are given an N×MN \times M grid of lowercase letters. Your task is to determine whether it is possible to rearrange the rows of the grid so that the letters in every column become sorted in non-decreasing order. In other words, after rearranging, for every column jj (with 0j<M0 \le j < M) and for every row ii (with 0i<N10 \le i < N-1), the following condition must hold: [ \text{grid}[i][j] \leq \text{grid}[i+1][j] ]

For example, consider the following grid with N=3N=3, M=3M=3:

cba
fgh
ijk

If we rearrange the rows (if needed) so that the grid becomes:

cba
fgh
ijk

Then for each column the characters are in non-decreasing order (cfic \le f \le i, bgjb \le g \le j, ahka \le h \le k), and the answer is possible.

However, if no such rearrangement exists that will yield sorted columns, then the answer must be impossible.

inputFormat

The input is given via standard input and has the following format:

The first line contains two integers NN and MM, denoting the number of rows and columns respectively. Each of the next NN lines contains a string of length MM, representing one row of the grid.

You may assume that the grid consists of lowercase English letters.

outputFormat

Output a single line to standard output containing either possible or impossible. Output possible if there exists a rearrangement of the rows such that every column is sorted in non-decreasing order, and impossible otherwise.## sample

3 3
cba
fgh
ijk
possible

</p>