Given a matrix of  size n x m. Your task is to make Zeroes, that means in whole matrix when you find a zero, convert its upper, lower, left, and right value to zero and make that element the sum of the upper, lower, left and right value. Do the following tasks according to the initial matrix.
 
Example 1:
Input: matrix = {{1, 2, 3, 4},
                 {5, 6, 0, 7}, 
                 {8, 9, 4, 6},
                 {8, 4, 5, 2}}
Output: {{1, 2, 0, 4}, 
         {5, 0, 20, 0},
         {8, 9, 0, 6}, 
         {8, 4, 5, 2}}
Explanation: As matrix[1][2] = 0, we will
perform the operation here. Then matrix[1][2]
= matrix[0][2] + matrix[2][2] + matrix[1][1] 
+ matrix[1][3] and matrix[0][2] = matrix[2][2] 
= matrix[1][1] = matrix[1][3] = 0.
Example 2:
Input: matrix = {{1, 2}, 
                 {3, 4}}
output: {{1, 2}, 
         {3, 4}}
Your Task:
You don't need to read or print anything. Your task is to complete the function MakeZeros() which takes the matrix as input parameter and does the given task according to initial matrix. You don't need to return anything. The driver code prints the modified matrix itself in the output.
 
Expected Time Complexity: O(n * m)
Expected Space Complexity: O(n * m)
 
Constraints:
1 ≤ n, m ≤ 100
0 ≤ matrix[i][j] ≤ 100, where 0 ≤ i ≤ n and 0 ≤ j ≤ m

Source: geeksforgeeks | Difficulty: EASY
Tags: Matrices

Data structures

Starter code: #User function Template for python3



class Solution:

	def MakeZeros(self, matrix):

		# Code here

Solutions:
import copy

class Solution:

	def MakeZeros(self, matrix):
		zeroCoordinates = []
		hashMap = {}
		(m, n) = (len(matrix), len(matrix[0]))
		newMatrix = copy.deepcopy(matrix)
		for i in range(len(matrix)):
			for j in range(len(matrix[0])):
				if matrix[i][j] == 0:
					zeroCoordinates.append((i, j))
				hashMap[i, j] = matrix[i][j]
		for coordinate in zeroCoordinates:
			dr = [-1, 0, 1, 0]
			dc = [0, -1, 0, 1]
			totalSum = 0
			for i in range(4):
				newRow = coordinate[0] + dr[i]
				newCol = coordinate[1] + dc[i]
				if newRow >= 0 and newRow < m and (newCol >= 0) and (newCol < n):
					totalSum += hashMap[newRow, newCol]
					matrix[newRow][newCol] = 0
				matrix[coordinate[0]][coordinate[1]] = totalSum
		return


def zeros(i, j):
	if i != 0:
		matrix[i - 1][j] = 0
	if i != n - 1:
		matrix[i + 1][j] = 0
	if j != 0:
		matrix[i][j - 1] = 0
	if j != m - 1:
		matrix[i][j + 1] = 0

class Solution:

	def MakeZeros(self, matrix):
		p = []
		for i in range(n):
			for j in range(m):
				if matrix[i][j] == 0:
					p.append([i, j])
		x = []
		for k in p:
			i = k[0]
			j = k[1]
			x.append((matrix[i - 1][j] if i != 0 else 0) + (matrix[i][j - 1] if j != 0 else 0) + (matrix[i + 1][j] if i != n - 1 else 0) + (matrix[i][j + 1] if j != m - 1 else 0))
		for i in range(len(p)):
			a = p[i][0]
			b = p[i][1]
			matrix[a][b] = x[i]
			zeros(a, b)


import copy

class Solution:

	def MakeZeros(self, matrix):
		mat = copy.deepcopy(matrix)
		s = 0
		for i in range(len(mat)):
			for j in range(len(mat[i])):
				if mat[i][j] == 0:
					if i > 0:
						s += mat[i - 1][j]
						matrix[i - 1][j] = 0
					if i < len(mat) - 1:
						s += mat[i + 1][j]
						matrix[i + 1][j] = 0
					if j > 0:
						s += mat[i][j - 1]
						matrix[i][j - 1] = 0
					if j < len(mat[i]) - 1:
						s += mat[i][j + 1]
						matrix[i][j + 1] = 0
					matrix[i][j] = s
					s = 0
		return matrix


import copy

class Solution:

	def MakeZeros(self, matrix):
		l_r = len(matrix)
		l_c = len(matrix[0])
		p_c = -1
		p_r = -1
		c_m = copy.deepcopy(matrix)
		arr = []
		for r in range(l_r):
			for c in range(l_c):
				if c_m[r][c] == 0:
					p_r = r
					p_c = c
					arr.append([r, c])
		for obj in arr:
			(p_r, p_c) = (obj[0], obj[1])
			top_r = p_r - 1
			top_c = p_c
			b_r = p_r + 1
			b_c = p_c
			left_r = p_r
			left_c = p_c - 1
			right_r = p_r
			right_c = p_c + 1
			sum_s = 0
			if top_r >= 0:
				sum_s += c_m[top_r][top_c]
				matrix[top_r][p_c] = 0
			if b_r < l_r:
				sum_s += c_m[b_r][b_c]
				matrix[b_r][b_c] = 0
			if left_c >= 0:
				sum_s += c_m[left_r][left_c]
				matrix[left_r][left_c] = 0
			if right_c < l_c:
				sum_s += c_m[right_r][right_c]
				matrix[right_r][right_c] = 0
			matrix[p_r][p_c] = sum_s


import copy

class Solution:

	def MakeZeros(self, matrix):
		i = []
		j = []
		duplicate = copy.deepcopy(matrix)
		rows = len(matrix)
		cols = len(matrix[0])
		for row in range(rows):
			for col in range(cols):
				if matrix[row][col] == 0:
					i.append(row)
					j.append(col)
		for (row, col) in zip(i, j):
			sum = 0
			if row - 1 >= 0:
				sum += duplicate[row - 1][col]
				matrix[row - 1][col] = 0
			if row + 1 < rows:
				sum += duplicate[row + 1][col]
				matrix[row + 1][col] = 0
			if col - 1 >= 0:
				sum += duplicate[row][col - 1]
				matrix[row][col - 1] = 0
			if col + 1 < cols:
				sum += duplicate[row][col + 1]
				matrix[row][col + 1] = 0
			matrix[row][col] = sum


