Calculate min number of moves (no path involved)

I’m looking to implement an algorithm that will calculate the minimum number of moves based on the rules below.

Let’s say that we have a grid (NxM), as exampled in the image below.

enter image description here

Each cell will be the sum based on the row and column and I can subtract 1 unit from one cell and add it to a neighbour. For example, on the image above, the minimum number of moves on Initial will be 2 and on Initial – 2 will be 4.