Given a boolean matrix of size RxC where each cell contains either 0 or 1, modify it such that if a matrix cell matrix[i][j] is 1 then all the cells in its i^{th} row and j^{th} column will become 1.
Example 1:
Input:
R = 2, C = 2
matrix[][] = {{1, 0},
              {0, 0}}
Output: 
1 1
1 0 
Explanation:
Only cell that has 1 is at (0,0) so all 
cells in row 0 are modified to 1 and all 
cells in column 0 are modified to 1.
Example 2:
Input:
R = 4, C = 3
matrix[][] = {{ 1, 0, 0},
              { 1, 0, 0},
              { 1, 0, 0},
              { 0, 0, 0}}
Output: 
1 1 1
1 1 1
1 1 1
1 0 0 
Explanation:
The position of cells that have 1 in
the original matrix are (0,0), (1,0)
and (2,0). Therefore, all cells in row
0,1,2 are and column 0 are modified to 1. 
Your Task:
You dont need to read input or print anything. Complete the function booleanMatrix() that takes the matrix as input parameter and modifies it in-place.
 
Expected Time Complexity: O(R * C)
Expected Auxiliary Space: O(R + C) 
 
Constraints:
1 ≤ R, C ≤ 1000
0 ≤ matrix[i][j] ≤ 1

Difficulty: MEDIUM
Tags: Matrices

Data structures
Skill types: Data structures
Expected Time Complexity: O(R * C)
Expected Auxiliary Space: O(R + C)

Starter code:
#User function Template for python3


#Function to modify the matrix such that if a matrix cell matrix[i][j]
#is 1 then all the cells in its ith row and jth column will become 1.
def booleanMatrix(matrix):
    # code here 

Solutions:
def booleanMatrix(matrix):
	fRow = 0
	(row, col) = (len(matrix), len(matrix[0]))
	for i in range(col):
		if matrix[0][i] == 1:
			fRow = 1
			break
	for r in range(row):
		for c in range(col):
			if matrix[r][c] == 1:
				matrix[r][0] = matrix[0][c] = 1
	for r in range(1, row):
		for c in range(1, col):
			if matrix[r][0] == 1 or matrix[0][c] == 1:
				matrix[r][c] = 1
	if matrix[0][0] == 1:
		for r in range(row):
			matrix[r][0] = 1
	if fRow:
		for c in range(col):
			matrix[0][c] = 1


def booleanMatrix(matrix):
	row = set()
	col = set()
	for i in range(len(matrix)):
		for j in range(len(matrix[0])):
			if matrix[i][j] == 1:
				row.add(i)
				col.add(j)
	for i in range(len(matrix)):
		for j in range(len(matrix[0])):
			if i in row or j in col:
				matrix[i][j] = 1


def booleanMatrix(arr):
	row = set()
	col = set()
	r = len(arr)
	c = len(arr[0])
	for i in range(r):
		for j in range(c):
			if arr[i][j] == 1:
				row.add(i)
				col.add(j)
	for i in range(r):
		for j in range(c):
			if i in row or j in col:
				arr[i][j] = 1
	return arr


def booleanMatrix(matrix):
	c = set()
	r = set()
	for i in range(len(matrix)):
		for j in range(len(matrix[0])):
			if matrix[i][j] == 1:
				c.add(j)
				r.add(i)
	for i in c:
		for j in range(len(matrix)):
			matrix[j][i] = 1
	for i in r:
		for j in range(len(matrix[0])):
			matrix[i][j] = 1
	return matrix


def booleanMatrix(matrix):
	(r, c) = ({}, {})
	row_count = len(matrix)
	column_count = len(matrix[0])
	for row in range(row_count):
		for column in range(column_count):
			if matrix[row][column]:
				r.update({row: 0})
				c.update({column: 0})
	for row in range(row_count):
		if row in r:
			matrix[row] = [1] * column_count
		for column in range(column_count):
			if column in c:
				matrix[row][column] = 1


def booleanMatrix(matrix):
	n = len(matrix)
	m = len(matrix[0])
	r = [0] * n
	c = [0] * m
	for i in range(n):
		for j in range(m):
			if matrix[i][j] == 1:
				r[i] = 1
				c[j] = 1
	for i in range(n):
		for j in range(m):
			if (r[i] == 1 or c[j] == 1) and matrix[i][j] != 1:
				matrix[i][j] = 1
	return matrix


