Solution:
Let D be the number of grid squares of which exactly one side belongs to P, let E be the number of grid squares of which exactly two sides belong to P, which are moreover adjacent, and finally let F be the number of grid squares of which exactly three sides belong to P. Since m,n⩾4, among the mn grid squares there is none whose entire boundary is contained in P, and consequently there are for the intersection of such a boundary with P only the five possibilities addressed in the definitions of B,C,D,E and F. We thus have
mn=B+C+D+E+F
Now we want to count in two ways the pairs (Q,S) consisting of a grid square Q and a side S of Q belonging to P. On the one hand, for the squares of type B,C,D,E and F there are respectively exactly 2,0,1,2 and 3 possible choices for S, and hence this number amounts to 2B+D+2E+3F. On the other hand, P is composed of (m−1)(n−1) unit segments, all of which can play the role of S, and each of these has two sides on each of which lies one grid square. Altogether we thus obtain
2(m−1)(n−1)=2B+D+2E+3F
At each of the (m−1)(n−1) interior vertices of our rectangle, P either passes straight through, and this happens exactly A times, or P turns. Whenever this happens, the two associated unit segments of P enclose a grid square, and this must be either of type E or of type F. Conversely, to each square of type E belongs one, and to each square of type F belong two, of these turning points of P. Altogether, therefore, we also have
(m−1)(n−1)=A+E+2F
From (2) − (3) + (1) we now obtain
m+n−1=A−B−C
and this in turn implies
A=B−C+m−n−1
With this the problem is solved.