class Solution:

	def MakeZeros(self, matrix):
		n = len(matrix)
		m = len(matrix[0])
		mark = [[0] * m for i in range(n)]
		for i in range(n):
			for j in range(m):
				mark[i][j] = matrix[i][j]
		directions = [(1, 0), (-1, 0), (0, -1), (0, 1)]
		for i in range(n):
			for j in range(m):
				total = 0
				if matrix[i][j] == 0:
					for direction in directions:
						if i + direction[0] in range(0, n) and j + direction[1] in range(0, m):
							total += matrix[i + direction[0]][j + direction[1]]
							if mark[i + direction[0]][j + direction[1]] != 0:
								mark[i + direction[0]][j + direction[1]] = -1
					mark[i][j] = total
		for i in range(n):
			for j in range(m):
				if mark[i][j] == -1:
					matrix[i][j] = 0
				else:
					matrix[i][j] = mark[i][j]


class Solution:

	def MakeZeros(self, matrix):
		temp = [[matrix[i][j] for j in range(len(matrix[0]))] for i in range(len(matrix))]
		for i in range(len(matrix)):
			for j in range(len(matrix[0])):
				if temp[i][j] == 0:
					sum = 0
					delrow = [-1, 1, 0, 0]
					delcol = [0, 0, -1, 1]
					for k in range(4):
						nrow = i + delrow[k]
						ncol = j + delcol[k]
						if 0 <= nrow < len(matrix) and 0 <= ncol < len(matrix[0]):
							sum += temp[nrow][ncol]
							matrix[nrow][ncol] = 0
					matrix[i][j] = sum


class Solution:

	def MakeZeros(self, matrix):
		n = len(matrix)
		m = len(matrix[0])
		temp = [row[:] for row in matrix]
		for i in range(n):
			for j in range(m):
				if temp[i][j] == 0:
					if i + 1 < n:
						matrix[i][j] += temp[i + 1][j]
						matrix[i + 1][j] = 0
					if i - 1 >= 0:
						matrix[i][j] += temp[i - 1][j]
						matrix[i - 1][j] = 0
					if j + 1 < m:
						matrix[i][j] += temp[i][j + 1]
						matrix[i][j + 1] = 0
					if j - 1 >= 0:
						matrix[i][j] += temp[i][j - 1]
						matrix[i][j - 1] = 0


class Solution:

	def MakeZeros(self, matrix):
		arr = []
		r = len(matrix)
		c = len(matrix[0])
		for i in range(r):
			for j in range(c):
				if matrix[i][j] == 0:
					temp = 0
					if i > 0:
						temp += matrix[i - 1][j]
					if i < r - 1:
						temp += matrix[i + 1][j]
					if j > 0:
						temp += matrix[i][j - 1]
					if j < c - 1:
						temp += matrix[i][j + 1]
					arr.append([i, j, temp])
		for (i, j, temp) in arr:
			matrix[i][j] = temp
			if i > 0:
				matrix[i - 1][j] = 0
			if i < r - 1:
				matrix[i + 1][j] = 0
			if j > 0:
				matrix[i][j - 1] = 0
			if j < c - 1:
				matrix[i][j + 1] = 0


class Solution(object):

	def MakeZeros(self, matrix):

		def isValid(row, col):
			return 0 <= row < rows and 0 <= col < cols
		(rows, cols) = (len(matrix), len(matrix[0]))
		directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
		lookup = [[matrix[row][col] for col in range(cols)] for row in range(rows)]
		for row in range(rows):
			for col in range(cols):
				if matrix[row][col] == 0:
					newValue = 0
					for (r_var, c_var) in directions:
						(next_row, next_col) = (row + r_var, col + c_var)
						if isValid(next_row, next_col):
							newValue += matrix[next_row][next_col]
							lookup[next_row][next_col] = -1
					lookup[row][col] = newValue
		for row in range(rows):
			for col in range(cols):
				value = lookup[row][col]
				if value == -1:
					matrix[row][col] = 0
				else:
					matrix[row][col] = lookup[row][col]
		return matrix


class Solution:

	def MakeZeros(self, matrix):
		r = len(matrix)
		c = len(matrix[0])
		copy = [[0] * c for i in range(r)]
		for i in range(r):
			for j in range(c):
				copy[i][j] = matrix[i][j]
		for i in range(r):
			for j in range(c):
				if matrix[i][j] == 0:
					s = 0
					if i - 1 >= 0:
						s += matrix[i - 1][j]
						copy[i - 1][j] = 0
					if j + 1 < c:
						s += matrix[i][j + 1]
						copy[i][j + 1] = 0
					if i + 1 < r:
						s += matrix[i + 1][j]
						copy[i + 1][j] = 0
					if j - 1 >= 0:
						s += matrix[i][j - 1]
						copy[i][j - 1] = 0
					copy[i][j] = s
		for i in range(r):
			for j in range(c):
				matrix[i][j] = copy[i][j]


import copy

class Solution:

	def MakeZeros(self, matrix):
		arr = copy.deepcopy(matrix)
		n = len(matrix[0])
		m = len(matrix)
		if m <= 1 and n <= 1:
			return
		if m == 1:
			for i in range(n):
				if arr[0][i] == 0:
					s = 0
					if i == 0:
						s += arr[0][i + 1]
						matrix[0][i + 1] = 0
					elif i == n - 1:
						s += arr[0][i - 1]
						matrix[0][i - 1] = 0
					else:
						s += arr[0][i - 1] + arr[0][i + 1]
						matrix[0][i - 1] = matrix[0][i + 1] = 0
					matrix[0][i] = s
			return
		if n == 1:
			for j in range(m):
				if arr[j][0] == 0:
					s = 0
					if j == 0:
						s += arr[j + 1][0]
						matrix[j + 1][0] = 0
					elif j == m - 1:
						s += arr[j - 1][0]
						matrix[j - 1][0] = 0
					else:
						s += arr[j - 1][0] + arr[j + 1][0]
						matrix[j - 1][0] = matrix[j + 1][0] = 0
					matrix[j][0] = s
			return
		for i in range(m):
			for j in range(n):
				if arr[i][j] == 0:
					s = 0
					if i == 0:
						s += arr[i + 1][j]
						matrix[i + 1][j] = 0
						if j == 0:
							s += arr[i][j + 1]
							matrix[i][j + 1] = 0
						elif j == n - 1:
							s += arr[i][j - 1]
							matrix[i][j - 1] = 0
						else:
							s += arr[i][j + 1] + arr[i][j - 1]
							matrix[i][j + 1] = matrix[i][j - 1] = 0
					elif i == m - 1:
						s += arr[i - 1][j]
						matrix[i - 1][j] = 0
						if j == 0:
							s += arr[i][j + 1]
							matrix[i][j + 1] = 0
						elif j == n - 1:
							s += arr[i][j - 1]
							matrix[i][j - 1] = 0
						else:
							s += arr[i][j - 1] + arr[i][j + 1]
							matrix[i][j - 1] = matrix[i][j + 1] = 0
					elif j == 0:
						s = arr[i - 1][j] + arr[i + 1][j] + arr[i][j + 1]
						matrix[i - 1][j] = matrix[i + 1][j] = matrix[i][j + 1] = 0
					elif j == n - 1:
						s += arr[i - 1][j] + arr[i + 1][j] + arr[i][j - 1]
						matrix[i - 1][j] = matrix[i + 1][j] = matrix[i][j - 1] = 0
					else:
						s += arr[i - 1][j] + arr[i + 1][j] + arr[i][j + 1] + arr[i][j - 1]
						matrix[i - 1][j] = matrix[i + 1][j] = matrix[i][j + 1] = matrix[i][j - 1] = 0
					matrix[i][j] = s


