Given an integer matrix of size, say, M x N
, you have to write a program to remove all the rows and columns consisting of all zeros. Your program must remove those rows and columns that consist of zero valued elements only. That is, if all the elements in a row (column) are zeros, then remove that row (column). All the remaining rows and columns should be output.
Input Specification:
- The first line of input will have two integers, say,
M
andN
.M
specifies the number of rows in the matrix andN
specifies the number of columns in the matrix. - This will be followed by
M
lines, each consisting ofN
integers. Note: Assume1 <= M <= 10
and1 <= N <= 10
. Also, you may assume that there is at least one non-zero element in the given matrix.
Output Specification:
- The output should be the resulting matrix.
- Each row of the output matrix should be terminated by a newline character.
- There should be exactly one space (not tab) between successive elements in each row.
Example1:
Sample Input:
4 4
1 2 3 4
0 0 0 0
5 6 7 8
0 0 0 3
Sample Output:
1 2 3 4
5 6 7 8
0 0 0 3
Example2:
Sample Input:
4 5
1 2 0 3 5
0 0 0 0 0
4 5 0 6 9
7 8 0 9 1
Sample Output:
1 2 3 5
4 5 6 9
7 8 9 1