def booleanMatrix(matrix):
	row = set()
	col = set()
	n = len(matrix)
	m = len(matrix[0])
	for i in range(n):
		for j in range(m):
			if matrix[i][j] == 1:
				row.add(i)
				col.add(j)
	for i in range(n):
		for j in range(m):
			if i in row:
				matrix[i][j] = 1
			if j in col:
				matrix[i][j] = 1
	return matrix


from collections import defaultdict

def booleanMatrix(matrix):
	row = defaultdict(int)
	col = defaultdict(int)
	for i in range(len(matrix)):
		for j in range(len(matrix[0])):
			if matrix[i][j] == 1:
				row[i] = 1
				col[j] = 1
	for i in range(len(matrix)):
		for j in range(len(matrix[0])):
			if row[i] == 1 or col[j] == 1:
				matrix[i][j] = 1


def booleanMatrix(matrix):
	m1 = {}
	m2 = {}
	for i in range(len(matrix)):
		for j in range(len(matrix[0])):
			if matrix[i][j] == 1:
				m1[i] = True
				m2[j] = True
	for i in m1:
		for j in range(len(matrix[0])):
			matrix[i][j] = 1
	for j in m2:
		for i in range(len(matrix)):
			matrix[i][j] = 1


def booleanMatrix(matrix):
	R = len(matrix)
	C = len(matrix[0])
	row_update = [False] * R
	col_update = [False] * C
	for i in range(R):
		for j in range(C):
			if matrix[i][j] == 1:
				row_update[i] = True
				col_update[j] = True
	for i in range(R):
		for j in range(C):
			if row_update[i] or col_update[j]:
				matrix[i][j] = 1


def booleanMatrix(matrix):
	(col0, rows, cols) = (0, len(matrix), len(matrix[0]))
	for i in range(0, rows):
		if matrix[i][0] == 1:
			col0 = 1
		for j in range(1, cols):
			if matrix[i][j] == 1:
				matrix[i][0] = matrix[0][j] = 1
	for i in range(rows - 1, -1, -1):
		for j in range(cols - 1, 0, -1):
			if matrix[i][0] == 1 or matrix[0][j] == 1:
				matrix[i][j] = 1
		if col0 == 1:
			matrix[i][0] = 1


def booleanMatrix(matrix):
	r = len(matrix)
	c = len(matrix[0])
	row = [0] * r
	col = [0] * c
	for i in range(r):
		for j in range(c):
			if matrix[i][j] == 1:
				row[i] = 1
				col[j] = 1
	for i in range(r):
		for j in range(c):
			if row[i] == 1 or col[j] == 1:
				matrix[i][j] = 1


def booleanMatrix(matrix):
	(row, col) = (set(), set())
	for r in range(len(matrix)):
		for c in range(len(matrix[0])):
			if matrix[r][c] == 1:
				row.add(r)
				col.add(c)
	for r in range(len(matrix)):
		for c in range(len(matrix[0])):
			if r in row or c in col:
				matrix[r][c] = 1
	return matrix


def booleanMatrix(matrix):
	q = {}
	n = len(matrix)
	m = len(matrix[0])
	for i in range(n):
		for j in range(m):
			if matrix[i][j] == 1:
				q[i, 'r'] = 1
				q[j, 'c'] = 1
	for i in q.keys():
		if i[1] == 'r':
			for x in range(m):
				matrix[i[0]][x] = 1
		else:
			for x in range(n):
				matrix[x][i[0]] = 1
	return matrix


def booleanMatrix(matrix):
	rowSet = set()
	colSet = set()
	for i in range(len(matrix)):
		for j in range(len(matrix[0])):
			if matrix[i][j] == 1:
				rowSet.add(i)
				colSet.add(j)
	for row in rowSet:
		for j in range(len(matrix[0])):
			matrix[row][j] = 1
	for col in colSet:
		for i in range(len(matrix)):
			matrix[i][col] = 1


def booleanMatrix(matrix):
	rows = []
	cols = []
	for i in range(len(matrix)):
		for j in range(len(matrix[0])):
			if matrix[i][j] == 1:
				if i not in rows:
					rows.append(i)
				if j not in cols:
					cols.append(j)
	if len(rows) == 0 and len(cols) == 0:
		return matrix
	for i in range(len(rows)):
		for j in range(len(matrix[0])):
			if matrix[rows[i]][j] == 0:
				matrix[rows[i]][j] = 1
	for i in range(len(cols)):
		for j in range(len(matrix)):
			if matrix[j][cols[i]] == 0:
				matrix[j][cols[i]] = 1
	return matrix