class Solution:

	def MakeZeros(self, m):
		(rl, cl) = (len(m), len(m[0]))
		ans = [[-1] * cl for i in range(rl)]
		for i in range(rl):
			for j in range(cl):
				if m[i][j] == 0:
					s = 0
					if i - 1 >= 0:
						s += m[i - 1][j]
						ans[i - 1][j] = 0
					if j + 1 < cl:
						s += m[i][j + 1]
						ans[i][j + 1] = 0
					if i + 1 < rl:
						s += m[i + 1][j]
						ans[i + 1][j] = 0
					if j - 1 >= 0:
						s += m[i][j - 1]
						ans[i][j - 1] = 0
					ans[i][j] = s
		for i in range(rl):
			for j in range(cl):
				if ans[i][j] == -1:
					ans[i][j] = m[i][j]
		for i in range(rl):
			for j in range(cl):
				m[i][j] = ans[i][j]


import numpy
import copy

class Solution:

	def MakeZeros(self, matrix):
		d = numpy.shape(matrix)
		matrixd = copy.deepcopy(matrix)
		for i in range(d[0]):
			for j in range(d[1]):
				if matrixd[i][j] == 0:
					sum1 = 0
					if i - 1 >= 0:
						sum1 += matrixd[i - 1][j]
						matrix[i - 1][j] = 0
					if j - 1 >= 0:
						sum1 += matrixd[i][j - 1]
						matrix[i][j - 1] = 0
					if i + 1 < d[0]:
						sum1 += matrixd[i + 1][j]
						matrix[i + 1][j] = 0
					if j + 1 < d[1]:
						sum1 += matrixd[i][j + 1]
						matrix[i][j + 1] = 0
					matrix[i][j] = sum1


class Solution:

	def MakeZeros(self, matrix):
		n = len(matrix)
		m = len(matrix[0])
		mat = [['a' for i in range(m)] for i in range(n)]
		for i in range(n):
			for j in range(m):
				if matrix[i][j] == 0:
					s = 0
					if i - 1 >= 0:
						s += matrix[i - 1][j]
						mat[i - 1][j] = 0
					if i + 1 < n:
						s += matrix[i + 1][j]
						mat[i + 1][j] = 0
					if j - 1 >= 0:
						s += matrix[i][j - 1]
						mat[i][j - 1] = 0
					if j + 1 < m:
						s += matrix[i][j + 1]
						mat[i][j + 1] = 0
					mat[i][j] = s
		for i in range(n):
			for j in range(m):
				if mat[i][j] != 'a':
					matrix[i][j] = mat[i][j]


import copy

class Solution:

	def MakeZeros(self, matrix):
		mat = copy.deepcopy(matrix)
		n = len(matrix)
		m = len(matrix[0])
		queue = []
		for i in range(n):
			for j in range(m):
				if matrix[i][j] == 0:
					queue.append((i, j))
		while queue:
			summ = 0
			(x, y) = queue.pop(0)
			if x > 0:
				matrix[x][y] += mat[x - 1][y]
				matrix[x - 1][y] = 0
			if x < n - 1:
				matrix[x][y] += mat[x + 1][y]
				matrix[x + 1][y] = 0
			if y > 0:
				matrix[x][y] += mat[x][y - 1]
				matrix[x][y - 1] = 0
			if y < m - 1:
				matrix[x][y] += mat[x][y + 1]
				matrix[x][y + 1] = 0


class Solution:

	def MakeZeros(self, matrix):
		(n, m) = (len(matrix), len(matrix[0]))
		ans = [[0 for _ in range(m)] for _ in range(n)]
		visited = [[False for _ in range(m)] for j in range(n)]
		dx = [-1, 0, 1, 0]
		dy = [0, 1, 0, -1]
		for i in range(n):
			for j in range(m):
				temp = 0
				if matrix[i][j] == 0:
					for k in range(4):
						nx = i + dx[k]
						ny = j + dy[k]
						if nx >= 0 and nx < n and (ny >= 0) and (ny < m):
							temp += matrix[nx][ny]
							ans[nx][ny] = 0
							visited[nx][ny] = True
					ans[i][j] = temp
				elif visited[i][j] == False:
					ans[i][j] = matrix[i][j]
		for i in range(n):
			for j in range(m):
				matrix[i][j] = ans[i][j]


import copy

class Solution:

	def MakeZeros(self, matrix):
		n = len(matrix)
		m = len(matrix[0])
		t = copy.deepcopy(matrix)
		for i in range(n):
			for j in range(m):
				if t[i][j] == 0:
					if i - 1 >= 0:
						matrix[i][j] += t[i - 1][j]
						matrix[i - 1][j] = 0
					if i + 1 < n:
						matrix[i][j] += t[i + 1][j]
						matrix[i + 1][j] = 0
					if j - 1 >= 0:
						matrix[i][j] += t[i][j - 1]
						matrix[i][j - 1] = 0
					if j + 1 < m:
						matrix[i][j] += t[i][j + 1]
						matrix[i][j + 1] = 0
		return matrix


class Solution:

	def MakeZeros(self, matrix):
		temp = [[matrix[i][j] for j in range(len(matrix[0]))] for i in range(len(matrix))]
		for i in range(len(matrix)):
			for j in range(len(matrix[0])):
				s = 0
				if temp[i][j] == 0:
					if i + 1 < len(matrix):
						s += temp[i + 1][j]
						matrix[i + 1][j] = 0
					if i - 1 >= 0:
						s += temp[i - 1][j]
						matrix[i - 1][j] = 0
					if j + 1 < len(matrix[0]):
						s += temp[i][j + 1]
						matrix[i][j + 1] = 0
					if j - 1 >= 0:
						s += temp[i][j - 1]
						matrix[i][j - 1] = 0
					matrix[i][j] = s
				elif matrix[i][j] == 0:
					continue
				else:
					matrix[i][j] = temp[i][j]
		return matrix


