I propose that
1. If $\min(N,M) \geq 3$ then $r=4$
2. If $\min(N,M) = 2$ and $\max(N,M) > 2$ then $r=3$
3. If $M=N=2$ then $r=2$
4. If $\min(N,M) = 1$ and $\max(N,M) > 2$ then $r=2$
5. If $\min(N,M) = 1$ and $\max(N,M) \leq 2$ then $r=1$
Proof
1.
If $\min(N,M) \geq 3$ then there is at least one cell surrounded by $4$ others and all have different values. Hence, $r \geq 4$.
Now suppose we colour the cells of the grid like a chessboard and do the following:
(i) In every odd-numbered column number the white squares $1$ and $2$ alternately, always beginning each column with the opposite number of the previous, as shown
(ii) Proceed analogously for the white squares in the even-numbered columns and the numbers $3$ and $4$, as shown
(iii) Now do exactly the same for the black squares and we are done
Hence we can always achieve $r \leq 4$ and in this scenario we have $r=4$
2.
If $\min(N,M) = 2$ but $\max(N,M) >2$, then we know there is at least one cell with $3$ neighbours. Hence, $r \geq 3$.
Without loss of generality suppose $N=2$ (i.e, $2$ rows but more than $2$ columns) then we can fill each row with the repeating sequence $1,2,3,1,2,3,\ldots$. That is, we have $$\left(\begin{array}{l}1&2&3&1&2&3&\ldots\\1&2&3&1&2&3&\ldots\end{array}\right) $$ That is, we have $r \leq 3$.
Hence, $r=3$ in this case.
3.
If $N=M=2$ then we know that $r \geq 2$ since each cell has $2$ neighbours.
To achieve $r=2$, we can put $1$s in the first column and $2$s in the second as shown $$\left(\begin{array}{l}1&2\\1&2\end{array}\right) $$
4.
If $\min(N,M) =1$ and $\max(N,M) > 2$ then there is at least one cell with $2$ neighbours so $r \geq 2$.
To achieve $r=2$, we can fill the single row (or column) with a repeating sequence $$ \left( 1,1,2,2,1,1,2,2,\ldots \right) $$
5.
There are just two cases here, really, and, in both, we can fill the grid with $1$s that is, $$ (1) \,\,\,\,\text{ and } \,\,\,\,(1 1) $$