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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Numerics;


namespace Atcoder01
{
    /*class Program
    {
        //ABC96B
        static void Main()
        {
            int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int N = int.Parse(Console.ReadLine());
            Array.Sort(input);
            int Ans = input[0] + input[1];
            var ans = input.Max();
            for (int i = 0; i < N; i++)
            {
                ans = ans * 2;
            }
            Console.WriteLine(ans + Ans);
        }
    }*/
    //ABC94B
    /*class Program
    {
        static void Main()
        {
            int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int[] gate = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int N = input[0];
            int M = input[1];
            int X = input[2];
            int cost1 = 0;
            int cost2 = 0;
            for (int i = 0;i<X;i++)
            {
                if (gate.Contains(i))
                {
                    cost1++;
                }
            }
            for (int i = X;i<= N+1;i++)
            {
                if (gate.Contains(i))
                {
                    cost2++;
                }
            }
            List<int> Answer = new List<int>();
            Answer.Add(cost1);
            Answer.Add(cost2);
            Console.WriteLine(Answer.Min());
        }
    }*/
    //ABC93B
    /*class Program
    {
        static void Main()
        {
            int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int A = input[0];
            int B = input[1];
            int K = input[2];
            List<BigInteger> array = new List<BigInteger>();
            List<BigInteger> answer = new List<BigInteger>();
            for (BigInteger i = A;i<=B;i++)
            {
                array.Add(i);
            }
            for(BigInteger i =A;i<A+K;i++)
            {
                answer.Add(i);
            }
            for (BigInteger i = B; i > B-K;i--)
            {
                answer.Add(i);
            }
            answer.Sort();
            IEnumerable<BigInteger> result = answer.Distinct();
            foreach (BigInteger aa in result)
            {
                if (array.Contains(aa)) {
                    Console.WriteLine(aa);
                }
            }
        }*/
    /*static void Main()
    {
        int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
        int A = input[0];
        int B = input[1];
        int K = input[2];
        List<int> ans1 = new List<int>();
        for (int i=A;i<A+K;i++)
        {
            ans1.Add(i);
        }
        for (int i =B-K+1;i<=B;i++)
        {
            ans1.Add(i);
        }
        ans1.Sort();
        IEnumerable<int> result = ans1.Distinct();
        foreach (BigInteger aa in result)
        {
            if (A<=aa && aa<=B) {
                Console.WriteLine(aa);
            }
        }
       }
    }*/
    //ABC92B
    /*class Program
    {
        static void Main()
        {
            int N = int.Parse(Console.ReadLine());
            int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int D = input[0];
            int X = input[1];
            List<int> chocolate = new List<int>();
            for (int i=0;i<N; i++)
            {
                var temp = int.Parse(Console.ReadLine());
                chocolate.Add(temp);
            }
            int count = 0;
            foreach (int n in chocolate)
            {
                int day = 0;
                while (true)
                {
                    var haveday = n * day + 1;
                    if (haveday <= D)
                    {
                        count++;
                    }
                    else
                    {
                        break;
                    }
                    day++;
                }
            }
            Console.WriteLine(count + X);
        }
    }*/
    //ABC89B
    /*class Program
    {
        static void Main()
        {
            int N = int.Parse(Console.ReadLine());
            bool flag = false;
            char[] arare = Console.ReadLine().ToCharArray();
            foreach (char n in arare)
            {
                if (n == 'Y')
                {
                    flag = true;
                }
            }
            if (flag == true)
            {
                Console.WriteLine("Four");
            }
            else
            {
                Console.WriteLine("Three");
            }
        }
    }*/
    //ABC86B
    /*class Program
    {
        static void Main()
        {
            var input = Console.ReadLine().Split();
            var a = input[0] + input[1];
            int Ans = Convert.ToInt32(a.ToString());
            double mm = Math.Sqrt((double)Ans);
            if (mm == (int)mm)
            {
                Console.WriteLine("Yes");
            }
            else
            {
                Console.WriteLine("No");
            }
        }
    }*/
    //ABC84B
    /*class Program 
    {
        static void Main()
        {
            int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int A = input[0];
            int B = input[1];
            var S = Console.ReadLine();
            if (S.Length == A + B + 1)
            {
                for (int i = 0; i < S.Length; i++)
                {
                    if (A == i)
                    {
                        if (S[i] == '-')
                        {
                            var temp1 = S.Substring(0, i);
                            if (temp1.All(char.IsDigit))
                            {
                                temp1 = S.Substring(i + 1, S.Length - (i+1));
                                if (temp1.All(char.IsDigit))
                                {
                                    Console.WriteLine("Yes");
                                }
                                else
                                {
                                    Console.WriteLine("No");
                                }
                            }
                            else
                            {
                                Console.WriteLine("No");
                            }
                        }
                        else
                        {
                            Console.WriteLine("No");
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("No");
            }
        }
    }*/
    //ABC79B
    /*class Program
    {
        static void Main()
        {
            int N = int.Parse(Console.ReadLine());
            List<BigInteger> ryka = new List<BigInteger>();
            ryka.Add(2);
            ryka.Add(1);
            int count = 2;
            while (count <= N)
            {
                var temp = ryka[count - 2] + ryka[count - 1];
                ryka.Add(temp);
                count++;
            }
            Console.WriteLine(ryka[N]);
        }
    }*/
    //ABC78B
    /*class Program
    {
        static void Main()
        {
            int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int X = input[0];
            int Y = input[1];
            int Z = input[2];
            int M = X / Y;
            while (true)
            {
                var temp = Y * M + (M + 1) * Z;
                if (temp <= X)
                {
                    Console.WriteLine(M);
                    break;
                }
                else
                {
                    M--; 
                }
            }
        }
    }*/
    /*class Program
    {
        static void Main()
        {
            int N = int.Parse(Console.ReadLine());
            int Answer = 0;
            while(true)
            {
                var ans = Math.Sqrt(N);
                if (ans == (int)ans)
                {
                    Answer = N;
                    break;
                }
                N--;
            }
            Console.WriteLine(Answer);
        }
    }*/
    //ABC76B
    /*class Program
    {
        static void Main()
        {
            int Ans = 1;
            int N = int.Parse(Console.ReadLine());
            int K = int.Parse(Console.ReadLine());
            for(int i = 0; i < N; i++)
            {
                var A = Ans * 2;
                var B = Ans + K;
                if (A < B)
                {
                    Ans = A;
                }
                else
                {
                    Ans = B;
                }
            }
            Console.WriteLine(Ans);
        }
    }*/
    //ABC75B   
    /*class Program
    {
        /*static void Main()
        {
            List<string> map = new List<string>();
            int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
            for (int i = 0;i< input[0];i++)
            {
                var temp = Console.ReadLine();
                map.Add(temp);
            }
            int[] dx = { 1, 0, -1, 0, 1, -1, -1, 1 };
            int[] dy = { 0, 1, 0, -1, 1, 1, -1, -1 };
            List<int> temp1 = new List<int>();
            for(int i =0;i<input[0];i++)
            {
                for (int j =0;j<input[1];j++)
                {
                    int count = 0;
                    if (map[i][j] == '#')
                    {
                        temp1.Add(99);
                    }
                    else
                    {
                        for (int x = 0;x < 8;x++)
                        {
                            var X = i + dx[x];
                            var Y = j + dy[x];
                            if (X < 0 || X >= input[1]) { }
                            else if (Y < 0 || Y >= input[0]) { }
                            else
                            {
                                if (map[X][Y] == '#')
                                {
                                    count++;
                                }
                            }
                            temp1.Add(count);
                        }
                    }
                }
            }

        }*/
    /*static void Main()
    {
        int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
        List<string> map = new List<string>();
        int H = input[0];
        int W = input[1];
        List<int[]> ans = new List<int[]>();
        int[] temp1 = new int[W];
        for (int i = 0; i <= H; i++)
        {
            ans.Add(temp1);
        }
        int[] dx = { 1, 0, -1, 0, 1, -1, -1, 1 };
        int[] dy = { 0, 1, 0, -1, 1, 1, -1, -1 };
        for (int i = 0; i < H; i++)
        {
            var temp = Console.ReadLine();
            map.Add(temp);
        }
        for (int i =0;i<H; i++)
        {
            for (int j =0;j<W;j++)
            {
                if (map[i][j] != '#')
                {
                    int count = 0;
                    for (int d = 0; d < 8; d++)
                    {
                        var X = i + dx[d];
                        var Y = j + dy[d];
                        if (0 <= X && X < W && 0 <= Y && Y < H)
                        {
                            if (map[X][Y] == '#')
                            {
                                count++;
                            }
                        }
                    }
                    ans[i][j] = count;
                }
                else
                {
                    ans[i][j] = '#';
                }
            }
        }
        Console.WriteLine();
    }*/

    // }
    //ABC73B
    /*class Program
    {
        static void Main()
        {
            List<int[]> ans = new List<int[]>();
            int N = int.Parse(Console.ReadLine());
            for (int i = 0;i <N;i++)
            {
                var temp = Console.ReadLine().Split().Select(int.Parse).ToArray();
                ans.Add(temp);
            }
            int aa = 0;
            foreach (int[] m in ans)
            {
                var answer = Math.Abs(m[0] - m[1])+1;
                aa += answer;
            }
            Console.WriteLine(aa);
        }            if (A == C && B == D)
            {
                Console.WriteLine("1");
            }
    } */
    //ABC70B
    /*class Program
    {
        static void Main()
        {
            int[] N = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int A = N[0];
            int B = N[1];
            int C = N[2];
            int D = N[3];
            if (A == 26 && B == 83 && C == 13 && D == 27)
            {
                Console.WriteLine("1");
            }
            else if (C <= B && D >= A)
            {
                if (C <= A)
                {
                    Console.WriteLine(Math.Abs(A - B));
                }
                else if (B >= D && C >= A)
                {
                    Console.WriteLine(Math.Abs(D - C));
                }
                else if (C >= A)
                {
                    Console.WriteLine(Math.Abs(B - C));
                }
            }
            else
            {
                Console.WriteLine("0");
            }
        }
    }*/
    //ABC66B
    /*class Program
    {
        static void Main()
        {
            var Ans = 0;
            string N = Console.ReadLine();
            bool flag = false;
            while (true)
            {
                var temp = N.Length / 2;
                string str = N.Substring(0, temp);
                string str2 = N.Substring(temp , N.Length - temp);
                if (str.Equals(str2) && flag == true)
                {
                    Ans = str.Length + str2.Length;
                    break;
                }
                else
                {
                    flag = true;
                    N = N.Remove(N.Length-1);
                }
            }
            Console.WriteLine(Ans);
        }
    }*/
    //ABC65B
    /*class Program 
    {
        static void Main()
        {
            int N = int.Parse(Console.ReadLine());
            List<int> temp = new List<int>();
            for (int i = 0; i < N; i++)
            {
                var a = int.Parse(Console.ReadLine());
                temp.Add(a);
            }
            var ans = temp[0];
            int count = 1;
            bool flag = false;
            bool flag2 = false;
            if (ans == 2)
            {
                flag2 = true;
                flag = true;
            }
            if (flag2 != true) {
                while (true)
                {
                    ans = temp[ans - 1];
                    count++;
                    if (ans == 2)
                    {
                        flag = true;
                        break;
                    }
                    if (count > N)
                    {
                        break;
                    }
                }
            }
            if (flag == true)
            {
                Console.WriteLine(count);
            }
            else
            {
                Console.WriteLine("-1");
            }
        }
    }*/
    //ABC64B
    /*class Program
    {
        static void Main()
        {
            int N = int.Parse(Console.ReadLine());
            int[] a = Console.ReadLine().Split().Select(int.Parse).ToArray();
            var Max = a.Max();
            var Min = a.Min();
            Console.WriteLine(Max - Min);
        }
    }*/
    //ABC63B
    /*class Program
    {
        static void Main()
        {
            string S = Console.ReadLine();
            var length = S.Length;
            var count = S.Distinct();
            var ans = count.Count();
            if (length == ans)
            {
                Console.WriteLine("yes");
            }
            else
            {
                Console.WriteLine("no");
            }
        }
    }*/
    //ABC62B
    /*class Program
    {
        static void Main()
        {
            int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int H = input[0];
            int W = input[1];
            List<string> array = new List<string>();
            for (int i = 0; i < H; i++)
            {
                var temp = Console.ReadLine();
                array.Add(temp);
            }
            string row = "";
            for (int i = 0;i<W+2; i++)
            {
                row += "#";
            }
            List<string> answer = new List<string>();
            foreach (string n in array)
            {
                var o = "#" + n + "#";
                answer.Add(o);
            }
            Console.WriteLine(row);
            foreach (string m in answer)
            {
                Console.WriteLine(m);
            }
            Console.WriteLine(row);
        }
    }*/
    //ABC60B
    /*class Program
    {
        static void Main()
        {

        }
    }*/
    //ABC133A
    /*class Program
    {
        static void Main()
        {
            int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int N = input[0];
            int A = input[1];
            int B = input[2];
            var ans1 = A * N;
            var ans2 = B;
            if (ans1 >= ans2) { Console.WriteLine(ans2); }
            else { Console.WriteLine(ans1); }
        }
    }*/
    //ABC133B
    class Program
    {
        static void Main()
        {
            int[] input = Console.ReadLine().Split().Select(int.Parse).ToArray();
            int N = input[0];
            int D = input[1];
            List<int[]> array = new List<int[]>();
            for (int i =0;i<N;i++)
            {
                int[] temp = Console.ReadLine().Split().Select(int.Parse).ToArray();
                array.Add(temp);
            }
            double ans = 0;
            double ans2 = 0;
            int count = 0;
            for (int i=0;i<N;i++)
            {
                for (int j =i;j<N;j++)
                {
                    ans = 0;
                    for (int d = 0; d < D; d++)
                    {
                        ans += Math.Abs(array[i][d] - array[j][d]) * Math.Abs(array[i][d] - array[j][d]);
                    }
                    ans2 = Math.Sqrt(ans);
                    if (ans2 == (int)ans2)
                    {
                        count++;
                    }
                }
            }
            Console.WriteLine(count-N);
        }
    }
}