class Solution:

	def MakeZeros(self, matrix):
		l = []
		n = len(matrix)
		m = len(matrix[0])
		for k in range(n):
			for f in range(m):
				if matrix[k][f] == 0:
					l += [[k, f]]
		indexes = []
		sumof = []
		for (i, j) in l:
			sum = 0
			if 0 <= i < n and 0 <= j - 1 < m:
				sum += matrix[i][j - 1]
				indexes += [[i, j - 1]]
			if 0 <= i < n and 0 <= j + 1 < m:
				sum += matrix[i][j + 1]
				indexes += [[i, j + 1]]
			if 0 <= i - 1 < n and 0 <= j < m:
				sum += matrix[i - 1][j]
				indexes += [[i - 1, j]]
			if 0 <= i + 1 < n and 0 <= j < m:
				sum += matrix[i + 1][j]
				indexes += [[i + 1, j]]
			sumof += [[i, j, sum]]
		for (i, j) in indexes:
			matrix[i][j] = 0
		for (i, j, sum) in sumof:
			matrix[i][j] = sum
			if [i - 1, j] in indexes:
				matrix[i - 1][j] = 0
			if [i, j - 1] in indexes:
				matrix[i][j - 1] = 0


class Solution:

	def MakeZeros(self, mat):
		n = len(mat)
		m = len(mat[0])
		mat2 = [[0 for i in range(m)] for j in range(n)]
		for i in range(n):
			for j in range(m):
				if mat[i][j] == 0:
					sm = 0
					if i > 0:
						sm += mat[i - 1][j]
						mat2[i - 1][j] = -1
					if j > 0:
						sm += mat[i][j - 1]
						mat2[i][j - 1] = -1
					if i < n - 1:
						sm += mat[i + 1][j]
						mat2[i + 1][j] = -1
					if j < m - 1:
						sm += mat[i][j + 1]
						mat2[i][j + 1] = -1
					mat2[i][j] = sm
		for i in range(n):
			for j in range(m):
				if mat2[i][j] == -1:
					mat[i][j] = 0
				elif mat2[i][j] > 0:
					mat[i][j] = mat2[i][j]


class Solution:

	def MakeZeros(self, matrix):
		n = len(matrix)
		m = len(matrix[0])
		a = []
		b = []
		res = []
		for i in range(n):
			t = [0] * m
			res.append(t)
		for i in range(n):
			for j in range(m):
				if matrix[i][j] == 0:
					a.append(i)
					b.append(j)
				res[i][j] = matrix[i][j]
		for x in range(len(a)):
			i = a[x]
			j = b[x]
			cnt = 0
			if i - 1 >= 0:
				cnt += matrix[i - 1][j]
				res[i - 1][j] = 0
			if i + 1 < n:
				cnt += matrix[i + 1][j]
				res[i + 1][j] = 0
			if j - 1 >= 0:
				cnt += matrix[i][j - 1]
				res[i][j - 1] = 0
			if j + 1 < m:
				cnt += matrix[i][j + 1]
				res[i][j + 1] = 0
			res[i][j] = cnt
		for i in range(n):
			for j in range(m):
				matrix[i][j] = res[i][j]


class Solution:

	def MakeZeros(self, matrix):
		temp = []
		for row in range(len(matrix)):
			a = []
			for col in range(len(matrix[0])):
				a.append(matrix[row][col])
			temp.append(a)
		for row in range(len(temp)):
			for col in range(len(temp[0])):
				if temp[row][col] == 0:
					if row - 1 >= 0:
						matrix[row][col] += temp[row - 1][col]
						matrix[row - 1][col] = 0
					if col + 1 < len(matrix[0]):
						matrix[row][col] += temp[row][col + 1]
						matrix[row][col + 1] = 0
					if row + 1 < len(matrix):
						matrix[row][col] += temp[row + 1][col]
						matrix[row + 1][col] = 0
					if col - 1 >= 0:
						matrix[row][col] += temp[row][col - 1]
						matrix[row][col - 1] = 0


class Solution:

	def MakeZeros(self, matrix):
		n = len(matrix)
		m = len(matrix[0])
		out = [m * [False] for i in range(n)]
		for r in range(n):
			for c in range(m):
				if matrix[r][c] == 0:
					if r - 1 < 0:
						top = 0
					else:
						top = matrix[r - 1][c]
						out[r - 1][c] = -1
					if r + 1 >= n:
						bot = 0
					else:
						bot = matrix[r + 1][c]
						out[r + 1][c] = -1
					if c - 1 < 0:
						left = 0
					else:
						left = matrix[r][c - 1]
						out[r][c - 1] = -1
					if c + 1 >= m:
						right = 0
					else:
						right = matrix[r][c + 1]
						out[r][c + 1] = -1
					out[r][c] = top + bot + left + right
		for i in range(n):
			for j in range(m):
				if out[i][j] == -1:
					matrix[i][j] = 0
				if out[i][j] != 0 and out[i][j] != -1:
					matrix[i][j] = out[i][j]
		return matrix


import copy

class Solution:

	def MakeZeros(self, matrix):
		c = len(matrix[0])
		r = len(matrix)
		temp = copy.deepcopy(matrix)
		for i in range(0, r):
			for j in range(0, c):
				if temp[i][j] == 0:
					if i + 1 < n:
						matrix[i][j] += temp[i + 1][j]
						matrix[i + 1][j] = 0
					if i - 1 >= 0:
						matrix[i][j] += temp[i - 1][j]
						matrix[i - 1][j] = 0
					if j + 1 < m:
						matrix[i][j] += temp[i][j + 1]
						matrix[i][j + 1] = 0
					if j - 1 >= 0:
						matrix[i][j] += temp[i][j - 1]
						matrix[i][j - 1] = 0


class Solution:

	def MakeZeros(self, matrix):
		ans = [[0 for j in range(len(matrix[0]))] for i in range(len(matrix))]
		for i in range(len(matrix)):
			for j in range(len(matrix[0])):
				ans[i][j] = matrix[i][j]
		for i in range(len(matrix)):
			for j in range(len(matrix[0])):
				if not matrix[i][j]:
					if i > 0:
						ans[i][j] += matrix[i - 1][j]
						ans[i - 1][j] = 0
					if i < len(matrix) - 1:
						ans[i][j] += matrix[i + 1][j]
						ans[i + 1][j] = 0
					if j > 0:
						ans[i][j] += matrix[i][j - 1]
						ans[i][j - 1] = 0
					if j < len(matrix[0]) - 1:
						ans[i][j] += matrix[i][j + 1]
						ans[i][j + 1] = 0
		for i in range(len(matrix)):
			for j in range(len(matrix[0])):
				matrix[i][j] = ans[i][j]


