The problem is present in the below line
grid=[[0]*m]*n
Because it doesn't create the nxm grid of 0s, it copies that object: [[0]*m] n times to create this 2d array. Hence if you mutate one of the values in this object, other n object instances get edited too. Try the below lines:
grid = [[0]*m for _ in range(n)]
If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch