Problem:
Score : 200 points

Problem Statement

There are N points in a D-dimensional space.

The coordinates of the i-th point are (X_{i1}, X_{i2}, ..., X_{iD}).

The distance between two points with coordinates (y_1, y_2, ..., y_D) and (z_1, z_2, ..., z_D) is \sqrt{(y_1 - z_1)^2 + (y_2 - z_2)^2 + ... + (y_D - z_D)^2}.

How many pairs (i, j) (i < j) are there such that the distance between the i-th point and the j-th point is an integer?

Constraints

All values in input are integers.

2 \leq N \leq 10

1 \leq D \leq 10

-20 \leq X_{ij} \leq 20

No two given points have the same coordinates. That is, if i \neq j, there exists k such that X_{ik} \neq X_{jk}.

Input

Input is given from Standard Input in the following format:

N D
X_{11} X_{12} ... X_{1D}
X_{21} X_{22} ... X_{2D}
\vdots
X_{N1} X_{N2} ... X_{ND}

Output

Print the number of pairs (i, j) (i < j) such that the distance between the i-th point and the j-th point is an integer.

Sample Input 1

3 2
1 2
5 5
-2 8

Sample Output 1

1

The number of pairs with an integer distance is one, as follows:

The distance between the first point and the second point is \sqrt{|1-5|^2 + |2-5|^2} = 5, which is an integer.

The distance between the second point and the third point is \sqrt{|5-(-2)|^2 + |5-8|^2} = \sqrt{58}, which is not an integer.

The distance between the third point and the first point is \sqrt{|-2-1|^2+|8-2|^2} = 3\sqrt{5}, which is not an integer.

Sample Input 2

3 4
-3 7 8 2
-12 1 10 2
-2 8 9 3

Sample Output 2

2

Sample Input 3

5 1
1
2
3
4
5

Sample Output 3

10

Solution in Scala (2.11.7) (judge verdict: Accepted)

import java.util.Scanner

import scala.collection.mutable.ArrayBuffer
import scala.math._

object Main {

  case class Point(grids: Vector[Int])

  def solve(sc: Scanner): String = {
    val (n, d) = (sc.nextInt(), sc.nextInt())
    val points = Vector.fill(n)(Vector.fill(d)(sc.nextInt())).map(v => Point(v))

    points.indices.foldLeft(ArrayBuffer.empty[Int]) { case (acc, i) =>
      acc += getGoodDistanceCount(points, i)
      acc
    }.sum.toString
  }

  def getGoodDistanceCount(points: Vector[Point], from: Int): Int = {
    val base = points(from)
    var count = 0
    points.slice(from + 1, points.size).foreach { p =>
      val d = getDistance(base, p)
      if (d.toInt == d) count += 1
    }
    count
  }

  def getDistance(p1: Point, p2: Point) = {
    sqrt(p1.grids.zipWithIndex.foldLeft(ArrayBuffer.empty[Double]) { case (acc, (grid, i)) =>
      acc += pow(grid - p2.grids(i), 2)
      acc
    }.sum)
  }

  def main(args: Array[String]): Unit = {
    val sc = new Scanner(System.in)
    val writer = new java.io.PrintWriter(System.out)
    writer.println(solve(sc))
    writer.flush()
  }

  private def debug(x: Any): Unit = {
    if (System.getenv("LOCAL_DEBUG") != null) println(x)
  }
}