Not including the point where the diagonal enters the grid and the point where it exits, it must cross the $m-1$ lines that divide the columns of $m$ rectangles in one dimension and the $n-1$ lines that divide the rows of $n$ rectangles in the other dimension.
That would be $m + n - 2$ crossings altogether, but depending on the dimensions of the grid, the diagonal may cross through the corner of one rectangle into another, thereby crossing one of the $m-1$ lines and one of the $n-1$ lines at one time. Usually we consider this just one "time" the rectangle crosses a rectangle side--but perhaps you don't consider it crossing a side at all, but rather crossing a corner.
The number of corner crossings is $\gcd(m,n) - 1$, where $\gcd(m,n)$ is the greatest common divisor of $m$ and $n.$