def booleanMatrix(matrix):
	rMatrix = [0] * len(matrix)
	cMatrix = [0] * len(matrix[0])
	for i in range(len(matrix)):
		for j in range(len(matrix[0])):
			if matrix[i][j] == 1:
				rMatrix[i] = 1
				cMatrix[j] = 1
	for i in range(len(matrix)):
		if rMatrix[i] == 1:
			for j in range(len(matrix[0])):
				matrix[i][j] = 1
	for i in range(len(matrix[0])):
		if cMatrix[i] == 1:
			for j in range(len(matrix)):
				matrix[j][i] = 1
	return


def booleanMatrix(matrix):
	(k, l) = ([0] * len(matrix), [0] * len(matrix[0]))
	for i in range(len(matrix)):
		for j in range(len(matrix[0])):
			if matrix[i][j] == 1:
				k[i] = 1
				l[j] = 1
	for i in range(len(matrix)):
		for j in range(len(matrix[0])):
			if k[i] == 1 or l[j] == 1:
				matrix[i][j] = 1


def booleanMatrix(matrix):
	rows = len(matrix)
	cols = len(matrix[0])
	row_updates = set()
	col_updates = set()
	for i in range(rows):
		for j in range(cols):
			if matrix[i][j] == 1:
				row_updates.add(i)
				col_updates.add(j)
	for i in row_updates:
		for j in range(cols):
			matrix[i][j] = 1
	for j in col_updates:
		for i in range(rows):
			matrix[i][j] = 1
	return matrix


def booleanMatrix(matrix):
	r = len(matrix)
	c = len(matrix[0])
	row = []
	col = []
	for i in range(r):
		for j in range(c):
			if matrix[i][j] == 1:
				row.append(i)
				col.append(j)
	row = list(set(row))
	col = list(set(col))
	for item in row:
		for i in range(c):
			matrix[item][i] = 1
	for item in col:
		for i in range(r):
			matrix[i][item] = 1


def booleanMatrix(matrix):
	n = len(matrix)
	m = len(matrix[0])
	col0 = 0
	for i in range(n):
		if matrix[i][0]:
			col0 = 1
		for j in range(1, m):
			if matrix[i][j]:
				matrix[i][0] = 1
				matrix[0][j] = 1
	for i in range(n - 1, -1, -1):
		for j in range(m - 1, 0, -1):
			if matrix[0][j] == 1 or matrix[i][0] == 1:
				matrix[i][j] = 1
		if col0 == 1:
			matrix[i][0] = 1


def booleanMatrix(matrix):
	arow = set()
	acol = set()
	for i in range(0, r):
		for j in range(0, c):
			if matrix[i][j] == 1:
				arow.add(i)
				acol.add(j)
	for i in range(0, r):
		for j in range(0, c):
			if i in arow:
				matrix[i][j] = 1
			if j in acol:
				matrix[i][j] = 1


def booleanMatrix(matrix):
	rows = set()
	columns = set()
	n = len(matrix)
	m = len(matrix[0])
	for row in range(n):
		for column in range(m):
			if matrix[row][column] == 1:
				rows.add(row)
				columns.add(column)
	for row in range(n):
		for column in range(m):
			if row in rows or column in columns:
				matrix[row][column] = 1
	return matrix


def booleanMatrix(matrix):
	(R, C) = (len(matrix), len(matrix[0]))
	visited_rows = [False] * R
	visited_cols = [False] * C

	def dfs(row, col):
		visited_rows[row] = True
		visited_cols[col] = True
		for i in range(R):
			if matrix[i][col] == 1 and (not visited_rows[i]):
				dfs(i, col)
		for j in range(C):
			if matrix[row][j] == 1 and (not visited_cols[j]):
				dfs(row, j)
	for i in range(R):
		for j in range(C):
			if matrix[i][j] == 1 and (not visited_rows[i]) and (not visited_cols[j]):
				dfs(i, j)
	for i in range(R):
		for j in range(C):
			if visited_rows[i] or visited_cols[j]:
				matrix[i][j] = 1


