First, it is clear that all functions of the form f(n)=n+c with a constant nonnegative integer c satisfy the problem conditions since (f(m)+n)(f(n)+m)=(n+m+c)2 is a square.
We are left to prove that there are no other functions. We start with the following Lemma. Suppose that p∣f(k)−f(ℓ) for some prime p and positive integers k,ℓ. Then p∣k−ℓ.
Proof. Suppose first that p2∣f(k)−f(ℓ), so f(ℓ)=f(k)+p2a for some integer a. Take some positive integer D>max{f(k),f(ℓ)} which is not divisible by p and set n=pD−f(k). Then the positive numbers n+f(k)=pD and n+f(ℓ)=pD+(f(ℓ)−f(k))=p(D+pa) are both divisible by p but not by p2. Now, applying the problem conditions, we get that both the numbers (f(k)+n)(f(n)+k) and (f(ℓ)+n)(f(n)+ℓ) are squares divisible by p (and thus by p2 ); this means that the multipliers f(n)+k and f(n)+ℓ are also divisible by p, therefore p∣(f(n)+k)−(f(n)+ℓ)=k−ℓ as well.
On the other hand, if f(k)−f(ℓ) is divisible by p but not by p2, then choose the same number D and set n=p3D−f(k). Then the positive numbers f(k)+n=p3D and f(ℓ)+n=p3D+(f(ℓ)−f(k)) are respectively divisible by p3 (but not by p4 ) and by p (but not by p2 ). Hence in analogous way we obtain that the numbers f(n)+k and f(n)+ℓ are divisible by p, therefore p∣(f(n)+k)−(f(n)+ℓ)=k−ℓ.
We turn to the problem. First, suppose that f(k)=f(ℓ) for some k,ℓ∈N. Then by Lemma we have that k−ℓ is divisible by every prime number, so k−ℓ=0, or k=ℓ. Therefore, the function f is injective.
Next, consider the numbers f(k) and f(k+1). Since the number (k+1)−k=1 has no prime divisors, by Lemma the same holds for f(k+1)−f(k); thus ∣f(k+1)−f(k)∣=1.
Now, let f(2)−f(1)=q,∣q∣=1. Then we prove by induction that f(n)=f(1)+q(n−1). The base for n=1,2 holds by the definition of q. For the step, if n>1 we have f(n+1)=f(n)±q=f(1)+q(n−1)±q. Since f(n)=f(n−2)=f(1)+q(n−2), we get f(n)=f(1)+qn, as desired.
Finally, we have f(n)=f(1)+q(n−1). Then q cannot be -1 since otherwise for n≥f(1)+1 we have f(n)≤0 which is impossible. Hence q=1 and f(n)=(f(1)−1)+n for each n∈N, and f(1)−1≥0, as desired.