Help me in solving MATSETZERO problem

My issue

getting runtime error when while submitting the code

My code

# cook your dish here
def dozero(a,b,n,m,m2):
    for i in range(n):
        m2[a][i]=0
    for j in range(m-1):
        m2[j][b]=0


n,m=map(int,input().split())
m1=[]
for _ in range(n):
    lst=list(map(int,input().strip().split()))
    m1.append(lst)

m2=[]
for i in range(n):
    lst=[]
    for j in range(m):
        lst.append(1)
    m2.append(lst)

for i in range(n):
    for j in range(m):

        if m1[i][j]==0:
            dozero(i,j,n,m,m2)
            
for i in range(n):
    for j in range(m):
        if m2[i][j]!=0:
            m2[i][j]=m1[i][j]
for i in range(n):
    print(" ".join(map(str,m2[i])))



        
    

Learning course: Matrices
Problem Link: Set Matrix Zeroes Practice Problem in Matrices - CodeChef

Runtime Error means something failed during the execution of your problem: something wasn’t defined, index out of bounds, etc.

And you can spot it by using this example:

3 4
4 6 0 1
8 2 1 1
3 1 5 2

Your problem is your function “dozero” You are running the rows with the “n” number of rows, and the columns with the “m” number of columns. And that’s not ok.

You will only make 0 the numbers of the row, do you need the amount of rows? No, you need the amount of columns in the row (because a row is made of columns)

The same goes for the column loop. How many numbers are in that specific column? The number of rows ofc.

So, just put it backwards

# cook your dish here
def dozero(a,b,n,m,m2):
    for i in range(m):      # You have m and n backwards
        m2[a][i]=0
    for j in range(n):
        m2[j][b]=0

'''
---------------------------------
Beginning of the code 
---------------------------------
'''
n,m=map(int,input().split())
m1=[]
for _ in range(n):
    lst=list(map(int,input().strip().split()))
    m1.append(lst)
'''
---------------------------------
A second matrix to work on
---------------------------------
'''
m2=[]
for i in range(n):
    lst=[]
    for j in range(m):
        lst.append(1)
    m2.append(lst)
'''
---------------------------------
You are looking for the zeros
---------------------------------
'''

for i in range(n):
    for j in range(m):
        if m1[i][j]==0:
            dozero(i,j,n,m,m2)
'''
---------------------------------
Write the numbers other than zeros
---------------------------------
'''
for i in range(n):
    for j in range(m):
        if m2[i][j]!=0:
            m2[i][j]=m1[i][j]
            
for i in range(n):
    print(*m2[i])