0 21 9999 means that the weight to get from cell with ID 0 to cell with ID 21 is 9999
And a further consequence of this explanation, is that you are potentially allowing travel from
one cell to another which is only diagonally adjacent.
Which means each cell has not four neighbours, nor six neighbours, but eight neighbours.
The size of the datastructure needed to keep this data has been raised as an issue on the Arduino.
There are many ways in which the data could be stored more compactly. For a start, you could
leave out all the items of data which represent unreachable neighbours. If the "cost" isn't listed
in the list, then you can deduce it is unreachable without storing data to say so.
Secondly, if RAM space really is tight, you can dispense with the information about some cells
being slightly more difficult to reach than others. If the cells are squares, then the cost of reaching
a cell adjacent up or down or sideways is x and the cost of reaching a diagonally adjacent square
is 1.4x where the 1.4 is the square root of 2 and follows from elementary geometry.
You can then implement your shortest path from A to B algorithm with minimal data storage