@Michael1993, mathematically your approach is correct. It probably timed-out or had memory overflow in larger scale.
Print out all numbers you got in your small-scale array, e.g. up to 10x10, or 20x20 as you like. Line up the numbers tidily as a square. Try to spot the pattern with the numbers. The pattern is something taught in school and repeatedly shows up in many puzzles.
I can just tell you that much without spoiling the puzzle.