class Solution:

	def MakeZeros(self, matrix):
		n = len(matrix)
		m = len(matrix[0])
		zeros = []
		for i in range(n):
			for j in range(m):
				if matrix[i][j] == 0:
					new_value = 0
					if i > 0:
						new_value += matrix[i - 1][j]
					if i < n - 1:
						new_value += matrix[i + 1][j]
					if j > 0:
						new_value += matrix[i][j - 1]
					if j < m - 1:
						new_value += matrix[i][j + 1]
					zeros.append((i, j, new_value))
		for (i, j, new_value) in zeros:
			matrix[i][j] = new_value
			if i > 0:
				matrix[i - 1][j] = 0
			if i < n - 1:
				matrix[i + 1][j] = 0
			if j > 0:
				matrix[i][j - 1] = 0
			if j < m - 1:
				matrix[i][j + 1] = 0
		return matrix


class Solution:

	def MakeZeros(self, matrix):

		def isvalid(i, j, n, m, matrix):
			if i < 0 or j < 0 or i >= n or (j >= m):
				return 0
			return 1
		n = len(matrix)
		m = len(matrix[0])
		l = []
		for row in range(n):
			for col in range(m):
				if matrix[row][col] == 0:
					summ = 0
					for (x, y) in [[1, 0], [0, 1], [-1, 0], [0, -1]]:
						if isvalid(row + x, col + y, n, m, matrix):
							summ += matrix[row + x][col + y]
							l.append([row + x, col + y, 0])
					l.append([row, col, summ])
		for (x, y, z) in l:
			matrix[x][y] = z


class Solution:

	def aux(self, matrix, cord, row, col, val_map):
		r = cord[0]
		c = cord[1]
		dir_cords = [(r + 1, c), (r - 1, c), (r, c + 1), (r, c - 1)]
		for i in dir_cords:
			if i[0] >= 0 and i[0] < row and (i[1] >= 0) and (i[1] < col):
				key = '{},{}'.format(i[0], i[1])
				matrix[r][c] += val_map[key]
				matrix[i[0]][i[1]] = 0

	def MakeZeros(self, matrix):
		row = len(matrix)
		col = len(matrix[0])
		zero_cords = []
		val_map = {}
		for r in range(row):
			for c in range(col):
				key = '{},{}'.format(r, c)
				val_map[key] = matrix[r][c]
				if matrix[r][c] == 0:
					zero_cords.append((r, c))
		if zero_cords == []:
			return
		for cord in zero_cords:
			self.aux(matrix, cord, row, col, val_map)
		return


class Solution:

	def MakeZeros(self, matrix):
		l = [[matrix[i][j] for j in range(len(matrix[0]))] for i in range(len(matrix))]
		for i in range(len(l)):
			for j in range(len(l[0])):
				if l[i][j] == 0:
					if i - 1 >= 0:
						matrix[i][j] += l[i - 1][j]
						matrix[i - 1][j] = 0
					if i + 1 < len(l):
						matrix[i][j] += l[i + 1][j]
						matrix[i + 1][j] = 0
					if j - 1 >= 0:
						matrix[i][j] += l[i][j - 1]
						matrix[i][j - 1] = 0
					if j + 1 < len(l[0]):
						matrix[i][j] += l[i][j + 1]
						matrix[i][j + 1] = 0


import copy

class Solution:

	def MakeZeros(self, matrix):
		temp = copy.deepcopy(matrix)
		for i in range(len(matrix)):
			for j in range(len(matrix[0])):
				if temp[i][j] == 0:
					if i - 1 >= 0:
						matrix[i][j] += temp[i - 1][j]
						matrix[i - 1][j] = 0
					if j - 1 >= 0:
						matrix[i][j] += temp[i][j - 1]
						matrix[i][j - 1] = 0
					if i + 1 < len(matrix):
						matrix[i][j] += temp[i + 1][j]
						matrix[i + 1][j] = 0
					if j + 1 < len(matrix[0]):
						matrix[i][j] += temp[i][j + 1]
						matrix[i][j + 1] = 0


class Solution:

	def MakeZeros(self, matrix):
		row = len(matrix)
		col = len(matrix[0])
		l = []
		for i in range(row):
			for j in range(col):
				s = 0
				if matrix[i][j] == 0:
					if i - 1 >= 0:
						s += matrix[i - 1][j]
						l.append([i - 1, j, 0])
					if i + 1 < row:
						s += matrix[i + 1][j]
						l.append([i + 1, j, 0])
					if j + 1 < col:
						s += matrix[i][j + 1]
						l.append([i, j + 1, 0])
					if j - 1 >= 0:
						s += matrix[i][j - 1]
						l.append([i, j - 1, 0])
					l.append([i, j, s])
		for i in l:
			matrix[i[0]][i[1]] = i[2]


class Solution:

	def MakeZeros(self, matrix):
		temp = []
		for rows in range(len(matrix)):
			a = []
			for cols in range(len(matrix[0])):
				a.append(matrix[rows][cols])
			temp.append(a)
		for rows in range(len(temp)):
			for cols in range(len(temp[0])):
				if temp[rows][cols] == 0:
					if rows - 1 >= 0:
						matrix[rows][cols] += temp[rows - 1][cols]
						matrix[rows - 1][cols] = 0
					if rows + 1 < len(matrix):
						matrix[rows][cols] += temp[rows + 1][cols]
						matrix[rows + 1][cols] = 0
					if cols - 1 >= 0:
						matrix[rows][cols] += temp[rows][cols - 1]
						matrix[rows][cols - 1] = 0
					if cols + 1 < len(matrix[0]):
						matrix[rows][cols] += temp[rows][cols + 1]
						matrix[rows][cols + 1] = 0


class Solution:

	def MakeZeros(self, matrix1):
		n = len(matrix1)
		m = len(matrix1[0])
		matrix = [[0 for i in range(m)] for j in range(n)]
		for i in range(len(matrix1)):
			for j in range(len(matrix1[0])):
				matrix[i][j] = matrix1[i][j]
		for i in range(len(matrix)):
			for j in range(len(matrix[0])):
				if matrix1[i][j] == 0:
					t = 0
					if i - 1 >= 0:
						t += matrix1[i - 1][j]
						matrix[i - 1][j] = 0
					if j - 1 >= 0:
						t += matrix1[i][j - 1]
						matrix[i][j - 1] = 0
					if i + 1 < n:
						t += matrix1[i + 1][j]
						matrix[i + 1][j] = 0
					if j + 1 < m:
						t += matrix1[i][j + 1]
						matrix[i][j + 1] = 0
					matrix[i][j] = t
		for i in range(len(matrix1)):
			for j in range(len(matrix1[0])):
				matrix1[i][j] = matrix[i][j]
		return matrix1


