class Program {
private static void changeRowColumn(int[][] matrix, int row, int col) {
int rows = matrix.length;
int cols = matrix[0].length;
// -1 = different from the existing zeros
for (int j = 0; j < cols; j++) {
if (matrix[row][j] != 0) {
matrix[row][j] = -1;
}
}
for (int i = 0; i < rows; i++) {
if (matrix[i][col] != 0) {
matrix[i][col] = -1;
}
}
}
private static void changeBinaryMatrix(int[][] matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
if (rows == 0 || cols == 0) {
return;
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i][j] == 0) {
changeRowColumn(matrix, i, j);
}
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i][j] == -1) {
matrix[i][j] = 0;
}
}
}
}
private static void printMatrix(int[][] matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.print("\n");
}
}
public static void main(String[] args) {
int[][] matrix =
{
{1, 1, 0, 1, 1, 1},
{1, 1, 1, 1, 1, 1},
{1, 1, 0, 1, 1, 1},
{1, 1, 1, 1, 1, 1},
{1, 0, 1, 1, 1, 1}
};
changeBinaryMatrix(matrix);
printMatrix(matrix);
}
}
/*
run:
0 0 0 0 0 0
1 0 0 1 1 1
0 0 0 0 0 0
1 0 0 1 1 1
0 0 0 0 0 0
*/