def booleanMatrix(matrix):
	is_row = False
	for i in range(len(matrix)):
		for j in range(len(matrix[0])):
			if matrix[0][j]:
				is_row = True
			if matrix[i][j]:
				matrix[0][j] = 1
				matrix[i][0] = 1
	for i in range(1, len(matrix)):
		for j in range(1, len(matrix[0])):
			if matrix[i][0] or matrix[0][j]:
				matrix[i][j] = 1
	if matrix[0][0]:
		for i in range(len(matrix)):
			matrix[i][0] = 1
	if is_row:
		for i in range(len(matrix[0])):
			matrix[0][i] = 1
	return matrix


def booleanMatrix(matrix):
	n = len(matrix)
	m = len(matrix[0])
	row = [0 for _ in range(n)]
	col = [0 for _ in range(m)]
	for i in range(n):
		for j in range(m):
			if matrix[i][j] == 1:
				row[i] = 1
				col[j] = 1
	for i in range(n):
		for j in range(m):
			if row[i] or col[j]:
				matrix[i][j] = 1
	return matrix


def booleanMatrix(matrix):
	col = 1
	for i in range(len(matrix)):
		if matrix[i][0] == 1:
			col = 0
		for j in range(1, len(matrix[0])):
			if matrix[i][j] == 1:
				matrix[i][0] = 1
				matrix[0][j] = 1
	for i in range(len(matrix) - 1, -1, -1):
		for j in range(len(matrix[0]) - 1, 0, -1):
			if matrix[i][0] == 1 or matrix[0][j] == 1:
				matrix[i][j] = 1
		if col == 0:
			matrix[i][0] = 1


def booleanMatrix(matrix):
	n = len(matrix)
	m = len(matrix[0])
	row = [0] * n
	column = [0] * m
	for i in range(0, n):
		for j in range(0, m):
			if matrix[i][j] == 1:
				row[i] = 1
				column[j] = 1
	for i in range(0, n):
		if row[i] == 1:
			for j in range(0, m):
				matrix[i][j] = 1
	for i in range(0, m):
		if column[i] == 1:
			for j in range(0, n):
				matrix[j][i] = 1


def booleanMatrix(mat):
	R = len(mat)
	C = len(mat[0])
	row = [0] * R
	col = [0] * C
	for i in range(0, R):
		row[i] = 0
	for i in range(0, C):
		col[i] = 0
	for i in range(0, R):
		for j in range(0, C):
			if mat[i][j] == 1:
				row[i] = 1
				col[j] = 1
	for i in range(0, R):
		for j in range(0, C):
			if row[i] == 1 or col[j] == 1:
				mat[i][j] = 1


def booleanMatrix(matrix):
	row = len(matrix)
	col = len(matrix[0])
	(bool_row, bool_col) = ([0] * row, [0] * col)
	for i in range(row):
		for j in range(col):
			if matrix[i][j] == 1:
				bool_row[i] = 1
				bool_col[j] = 1
	for i in range(row):
		if bool_row[i] == 1:
			for j in range(col):
				matrix[i][j] = 1
	for i in range(col):
		if bool_col[i] == 1:
			for j in range(row):
				matrix[j][i] = 1


def booleanMatrix(matrix):
	row_indices = set()
	col_indices = set()
	for i in range(len(matrix)):
		for j in range(len(matrix[0])):
			if matrix[i][j] == 1:
				row_indices.add(i)
				col_indices.add(j)
	for i in row_indices:
		for j in range(len(matrix[0])):
			matrix[i][j] = 1
	for j in col_indices:
		for i in range(len(matrix)):
			matrix[i][j] = 1


def booleanMatrix(matrix):
	len_row = len(matrix[0])
	col = []
	for row in matrix:
		ind_1s = [i for (i, e) in enumerate(row) if e == 1]
		if len(ind_1s) > 0:
			row[:] = [1 for i in range(len_row)]
			for i in ind_1s:
				if i not in col:
					col.append(i)
	for row in matrix:
		for i in col:
			row[i] = 1


def booleanMatrix(matrix):
	n = len(matrix)
	m = len(matrix[0])
	rows = [-1] * n
	cols = [-1] * m
	for i in range(0, n):
		for j in range(0, m):
			if matrix[i][j] == 1:
				rows[i] = 0
				cols[j] = 0
	for i in range(0, n):
		for j in range(0, m):
			if rows[i] == 0 or cols[j] == 0:
				matrix[i][j] = 1
	return matrix


