### Solving an optimization problem by Sage

I would like to know how one can solve the following optimization problem using Sage.

I would like to have a matrix where every element is an integer from 0 to 9. One can read numbers from the matrix by choosing the starting element and direction from one of the eight direction (horizontal, vertical, diagonal) and go to that direction 0 to 5 steps and concatenate the numbers.

For example, one can read the squares of 1 to 10 from the following matrix:

```
0 0 1 8 2
3 6 4 9 5
```

Now the problem is to find an $n\times m$ matrix with digits from 0 to 9 where one can read the squares of 1 to 100 in a way described above where $nm$ is the smallest possible. How can I do that on Sage? Can Sage do it for example in the case 12x10 griid? Does for example simulated annealing or genetic algorithm work in here, or is it easy to implement the algorithm in https://stackoverflow.com/questions/943113/algorithm-to-generate-a-crossword works here? I would like to see how good result we can achieve.