Editorial for COCI '17 Contest 2 #1 Košnja
Submitting an official solution before solving the problem yourself is a bannable offence.
Let and denote the number of rows and columns of the matrix that represent the lawn, respectively.
Let's assume that . One of the optimal ways for Mirko to visit each field while making the minimal number of turns is that he starts from the upper left field facing right, visits the entire first row, goes to the second row, turns right, visits the entire second row, goes to the third row, and so on. The described way takes exactly turns.
In the case when , he needs to visit the fields in the same way, but now he needs to visit the columns in order. This takes exactly turns. Therefore, the solution to the problem is .
The proof that the described way is indeed the optimal way is left as an exercise to the reader