def booleanMatrix(mat):
	(r, c) = (len(mat), len(mat[0]))
	x = 0
	for i in range(c):
		if mat[0][i]:
			x = 1
			break
	y = 0
	for i in range(r):
		if mat[i][0]:
			y = 1
			break
	for i in range(1, r):
		for j in range(1, c):
			if mat[i][j]:
				mat[i][0] = mat[0][j] = 1
	for i in range(1, r):
		if mat[i][0]:
			for j in range(1, c):
				mat[i][j] = 1
	for j in range(1, c):
		if mat[0][j]:
			for i in range(1, r):
				mat[i][j] = 1
	if x:
		for j in range(c):
			mat[0][j] = 1
	if y:
		for i in range(r):
			mat[i][0] = 1


def booleanMatrix(m):
	dr = {}
	dc = {}
	for i in range(r):
		for j in range(c):
			if m[i][j] == 1:
				if i not in dr.keys():
					dr[i] = 1
				if j not in dc.keys():
					dc[j] = 1
	for i in dr.keys():
		for j in range(c):
			m[i][j] = 1
	for j in dc.keys():
		for i in range(r):
			m[i][j] = 1


def booleanMatrix(matrix):
	row_set = set()
	col_set = set()
	for i in range(len(matrix)):
		for j in range(len(matrix[0])):
			if matrix[i][j] == 1:
				if i not in row_set:
					row_set.add(i)
				if j not in col_set:
					col_set.add(j)
	for i in range(len(matrix)):
		for j in range(len(matrix[0])):
			if i in row_set or j in col_set:
				matrix[i][j] = 1
	return matrix


def booleanMatrix(matrix):
	n = len(matrix)
	one_rows = []
	one_columns = []
	m = len(matrix[0])
	for i in range(n):
		for j in range(m):
			if matrix[i][j] == 1:
				one_rows.append(i)
				one_columns.append(j)
	one_rows = set(one_rows)
	one_rows = list(one_rows)
	one_columns = set(one_columns)
	one_columns = list(one_columns)
	for i in one_rows:
		for j in range(m):
			matrix[i][j] = 1
	for i in range(n):
		for j in one_columns:
			matrix[i][j] = 1


def booleanMatrix(ma):
	(n, m) = (len(ma), len(ma[0]))
	(r, c) = ({}, {})
	for i in range(n):
		for j in range(m):
			if ma[i][j] == 1:
				r[i] = 1
				c[j] = 1
	for t in r:
		ma[t] = [1 for i in range(m)]
	for t in c:
		for i in range(n):
			ma[i][t] = 1


def booleanMatrix(matrix):
	boolRow = False
	boolCol = False
	n = len(matrix)
	m = len(matrix[0])
	for i in range(n):
		for j in range(m):
			if i == 0:
				if matrix[i][j] == 1:
					boolRow = True
			if j == 0:
				if matrix[i][j] == 1:
					boolCol = True
			elif matrix[i][j] == 1:
				matrix[i][0] = 1
				matrix[0][j] = 1
	for i in range(1, n):
		for j in range(1, m):
			if matrix[i][0] == 1 or matrix[0][j] == 1:
				matrix[i][j] = 1
	if boolRow:
		for i in range(m):
			matrix[0][i] = 1
	if boolCol:
		for i in range(n):
			matrix[i][0] = 1


def booleanMatrix(matrix):
	r = len(matrix)
	c = len(matrix[0])
	row = [0 for _ in range(r)]
	column = [0 for _ in range(c)]
	for i in range(r):
		for j in range(c):
			if matrix[i][j] == 1:
				row[i] = 1
				column[j] = 1
	for i in range(r):
		if row[i] == 1:
			for j in range(c):
				matrix[i][j] = 1
	for i in range(c):
		if column[i] == 1:
			for j in range(r):
				matrix[j][i] = 1


def booleanMatrix(matrix):
	rowZero = False
	for r in range(len(matrix)):
		for c in range(len(matrix[0])):
			if matrix[r][c]:
				matrix[0][c] = 1
				if r > 0:
					matrix[r][0] = 1
				else:
					rowZero = True
	for r in range(1, len(matrix)):
		for c in range(1, len(matrix[0])):
			if matrix[0][c] or matrix[r][0]:
				matrix[r][c] = 1
	if matrix[0][0] == 1:
		for r in range(len(matrix)):
			matrix[r][0] = 1
	if rowZero:
		for c in range(len(matrix[0])):
			matrix[0][c] = 1


