Existence and nonexistence of solutions to a variational problem on a square