class Solution:

	def MakeZeros(self, mat):
		row = len(mat)
		col = len(mat[0])
		temp = [[0 for j in range(col)] for i in range(row)]
		for i in range(row):
			for j in range(col):
				temp[i][j] = mat[i][j]
		for i in range(row):
			for j in range(col):
				if temp[i][j] == 0:
					val = 0
					if i - 1 >= 0:
						val += temp[i - 1][j]
						mat[i - 1][j] = 0
					if j - 1 >= 0:
						val += temp[i][j - 1]
						mat[i][j - 1] = 0
					if i + 1 < row:
						val += temp[i + 1][j]
						mat[i + 1][j] = 0
					if j + 1 < col:
						val += temp[i][j + 1]
						mat[i][j + 1] = 0
					mat[i][j] = val


class Solution:

	def MakeZeros(self, matrix):
		lst = []
		m = len(matrix)
		n = len(matrix[0])
		for i in range(m):
			k = []
			for j in range(n):
				k.append(matrix[i][j])
			lst.append(k)
		for row in range(m):
			for col in range(n):
				ans = 0
				if lst[row][col] == 0:
					if row + 1 == m:
						ans += lst[row - 1][col]
						matrix[row - 1][col] = 0
					elif row == 0:
						ans += lst[row + 1][col]
						matrix[row + 1][col] = 0
					else:
						ans += lst[row + 1][col] + lst[row - 1][col]
						matrix[row - 1][col] = 0
						matrix[row + 1][col] = 0
					if col + 1 == n:
						ans += lst[row][col - 1]
						matrix[row][col - 1] = 0
					elif col == 0:
						ans += lst[row][col + 1]
						matrix[row][col + 1] = 0
					else:
						ans += lst[row][col + 1] + lst[row][col - 1]
						matrix[row][col - 1] = 0
						matrix[row][col + 1] = 0
					matrix[row][col] = ans


class Solution:

	def MakeZeros(self, arr):
		temp = [[0 for i in range(len(arr[0]))] for j in range(len(arr))]
		for i in range(len(arr)):
			for j in range(len(arr[0])):
				temp[i][j] = arr[i][j]
		for i in range(len(arr)):
			for j in range(len(arr[0])):
				if temp[i][j] == 0:
					if i + 1 < len(arr):
						arr[i][j] += temp[i + 1][j]
						arr[i + 1][j] = 0
					if j + 1 < len(arr[0]):
						arr[i][j] += temp[i][j + 1]
						arr[i][j + 1] = 0
					if i > 0:
						arr[i][j] += temp[i - 1][j]
						arr[i - 1][j] = 0
					if j > 0:
						arr[i][j] += temp[i][j - 1]
						arr[i][j - 1] = 0


class Solution:

	def MakeZeros(self, matrix):
		n = len(matrix)
		m = len(matrix[0])
		res = [[0] * m for i in range(n)]
		del_col = [0, 1, 0, -1]
		del_row = [-1, 0, 1, 0]
		for i in range(n):
			for j in range(m):
				if matrix[i][j] == 0:
					sum = 0
					for k in range(0, 4):
						r = del_row[k] + i
						c = del_col[k] + j
						if r >= 0 and r < n and (c >= 0) and (c < m):
							sum += abs(matrix[r][c])
							if matrix[r][c] > 0:
								matrix[r][c] *= -1
							res[r][c] = 0
					res[i][j] = sum
				elif matrix[i][j] > 0:
					res[i][j] = matrix[i][j]
		for i in range(n):
			for j in range(m):
				matrix[i][j] = res[i][j]


class Solution:

	def MakeZeros(self, matrix):
		mat = []
		for i in matrix:
			l = []
			for j in i:
				l.append(j)
			mat.append(l)
		l1 = len(mat[0])
		l2 = len(mat)
		for i in range(l2):
			for j in range(l1):
				if mat[i][j] == 0:
					r1 = 0
					if i - 1 > -1:
						r1 = mat[i - 1][j]
						matrix[i - 1][j] = 0
					r2 = 0
					if i + 1 < l2:
						r2 = mat[i + 1][j]
						matrix[i + 1][j] = 0
					r3 = 0
					if j - 1 > -1:
						r3 = mat[i][j - 1]
						matrix[i][j - 1] = 0
					r4 = 0
					if j + 1 < l1:
						r4 = mat[i][j + 1]
						matrix[i][j + 1] = 0
					matrix[i][j] = r1 + r2 + r3 + r4


class Solution:

	def MakeZeros(self, matrix):
		r = len(matrix)
		c = len(matrix[0])
		ans = [[0 for i in range(c)] for j in range(r)]
		for i in range(r):
			for j in range(c):
				ans[i][j] = matrix[i][j]
		for i in range(r):
			for j in range(c):
				if matrix[i][j] == 0:
					summ = 0
					if i - 1 >= 0:
						summ += matrix[i - 1][j]
						ans[i - 1][j] = 0
					if i + 1 < r:
						summ += matrix[i + 1][j]
						ans[i + 1][j] = 0
					if j - 1 >= 0:
						summ += matrix[i][j - 1]
						ans[i][j - 1] = 0
					if j + 1 < c:
						summ += matrix[i][j + 1]
						ans[i][j + 1] = 0
					ans[i][j] = summ
		for i in range(r):
			matrix[i] = ans[i]


class Solution:

	def MakeZeros(self, matrix):
		r = len(matrix)
		c = len(matrix[0])
		matrix1 = [[matrix[i][j] for j in range(c)] for i in range(r)]
		for i in range(r):
			for j in range(c):
				if matrix[i][j] == 0:
					sum = 0
					for i1 in range(-1, 2):
						ind = i1 + i
						if ind >= 0 and ind < r:
							sum += matrix[ind][j]
							matrix1[ind][j] = 0
					for j1 in range(-1, 2):
						ind = j1 + j
						if ind >= 0 and ind < c:
							sum += matrix[i][ind]
							matrix1[i][ind] = 0
					matrix1[i][j] = sum
		matrix[:] = matrix1[:]


