PROMYS 2022 (Problem 5)

The rectangular floor of a bathroom is covered with square tiles (all of the same size). A spider starts at one corner of the bathroom and walks to the diagonally opposite corner. For example, the figure below shows a 6×86\times8 bathroom, in which the spider touches 1212 tiles on its path. (A spider doesn't touch a tile if it just walks over the grout at the corner of a tile.) For an m×nm\times n bathroom, how many tiles does the spider touch on its walk?
A 6 by 8 tiled rectangle with a diagonal path touching 12 tiles