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 Rust (1.15.1) (judge verdict: Accepted)

#![allow(unused_imports)]
#![allow(unused_macros)]
#![allow(unknown_lints)]
#![allow(unused_variables)]
#![allow(unused_must_use)]
#![allow(unused_mut)]
#![allow(dead_code)]
#![allow(unused_assignments)]

use std::cmp;
use std::process::exit;
use std::collections::{
    HashMap,
    VecDeque,
    HashSet
};
use std::io::{
    stdin,
    stdout,
    stderr,
    Read,
    Write,
    BufReader,
    BufWriter
};

macro_rules! debug {
    (($t:expr, $e:expr)) => {
        #[cfg(debug_assertions)]
        {
            let (e, mut err) = (stringify!($e), stderr());
            writeln!(err, "\x1B[33m{}\x1B[0m = {:?}", $t, $e).unwrap()
        }
    };
    ($e:expr) => {
        #[cfg(debug_assertions)]
        {
            let (e, mut err) = (stringify!($e), stderr());
            writeln!(err, "\x1B[33m{}\x1B[0m = {:?}", e, $e).unwrap()
        }
    };
}

macro_rules! d {
    ($($tt:tt),*) => {
        #[cfg(debug_assertions)]
        $({
            debug!($tt);
        })*
    };
}

macro_rules! e {
    ($($arg:tt)*) => {
        #[cfg(debug_assertions)]
        {
            let mut err = stderr();
            let e = format!($($arg)*);
            write!(err, "\x1B[32m{}\x1B[0m", e).unwrap()
        }
    };
}

macro_rules! eln {
    ($($arg:tt)*) => {
        #[cfg(debug_assertions)]
        {
            let mut err = stderr();
            let e = format!($($arg)*);
            writeln!(err, "\x1B[32m{}\x1B[0m", e).unwrap()
        }
    };
}

macro_rules! stdin {
    () => ({
        use std::io::Read;
        let mut s = String::new();
        std::io::stdin().read_to_string(&mut s).unwrap();
        s
    })
}

macro_rules! test {
    (name = $name:ident, $($input:expr => $output:expr),* $(,)*) => (
        #[test]
        fn $name() {
            $(
                assert_eq!(solve($input.to_string()), $output);
            )*
        }
    );
    ($($input:expr => $output:expr),* $(,)*) => (
        #[test]
        fn solve_test() {
            $(
                assert_eq!(solve($input.to_string()), $output);
            )*
        }
    );
}

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

pub trait VecUtils<T: Clone> {
    fn npop(&mut self, n: usize) -> Result<Vec<T>, String>;
    fn nget(&self, n: usize) -> Result<Vec<T>, String>;
    fn is_valid_range(&self, idx_a: usize, idx_b: usize) -> bool;
    fn get_range(&self, idx_a: usize, idx_b: usize) -> Result<Vec<T>, String>;
    fn pop_range(&mut self, idx_a: usize, idx_b: usize) -> Result<Vec<T>, String>;
    fn mul(&self, n: usize) -> Vec<T>;
}

pub trait StrUtils {
    fn npop(&mut self, n: usize) -> Result<String, String>;
    fn nget(&self, n: usize) -> Result<String, String>;
    fn is_valid_range(&self, idx_a: usize, idx_b: usize) -> bool;
    fn get_range(&self, idx_a: usize, idx_b: usize) -> Result<String, String>;
    fn pop_range(&mut self, idx_a: usize, idx_b: usize) -> Result<String, String>;
    fn mul(&self, n: usize) -> String;
}

impl<T: Clone> VecUtils<T> for Vec<T> {
    fn npop(&mut self, n: usize) -> Result<Vec<T>, String> {
        let len = self.len();
        if n > len {
            return Err("too large num".to_string());
        }
        let mut res: Vec<T> = Vec::new();
        for _ in 0..n {
            let x = self.pop().ok_or("".to_string())?;
            res.push(x);
        }
        res.reverse();
        Ok(res)
    }

    fn nget(&self, n: usize) -> Result<Vec<T>, String> {
        let len = self.len();
        if n > len {
            return Err("too large num".to_string());
        }
        let mut res: Vec<T> = Vec::new();
        for i in 0..n {
            res.push(self[len - 1 - i].clone());
        }
        Ok(res)
    }

    fn is_valid_range(&self, idx_a: usize, idx_b: usize) -> bool {
        let mut can = true;
        let self_len = self.len();
        if self_len < idx_a {
            can = false;
        }
        if self_len < idx_b {
            can = false;
        }
        can
    }

    fn get_range(&self, idx_a: usize, idx_b: usize) -> Result<Vec<T>, String> {
        if ! self.is_valid_range(idx_a, idx_b) {
            return Err("invalid range".to_string());
        }
        let mut res: Vec<T> = Vec::new();
        for (i,x) in self.into_iter().enumerate() {
            let i = i;
            if i < idx_a {
                continue;
            }
            if idx_b <= i {
                break;
            }
            res.push(x.clone());
        }
        Ok(res)
    }

