By the way, you can also view this Matrix puzzle as a 2 player game where you can move left any number of squares, or up any number of squares. The winner is the first player to reach the top left.

This game is also isomorphic to a 2 stack game of Nim.

The general approach to computing who wins such games is to compute the Nim-numbers of the game. A nim-number is computed as the smallest non-negative number not already present in any position you can move to.

This operation of finding the smallest non-negative is sometimes called the “mex” function.

The numbers in your matrix are precisely the nim-numbers of this game.

It is well known in game theory that you can compute the Nim number of multiple stacks of Nim by XORing the sizes of the piles. (Although this is the first time I’ve encountered someone who has managed to work out the formula by themselves!)

In summary, this is another way to see why your solution is correct that you might find interesting.

Note also that your formula would work just as well for a 3d matrix (the function becomes x^y^z).

]]>