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 C# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
// using System.Collections.Generic;
// using System.Globalization;
using System.IO;
// using System.Linq;
using System.Text;
// using System.Text.RegularExpressions

class Program
{
    static void Main()
    {
        FastScanner sc = new FastScanner(Console.OpenStandardInput());
        int N = sc.nextInt();
        int D = sc.nextInt();
        int[][] X = new int[N][];
        for (int i = 0; i < N; i++) X[i] = sc.intArray(D);
        int ans = 0;
        for (int i = 0; i < N; i++)
            for (int j = i + 1; j < N; j++)
            {
                double num = 0D;
                for (int k = 0; k < D; k++) num += Math.Pow(X[j][k] - X[i][k], 2);
                double dbl = Math.Sqrt(num);
                if (dbl == Math.Floor(dbl)) ans++;
            }
        Console.WriteLine(ans);
        return;
    }
}

// FastScanner start
public class FastScanner
{
    public readonly Stream str;
    private readonly byte[] buf = new byte[1024];
    private int len, ptr;
    public bool isEof = false;
    public FastScanner(Stream stream)
    {
        str = stream;
    }
    private T[] enumerate<T>(int n, Func<T> f)
    {
        var a = new T[n];
        for (int i = 0; i < n; ++i) a[i] = f();
        return a;
    }
    public char nextChar()
    {
        byte b = 0;
        b = read();
        while (!isEof && (b < 33 || 126 < b)) b = read();
        return (char)b;
    }
    public string nextString()
    {
        byte b = 0;
        StringBuilder builder = new StringBuilder();
        b = read();
        while (!isEof && (b < 33 || 126 < b)) b = read();
        while (!isEof && 33 <= b && b <= 126)
        {
            builder.Append((char)b);
            b = read();
        }
        return builder.ToString();
    }
    public int nextInt()
    {
        return (isEof) ? int.MinValue : (int)nextLong();
    }
    public long nextLong()
    {
        if (isEof) return long.MinValue;
        long ret = 0;
        byte b = 0;
        bool neg = false;
        do b = read();
        while (b != 45 && (b < 48 || 57 < b));
        neg = b == 45;
        if (neg) b = read();
        while (!isEof && 48 <= b && b <= 57)
        {
            ret = ret * 10 + b - 48;
            b = read();
        }
        return neg ? -ret : ret;
    }
    public decimal nextDecimal()
    {
        if (isEof) return decimal.MinValue;
        decimal ret = 0M;
        decimal div = 1M;
        byte b = 0;
        bool neg = false;
        b = read();
        while (!isEof && (b < 48 || 57 < b) && b != 45 && b != 46) b = read();
        neg = b == 45;
        if (neg) b = read();
        while (!isEof && 48 <= b && b <= 57)
        {
            ret = ret * 10 + b - 48;
            b = read();
        }
        if (b == 46) b = read();
        while (!isEof && 48 <= b && b <= 57)
        {
            ret += (b - 48) / (div *= 10);
            b = read();
        }
        if (neg) return -ret; return ret;
    }
    public char[] charArray(int n)
    {
        return enumerate(n, nextChar);
    }
    public string[] stringArray(int n)
    {
        return enumerate(n, nextString);
    }
    public int[] intArray(int n)
    {
        return enumerate(n, nextInt);
    }
    public long[] longArray(int n)
    {
        return enumerate(n, nextLong);
    }
    public decimal[] decimalArray(int n)
    {
        return enumerate(n, nextDecimal);
    }
    private byte read()
    {
        if (isEof)
        {
            return 0;
        }
        if (ptr >= len)
        {
            ptr = 0;
            if ((len = str.Read(buf, 0, 1024)) <= 0)
            {
                isEof = true;
                return 0;
            }
        }
        return buf[ptr++];
    }
}
// FastScanner end