class Solution:

	def __init__(self):
		self.friends = [(-1, 0), (1, 0), (0, -1), (0, 1)]

	def MakeZeros(self, matrix):
		self.zeros = []
		self.matrix = matrix
		self.n = len(matrix)
		self.m = len(matrix[0])
		for i in range(self.n):
			for j in range(self.m):
				if matrix[i][j] == 0:
					self.handle_friends(i, j)
		for i in self.zeros:
			self.handle_friends1(i)

	def handle_friends(self, i, j):
		temp = 0
		for k in self.friends:
			if self.is_valid(i, j, k):
				x = i + k[0]
				y = j + k[1]
				temp += self.matrix[x][y]
		self.zeros.append((i, j, temp))

	def handle_friends1(self, m):
		i = m[0]
		j = m[1]
		temp = m[2]
		for k in self.friends:
			if self.is_valid(i, j, k):
				x = i + k[0]
				y = j + k[1]
				self.matrix[x][y] = 0
		self.matrix[i][j] = temp

	def is_valid(self, i, j, k):
		x = i + k[0]
		y = j + k[1]
		return x >= 0 and x < self.n and (y >= 0) and (y < self.m)


def get_neighbour_cordinate(matrix, row, col):
	n = len(matrix)
	m = len(matrix[0])
	neighbour_path = [[-1, 0], [0, -1], [1, 0], [0, 1]]
	neighbour_cordinate = []
	for cordinate in neighbour_path:
		if row + cordinate[0] < 0 or row + cordinate[0] >= n:
			continue
		if col + cordinate[1] < 0 or col + cordinate[1] >= m:
			continue
		neighbour_cordinate.append([row + cordinate[0], col + cordinate[1]])
	return neighbour_cordinate

def get_neighbouring_sum(matrix, row, col):
	n_cor = get_neighbour_cordinate(matrix, row, col)
	sm = 0
	for cor in n_cor:
		sm += matrix[cor[0]][cor[1]]
	return sm

def make_neighbour_zero(metrix, row, col):
	n_cor = get_neighbour_cordinate(matrix, row, col)
	for cor in n_cor:
		matrix[cor[0]][cor[1]] = 0

class Solution:

	def MakeZeros(self, matrix):
		track = []
		for row in range(len(matrix)):
			for col in range(len(matrix[0])):
				if matrix[row][col] == 0:
					sm = get_neighbouring_sum(matrix, row, col)
					track.append([row, col, sm])
		for row in track:
			matrix[row[0]][row[1]] = row[2]
			make_neighbour_zero(matrix, row[0], row[1])


class Solution:

	def MakeZeros(self, matrix):
		rows = len(matrix)
		cols = len(matrix[0])
		dummyMat = [row[:] for row in matrix]
		for i in range(rows):
			for j in range(cols):
				if dummyMat[i][j] == 0:
					sum = 0
					if i - 1 >= 0:
						sum += dummyMat[i - 1][j]
						matrix[i - 1][j] = 0
					if j + 1 < cols:
						sum += dummyMat[i][j + 1]
						matrix[i][j + 1] = 0
					if i + 1 < n:
						sum += dummyMat[i + 1][j]
						matrix[i + 1][j] = 0
					if j - 1 >= 0:
						sum += dummyMat[i][j - 1]
						matrix[i][j - 1] = 0
					matrix[i][j] = sum


class Solution:

	def MakeZeros(self, matrix):
		x = [0, -1, 1, 0]
		y = [-1, 0, 0, 1]
		n = len(matrix)
		m = len(matrix[0])
		dummy = [[matrix[j][i] for i in range(m)] for j in range(n)]
		for i in range(n):
			for j in range(m):
				if dummy[i][j] == 0:
					s = 0
					for k in range(4):
						if -1 < i + x[k] < n and -1 < j + y[k] < m:
							s += dummy[i + x[k]][j + y[k]]
							matrix[i + x[k]][j + y[k]] = 0
					matrix[i][j] = s


class Solution:

	def MakeZeros(self, matrix):
		n = len(matrix)
		m = len(matrix[0])
		res = [row[:] for row in matrix]
		l = 0
		r = 0
		u = 0
		lo = 0
		count = 0
		for i in range(n):
			for j in range(m):
				l = 0
				r = 0
				u = 0
				lo = 0
				if matrix[i][j] == 0:
					if j - 1 >= 0:
						l = matrix[i][j - 1]
						res[i][j - 1] = 0
					if j + 1 < m and matrix[i][j + 1]:
						r = matrix[i][j + 1]
						res[i][j + 1] = 0
					if i - 1 >= 0:
						u = matrix[i - 1][j]
						res[i - 1][j] = 0
					if i + 1 < n and matrix[i + 1][j]:
						lo = matrix[i + 1][j]
						res[i + 1][j] = 0
					res[i][j] = l + r + u + lo
		for i in range(n):
			for j in range(m):
				matrix[i][j] = res[i][j]


class Solution:

	def MakeZeros(self, matrix):
		n = len(matrix)
		m = len(matrix[0])
		to_be_zero = []
		import copy
		matrix_copy = copy.deepcopy(matrix)
		for i in range(n):
			for j in range(m):
				if matrix_copy[i][j] == 0:
					tot = 0
					if i > 0:
						tot += matrix_copy[i - 1][j]
						matrix[i - 1][j] = 0
					if j > 0:
						tot += matrix_copy[i][j - 1]
						matrix[i][j - 1] = 0
					if i < n - 1:
						tot += matrix_copy[i + 1][j]
						matrix[i + 1][j] = 0
					if j < m - 1:
						tot += matrix_copy[i][j + 1]
						matrix[i][j + 1] = 0
					matrix[i][j] = tot


class Solution:

	def MakeZeros(self, matrix):
		irow = []
		icol = []
		summ = 0
		ans = []
		ans = [[matrix[i][j] for j in range(len(matrix[0]))] for i in range(len(matrix))]
		m = len(matrix)
		n = len(matrix[0])
		for i in range(0, m):
			for j in range(0, n):
				if matrix[i][j] == 0:
					irow.append(j)
					icol.append(i)
		x = 0
		while len(irow) != 0 and len(icol) != 0:
			summ = 0
			if icol[x] > 0:
				summ = summ + matrix[icol[x] - 1][irow[x]]
				ans[icol[x] - 1][irow[x]] = 0
			if icol[x] < m - 1:
				summ = summ + matrix[icol[x] + 1][irow[x]]
				ans[icol[x] + 1][irow[x]] = 0
			if irow[x] > 0:
				summ = summ + matrix[icol[x]][irow[x] - 1]
				ans[icol[x]][irow[x] - 1] = 0
			if irow[x] < n - 1:
				summ = summ + matrix[icol[x]][irow[x] + 1]
				ans[icol[x]][irow[x] + 1] = 0
			ans[icol[x]][irow[x]] = summ
			irow.pop(0)
			icol.pop(0)
		for i in range(0, m):
			for j in range(0, n):
				matrix[i][j] = ans[i][j]