def booleanMatrix(matrix):
	rows = set()
	cols = set()
	for i in range(len(matrix)):
		for j in range(len(matrix[i])):
			if matrix[i][j] == 1:
				rows.add(i)
				cols.add(j)
	for i in range(len(matrix)):
		for j in range(len(matrix[i])):
			if i in rows or j in cols:
				matrix[i][j] = 1


def booleanMatrix(matrix):
	row = len(matrix)
	col = len(matrix[0])
	h = {}
	t = {}
	for i in range(row):
		for j in range(col):
			if matrix[i][j] == 1:
				t[i] = 0
				h[j] = 0
	for i in range(row):
		for j in range(col):
			if i in t or j in h:
				if matrix[i][j] != 1:
					matrix[i][j] = 1
				else:
					continue


def booleanMatrix(matrix):
	(row, col) = ([], [])
	for i in range(len(matrix)):
		if 1 not in matrix[i]:
			continue
		for j in range(len(matrix[0])):
			if matrix[i][j] == 1:
				if i not in row:
					row.append(i)
				if j not in col:
					col.append(j)
	for i in row:
		matrix[i] = [1] * len(matrix[0])
	for i in col:
		for j in range(len(matrix)):
			matrix[j][i] = 1
	return matrix


def booleanMatrix(matrix):
	R = len(matrix)
	C = len(matrix[0])
	rows_to_modify = set()
	cols_to_modify = set()
	for i in range(R):
		for j in range(C):
			if matrix[i][j] == 1:
				rows_to_modify.add(i)
				cols_to_modify.add(j)
	for i in rows_to_modify:
		for j in range(C):
			matrix[i][j] = 1
	for j in cols_to_modify:
		for i in range(R):
			matrix[i][j] = 1
	return matrix


def booleanMatrix(matrix):
	(row, col) = (len(matrix), len(matrix[0]))
	first_cell = False
	for i in range(row):
		for j in range(col):
			if matrix[i][j] == 1:
				matrix[0][j] = 1
				if i > 0:
					matrix[i][0] = 1
				else:
					first_cell = True
	for r in range(1, row):
		for c in range(1, col):
			if matrix[0][c] == 1 or matrix[r][0] == 1:
				matrix[r][c] = 1
	if matrix[0][0] == 1:
		for i in range(row):
			matrix[i][0] = 1
	if first_cell:
		for i in range(col):
			matrix[0][i] = 1
	return matrix


def booleanMatrix(A):
	n = len(A)
	m = len(A[0])
	N = [0 for i in range(n)]
	M = [0 for i in range(m)]
	for i in range(n):
		for j in range(m):
			if A[i][j] == 1:
				N[i] = 1
				M[j] = 1
	for i in range(n):
		if N[i] == 1:
			for j in range(m):
				A[i][j] = 1
	for i in range(m):
		if M[i] == 1:
			for j in range(n):
				A[j][i] = 1
	return A


def booleanMatrix(matrix):
	n = len(matrix)
	m = len(matrix[0])
	a = [0] * n
	b = [0] * m
	for i in range(n):
		for j in range(m):
			if matrix[i][j] == 1:
				a[i] = 1
				b[j] = 1
	for i in range(n):
		for j in range(m):
			if a[i] == 1 or b[j] == 1:
				matrix[i][j] = 1
	return matrix


def booleanMatrix(matrix):
	r = len(matrix)
	c = len(matrix[0])
	rows = [0] * len(matrix)
	columns = [0] * len(matrix[0])
	for i in range(r):
		for j in range(c):
			if matrix[i][j] == 1:
				rows[i] = 1
				columns[j] = 1
	for i in range(r):
		if rows[i] == 1:
			for j in range(c):
				matrix[i][j] = 1
	for i in range(c):
		if columns[i] == 1:
			for j in range(r):
				matrix[j][i] = 1


def booleanMatrix(matrix):
	r = len(matrix)
	c = len(matrix[0])
	t = matrix.copy()
	import numpy
	t1 = numpy.transpose(matrix)
	for i in range(r):
		if 1 in t[i]:
			for j in range(c):
				matrix[i][j] = 1
	for i in range(c):
		if 1 in t1[i]:
			for j in range(r):
				matrix[j][i] = 1
	return matrix


