I agree with what @hllowrld said that modifying the original matrix may not be allowed in production settings. After calling this function, the input no longer stores the values of grid, but the sum of paths, which can cause confusion and is dangerous in reality. It is good to try reducing the memory usage, but not in this way.
The answer upstairs seems to be a cute method for saving the memory. On each of first iteration, the dp(1-dimension list) itself is equal to dp[i-1](2-dimension list) before adding operation and dp[i](2-dimension list) after process, how wonderfu!