class Solution:

	def MakeZeros(self, matrix):
		a = []
		b = []
		for i in range(len(matrix)):
			for j in range(len(matrix[0])):
				c = 0
				if matrix[i][j] == 0:
					if i - 1 >= 0:
						c += matrix[i - 1][j]
					if j - 1 >= 0:
						c += matrix[i][j - 1]
					if i + 1 <= len(matrix) - 1:
						c += matrix[i + 1][j]
					if j + 1 <= len(matrix[0]) - 1:
						c += matrix[i][j + 1]
					a.append([i, j])
					b.append(c)
		for i in range(len(a)):
			if a[i][0] - 1 >= 0:
				matrix[a[i][0] - 1][a[i][1]] = 0
			if a[i][1] - 1 >= 0:
				matrix[a[i][0]][a[i][1] - 1] = 0
			if a[i][0] + 1 <= len(matrix) - 1:
				matrix[a[i][0] + 1][a[i][1]] = 0
			if a[i][1] + 1 <= len(matrix[0]) - 1:
				matrix[a[i][0]][a[i][1] + 1] = 0
			matrix[a[i][0]][a[i][1]] = b[i]
		return matrix


class Solution:

	def MakeZeros(self, matrix):
		n = len(matrix)
		m = len(matrix[0])
		new = [[-1 for i in range(m)] for j in range(n)]
		for i in range(n):
			for j in range(m):
				new[i][j] = matrix[i][j]
		ri = [-1, 0, 1, 0]
		cj = [0, 1, 0, -1]
		for i in range(n):
			for j in range(m):
				if new[i][j] == 0:
					sum = 0
					for k in range(4):
						row = i + ri[k]
						col = j + cj[k]
						if (row >= 0 and row < n) and (col >= 0 and col < m):
							sum += new[row][col]
							matrix[row][col] = 0
					matrix[i][j] = sum
		return


class Solution:

	def MakeZeros(self, arr):
		temp = [[arr[i][j] for j in range(len(arr[0]))] for i in range(len(arr))]
		n = len(arr)
		m = len(arr[0])
		di = [-1, 0, 1, 0]
		dj = [0, 1, 0, -1]
		for i in range(n):
			for j in range(m):
				if temp[i][j] == 0:
					sum = 0
					for ind in range(4):
						row = i + di[ind]
						col = j + dj[ind]
						if row >= 0 and row < n and (col < m) and (col >= 0):
							arr[row][col] = 0
							sum += temp[row][col]
					arr[i][j] = sum
		return arr


class Solution:

	def MakeZeros(self, mat):
		(rows, cols) = (len(mat), len(mat[0]))
		arr = []
		for r in range(rows):
			for c in range(cols):
				if mat[r][c] == 0:
					val = 0
					if r + 1 < rows:
						val += mat[r + 1][c]
					if r - 1 >= 0:
						val += mat[r - 1][c]
					if c - 1 >= 0:
						val += mat[r][c - 1]
					if c + 1 < cols:
						val += mat[r][c + 1]
					arr.append([r, c, val])
		for i in arr:
			mat[i[0]][i[1]] = i[2]
			if i[1] + 1 < cols:
				mat[i[0]][i[1] + 1] = 0
			if i[1] - 1 >= 0:
				mat[i[0]][i[1] - 1] = 0
			if i[0] + 1 < rows:
				mat[i[0] + 1][i[1]] = 0
			if i[0] - 1 >= 0:
				mat[i[0] - 1][i[1]] = 0
		return mat


class Solution:

	def MakeZeros(self, matrix):
		R = len(matrix)
		C = len(matrix[0])
		temp = [[0 for i in range(C)] for i in range(R)]
		for i in range(R):
			for j in range(C):
				temp[i][j] = matrix[i][j]
		for i in range(R):
			for j in range(C):
				if matrix[i][j] == 0 and temp[i][j] == 0:
					if i > 0:
						matrix[i][j] += temp[i - 1][j]
						matrix[i - 1][j] = 0
					if j > 0:
						matrix[i][j] += temp[i][j - 1]
						matrix[i][j - 1] = 0
					if i < R - 1:
						matrix[i][j] += temp[i + 1][j]
						matrix[i + 1][j] = 0
					if j < C - 1:
						matrix[i][j] += temp[i][j + 1]
						matrix[i][j + 1] = 0


import copy

class Solution:

	def MakeZeros(self, grid):
		m = len(grid)
		n = len(grid[0])
		cpy = copy.deepcopy(grid)
		for i in range(m):
			for j in range(n):
				if grid[i][j] == 0:
					temp = 0
					if i - 1 >= 0:
						temp += grid[i - 1][j]
						cpy[i - 1][j] = 0
					if j - 1 >= 0:
						temp += grid[i][j - 1]
						cpy[i][j - 1] = 0
					if i + 1 < m:
						temp += grid[i + 1][j]
						cpy[i + 1][j] = 0
					if j + 1 < n:
						temp += grid[i][j + 1]
						cpy[i][j + 1] = 0
					cpy[i][j] = temp
		for i in range(m):
			for j in range(n):
				grid[i][j] = cpy[i][j]


import copy

class Solution:

	def MakeZeros(self, arr):
		arrCpy = copy.deepcopy(arr)
		mpR = {}
		mpC = {}
		for i in range(0, len(arr)):
			for j in range(0, len(arr[i])):
				if arrCpy[i][j] == 0:
					sum = 0
					if i - 1 > -1:
						sum = sum + arrCpy[i - 1][j]
						arr[i - 1][j] = 0
					if i + 1 < len(arr):
						sum = sum + arrCpy[i + 1][j]
						arr[i + 1][j] = 0
					if j - 1 > -1:
						sum = sum + arrCpy[i][j - 1]
						arr[i][j - 1] = 0
					if j + 1 < len(arr[0]):
						sum = sum + arrCpy[i][j + 1]
						arr[i][j + 1] = 0
					arr[i][j] = sum


class Solution:

	def MakeZeros(self, matrix):
		(n, m) = (len(matrix), len(matrix[0]))
		res = [[matrix[i][j] for j in range(m)] for i in range(n)]
		for i in range(n):
			for j in range(m):
				if matrix[i][j] == 0:
					ans = 0
					if i - 1 >= 0:
						ans += matrix[i - 1][j]
						res[i - 1][j] = 0
					if i + 1 < n:
						ans += matrix[i + 1][j]
						res[i + 1][j] = 0
					if j - 1 >= 0:
						ans += matrix[i][j - 1]
						res[i][j - 1] = 0
					if j + 1 < m:
						ans += matrix[i][j + 1]
						res[i][j + 1] = 0
					res[i][j] = ans
		for i in range(n):
			for j in range(m):
				matrix[i][j] = res[i][j]