let () =
    let split s =
        let rec loop i cur flag acc =
            if i = String.length s then ( if flag then cur :: acc else acc) else (
                match flag, s.[i] with
                | true,  ' ' -> loop (i + 1) "" false (cur :: acc)
                | false, ' ' -> loop (i + 1) "" false acc
                | true,  v   -> loop (i + 1) (Printf.sprintf "%s%c" cur v) true acc
                | false, v   -> loop (i + 1) (Printf.sprintf "%s%c" cur v) true acc
            )
        in
        List.map int_of_string (List.rev (loop 0 "" false []))
    in
    let main () =
        let n, d = Scanf.sscanf (read_line ()) "%d %d" (fun n d -> n, d) in
        let x = Array.init n (fun _ -> Array.of_list (split (read_line ()))) in
        let judge i =
            let f = float i in
            let sf = sqrt f |> floor in
            let g = int_of_float  (sf *. sf) in
            if i = g then 1 else 0
        in
        let rec loop_i i acc =
            let rec loop_j j acc =
                let rec loop_k k sum =
                    if k = d then judge sum else
                        let xd = x.(i).(k) - x.(j).(k) in
                        loop_k (k + 1) (sum + xd * xd)
                in
                if j = n then acc else loop_j (j + 1) (acc + loop_k 0 0)
            in
            if i = n - 1 then acc else loop_i (i + 1) (loop_j (i + 1) acc)
        in
        Printf.printf "%d\n" (loop_i 0 0)
    in
    main ()