def booleanMatrix(matrix):
	r = len(matrix)
	c = len(matrix[0])
	rows = [any(row) for row in matrix]
	colums = [any(col) for col in zip(*matrix)]
	for i in range(r):
		for j in range(c):
			matrix[i][j] = int(any((rows[i], colums[j])))


import numpy as np

def booleanMatrix(matrix):
	row = len(matrix)
	col = len(matrix[0])
	dict1 = dict()
	matrix1 = np.array(matrix)
	dict2 = dict()
	dict3 = dict()
	for i in range(row):
		for j in range(col):
			if i in dict2 and j in dict3:
				pass
			elif matrix[i][j] == 1:
				dict1[i, j] = 1
				if i not in dict2:
					matrix1[i, :] = 1
					dict2[i] = 1
				if j not in dict3:
					matrix1[:, j] = 1
					dict3[j] = 1
	matrix[:] = matrix1
	return matrix


def booleanMatrix(matrix):
	l = len(matrix)
	m = len(matrix[0])
	r = [0] * l
	c = [0] * m
	for i in range(l):
		for j in range(m):
			if matrix[i][j] == 1:
				r[i] = 1
				c[j] = 1
	for i in range(l):
		for j in range(m):
			if c[j] == 1 or r[i] == 1:
				matrix[i][j] = 1


def booleanMatrix(matrix):
	row = set()
	column = set()
	r = len(matrix)
	c = len(matrix[0])
	for i in range(r):
		for j in range(c):
			if matrix[i][j] == 1:
				row.add(i)
				column.add(j)
	row = sorted(list(row))
	col = sorted(list(column))
	for i in row:
		for j in range(c):
			matrix[i][j] = 1
	for i in col:
		for j in range(r):
			matrix[j][i] = 1


def booleanMatrix(matrix):
	n = len(matrix)
	m = len(matrix[0])
	c = set()
	r = set()
	for i in range(n):
		for j in range(m):
			if matrix[i][j] == 1:
				if i not in r:
					r.add(i)
					for k in range(m):
						if matrix[i][k] == 0:
							matrix[i][k] = -1
	for i in range(n):
		for j in range(m):
			if matrix[i][j] == 1:
				if j not in c:
					c.add(j)
					for k in range(n):
						if matrix[k][j] == 0:
							matrix[k][j] = -1
	for i in range(n):
		for j in range(m):
			if matrix[i][j] == -1:
				matrix[i][j] = 1
	return matrix


def booleanMatrix(matrix):
	n = len(matrix)
	m = len(matrix[0])
	c = set()
	for i in range(n):
		temp = matrix[i]
		if 1 in temp:
			x = []
			for j in temp:
				if j == 0:
					x.append(-1)
				else:
					x.append(1)
			matrix[i] = x
	for i in range(n):
		for j in range(m):
			if matrix[i][j] == 1:
				if j not in c:
					c.add(j)
					for k in range(n):
						if matrix[k][j] == 0:
							matrix[k][j] = -1
	for i in range(n):
		for j in range(m):
			if matrix[i][j] == -1:
				matrix[i][j] = 1
	return matrix


def booleanMatrix(matrix):
	sr = set()
	sc = set()
	for i in range(len(matrix)):
		for j in range(len(matrix[0])):
			if matrix[i][j] == 1:
				sr.add(i)
				sc.add(j)
	for i in range(len(matrix)):
		for j in range(len(matrix[0])):
			if i in sr:
				matrix[i][j] = 1
			elif j in sc:
				matrix[i][j] = 1
	return matrix


def booleanMatrix(matrix):
	row = len(matrix)
	col = len(matrix[0])
	rowzero = False
	for i in range(0, row):
		for j in range(0, col):
			if matrix[i][j] == 1:
				matrix[0][j] = 1
				if i > 0:
					matrix[i][0] = 1
				else:
					rowzero = True
	for i in range(1, row):
		for j in range(1, col):
			if matrix[i][0] or matrix[0][j]:
				matrix[i][j] = 1
	if matrix[0][0] == 1:
		for i in range(0, row):
			matrix[i][0] = 1
	if rowzero:
		for i in range(1, col):
			matrix[0][i] = 1