Let be the minimum number of perfect squares that sum to . Base case: (you need zero squares to make zero).
For each from to , try all perfect squares . Compute .
This says: to make , take a perfect square and add it to the optimal solution for . The answer is .