    fn pop_range(&mut self, idx_a: usize, idx_b: usize) -> Result<Vec<T>, String> {
        if ! self.is_valid_range(idx_a, idx_b) {
            return Err("invalid range".to_string());
        }
        let mut res: Vec<T> = Vec::new();
        let mut pop_idxs: Vec<usize> = Vec::new();
        for (i,x) in self.iter().enumerate() {
            let i = i;
            if i < idx_a {
                continue;
            }
            if idx_b <= i {
                break;
            }
            res.push(x.clone());
            pop_idxs.push(i);
        }
        for _ in 0..pop_idxs.len() {
            match pop_idxs.pop() {
                Some(idx) => {
                    self.remove(idx as usize);
                },
                None => {}
            }
        }
        Ok(res)
    }

    fn mul(&self, n: usize) -> Vec<T> {
        let mut res: Vec<T> = Vec::new();
        for _ in 0..n {
            for x in self {
                res.push(x.clone());
            }
        }
        res
    }
}

impl StrUtils for String {
    fn npop(&mut self, n: usize) -> Result<String, String> {
        let mut self_chars: Vec<char> = self.chars().collect();
        match self_chars.npop(n) {
            Ok(poped) => {
                *self = self_chars.into_iter().collect::<String>();
                Ok(poped.into_iter().collect::<String>())
            },
            Err(e) => {
                Err(e)
            }
        }
    }

    fn nget(&self, n: usize) -> Result<String, String> {
        let self_chars: Vec<char> = self.chars().collect();
        match self_chars.nget(n) {
            Ok(v) => {
                Ok(v.into_iter().collect::<String>())
            },
            Err(e) => {
                Err(e)
            }
        }
    }

    fn is_valid_range(&self, idx_a: usize, idx_b: usize) -> bool {
        let self_chars: Vec<char> = self.chars().collect();
        self_chars.is_valid_range(idx_a, idx_b)
    }

    fn get_range(&self, idx_a: usize, idx_b: usize) -> Result<String, String> {
        let self_chars: Vec<char> = self.chars().collect();
        match self_chars.get_range(idx_a, idx_b) {
            Ok(v) => {
                Ok(v.into_iter().collect::<String>())
            },
            Err(e) => {
                Err(e)
            }
        }
    }

    fn pop_range(&mut self, idx_a: usize, idx_b: usize) -> Result<String, String> {
        let mut self_chars: Vec<char> = self.chars().collect();
        match self_chars.pop_range(idx_a, idx_b) {
            Ok(poped) => {
                *self = self_chars.into_iter().collect::<String>();
                Ok(poped.into_iter().collect::<String>())
            },
            Err(e) => {
                Err(e)
            }
        }
    }

    fn mul(&self, n: usize) -> String {
        let mut res: String = String::new();
        for _ in 0..n {
            res.push_str(self);
        }
        res
    }
}

fn main() {
    print!("{}", solve(stdin!()));
}

/* TEMPLATE END */

fn solve(src: String) -> String {
    let mut res = String::from("");
    input!{
        source = src,
        n: i32,
        d: i32,
        x_v_v: [[i32; d]; n],
    }
    eln!("###########################################");
    d!(n, d, x_v_v);
    eln!("-------------------------------------------");
    let mut ans = 0;
    let mut combinations: HashSet<(i32, i32)> = HashSet::new();
    for i in 0..n {
        for j in 0..n {
            let mut tmp_i = 0;
            let mut tmp_j = 0;
            d!(i, j);
            if i == j {
                continue;
            } else if i > j {
                tmp_i = j;
                tmp_j = i;
            } else {
                tmp_i = i;
                tmp_j = j;
            }
            d!(tmp_i, tmp_j);
            if ! combinations.contains(&(tmp_i, tmp_j)) {
                combinations.insert((tmp_i, tmp_j));
            }
        }
    }
    d!(combinations);
    for (v1_idx, v2_idx) in combinations {
        d!(v1_idx, v2_idx);
        let v1 = x_v_v.get(v1_idx as usize).unwrap();
        let v2 = x_v_v.get(v2_idx as usize).unwrap();
        let mut distance: f64 = 0.0;
        for i in 0..d {
            let tmp = v1[i as usize] - v2[i as usize];
            let tmp = tmp.abs();
            let tmp = tmp.pow(2);
            distance += tmp as f64;
        }
        let distance = distance.sqrt();
        d!(distance);
        let floored_distance = distance.floor();
        if distance == floored_distance {
            ans += 1;
        }
        d!(ans);
    }
    res.push_str(&ans.to_string());
    d!(res);
    res.push_str("\n");
    res
}