let rec for_fold a b v f = if a >= b then v else for_fold (a+1) b (f a v) f
let rec fix f x = f (fix f) x
let sq x = x * x
let () =
  Scanf.scanf "%d %d" @@ fun n d ->
  let ps = Array.init n @@ fun _ ->
    Array.init d @@ fun _ -> Scanf.scanf " %d" ((+) 0)
  in
  let ans =
    for_fold 0 n 0 @@ fun i v ->
      for_fold (i+1) n v @@ fun j v ->
        let w = for_fold 0 d 0 @@ fun k w -> w + sq (ps.(i).(k) - ps.(j).(k)) in
        let r = fix (fun f x -> if x*x > w then x-1 else f (x+1)) 1 in
        if r * r = w then v+1 else v
  in
  Printf.printf "%d\n" ans
