Per the title, I'm seeking the definition of a function $f(n, m)$ which evaluates to the number of lines made from exactly $n$ points which can be placed on a two-dimensional discrete, square grid of size $m \times m$.


My primary interest is the case where $n = 3$, as this is the answer I'm *really* after. I would've raised this question alone, however since I see that [$n = 2$ has been answered](https://mathoverflow.net/q/2806/92343) already, I figured I might as well just generalize the question in hopes that there's some nice, elegant solution out there.



How many lines of exactly n points can be placed in a discrete, square grid of size m x m?
It seems that this has been answered here by S. Mustonen: [PointsInGrid.pdf](http://www.survo.fi/papers/PointsInGrid.pdf)


There $f(n,m)$ is denoted by $L\_n(m)$ and a formula would be 
$$L\_n(m)=\frac{1}{2}[f(m,n+1)-2f(m,n)+f(m,n-1)]$$
for $$f(m,k)=\sum\_{\substack{-m<kx<m\\-m<ky<m\\(x,y)=1}}(m-|kx|)(m-|ky|)$$


