Since you ask for an algorithm idea, and not a precise answer, so here is my 2 cents.
You can transform your problem to get rid of the squares by finding all perfect squares ( e.g. 1, 4, 9, 16, 25...) from 1 to S and put them into a set called PerfectSquareSetLessThanS.
Then, if you can solve the following (more general) problem, you can easily solve your original problem.
General problem:
Given an arbitary set V, find a subset W of V that has exactly K numbers such that they sum to S.
While I don't yet have a precise solution, your general problem looks a lot like a well known problem called the 3SUM problem.
The 3SUM problem asks if given a set of n numbers, are there a set of 3 numbers whose sum is 0. The wikipedia page outlines an algorithm to search for such tupple.
Note that the 3SUM problems have different variants, 1 of which is the k-SUM (substitute 3 by k in the above paragraph) , and another one is the non-zero sum variant. Maybe you could find a combination of these two variants to build your generalized solution.