Think of it this way: You have a 5x5 array, and you need to fill it with values 0, 1, or -1, with three restrictions:
- The cells sharing an edge with a cell with a $\pm 1$ cannot contain a $\mp 1$.
- If a cell contains a 1, the adjacent cells to the top-left and bottom-right cannot also contain a 1.
- If a cell contains a -1, the adjacent cells to the bottom-left and top-right cannot also contain a -1.
Here, cells containing a "1" have a diagonal running top-left to bottom-right, and cells containing a "-1" have a diagonal running the other way. Cells containing 0 do not have a diagonal at all.
So, for the example grid you provide, you start with the grid:
I don't think there's "a mathematical solution" to it - that is, I doubt that one could immediately find the "right answer" without some level of trial and error. But it does give a good starting point to automating the search, if you want to try solving it computationally.