import           Control.Monad
import           Data.Char
import qualified Data.ByteString.Char8 as B
import qualified Data.Vector.Unboxed   as U
import qualified Data.IntSet           as S

main :: IO ()
main = do
  [n, d] <- map read . words <$> getLine
  xss <- replicateM n $ U.unfoldrN d (B.readInt . B.dropWhile isSpace) <$> B.getLine
  print $ solve xss

solve :: [U.Vector Int] -> Int
solve = length . filter p . pairs
  where
    p (xs, ys) = (`S.member` squares) $ U.sum $ U.zipWith sqareDist xs ys
    sqareDist x y = (x - y) ^ 2

squares :: S.IntSet
squares = S.fromList $ map (^ 2) [0 .. 400]

pairs :: [a] -> [(a, a)]
pairs []     = []
pairs [_]    = []
pairs (x:xs) = [(x, y) | y <- xs] ++ pairs xs