I just do what Graal does offline in the tile selection window thing. Take this matrix, for example...
NPC Code:
0 1 2 3 4
0 0 1 2 3 4
1 5 6 7 8 9
2 10 11 12 13 14
3 15 16 17 18 19
Those can be all the indexes, and you can treat them exactly like x,y coordinates. Just do row * 5 + column to get a specific index (start counting at 0).