You are on page 1of 2

Problem Statement:

Jarus dropped his Bike's key in a manhole. His mother was very angry at him and she
told him to block some of the holes on the lid covering the manhole.

The lid is a rectangular grid of n rows and m columns. Each cell of the grid
represents a hole in the lid. Initially, no hole is blocked. The process for
blocking the holes is not very simple. Jarus uses following process to create a lid
with some blocked holes.

In one operation he can select a non-empty set of rows S1 and a non-empty set of
columns S2. For each row R in S1 and each column C in S2, Jarus blocks the
intersecting holes of R and C. Jarus can perform this operation any number of times
(possibly zero).

There's one more constraint: a row or a column can only be chosen at most once
among all operations. It means that if a row or a column is used in one operation,
it should not be used in any subsequent operations.

Now, Jarus's mother gives him a grid with some specific holes been blocked. Jarus
wants you to verify if it is possible to create such a grid.

Input Format:

The first line contains two space-separated integers n and m — the number of rows
and columns of the grid, respectively.

Each of the following n lines contains a string of m characters, each being either
'0 (number zero)' (denoting an open hole in the lid) or 'X' (denoting a blocked
hole in the lid), representing the grid given by Jarus's mom.

Constraints:

1 <= n,m <=50

Output Format:

If the given grid can be achieved by any valid sequence of operations, output
"POSSIBLE"; otherwise output "IMPOSSIBLE" (both without quotes).

Print each character in uppercase only.

Sample Input 0:

5 8
0X0X00X0
00000X00
0X0X00X0
X0X0000X
00000X00

Sample Output 0:

POSSIBLE

Sample Input 1:

5 5
00X00
00X00
XXXXX
00X00
00X00

Sample Output 1:

IMPOSSIBLE

Explanation Case 0:

For this case, the desired setup can be produced by 3 operations, as is shown
below:
Operation 1: Select row 1,3 and column 2,4,7 and block the intersecting holes.
Operation 2: Select row 4 and column 1,3,8 and block the intersecting holes.
Operation 3: Select row 2,5 and column 6 and block the intersecting holes.

Explanation Case 1:

For this case, the desired setup cannot be produced, since in order to block all
the holes of the center row, the third row and all columns must be selected in one
operation, but after that no column can be selected again, hence it won't be
possible to block the other holes in the center column.

You might also like