{-# LANGUAGE MultiWayIf #-}
import Control.Monad

count :: String -> String -> Int
count "" _ = 0
count _ "" = 0
count (c1:cs1) (c2:cs2) = (if c1 == c2 then 1 else 0) + count cs1 cs2

count' :: String -> String -> String -> Int
count' _ "" _  = 0
count' "" _ "" = 0
count' "" (c2:cs2) (c3:cs3) = (if c2 == c3 then 1 else 0) + count cs2 cs3
count' (c1:cs1) (c2:cs2) "" = (if c1 == c2 then 1 else 0) + count cs1 cs2
count' (c1:cs1) (c2:cs2) (c3:cs3) = (if c1 == c2 then 1 else 0) + (if c2 == c3 then 1 else 0) + count' cs1 cs2 cs3

solve :: String -> String -> String -> Int
solve "" _ "" = 0
solve "" s2@(_:cs2) s3@(_:cs3) = if v2 <= v3
  then count s2 s3 + solve "" cs2 s3
  else count s2 s3 + solve "" s2 cs3
  where
    v2 = count cs2 s3
    v3 = count s2 cs3
solve _ "" _ = 0
solve s1@(_:cs1) s2@(_:cs2) "" = if v1 <= v2
  then count s1 s2 + solve cs1 s2 ""
  else count s1 s2 + solve s1 cs2 ""
  where
    v1 = count cs1 s2
    v2 = count s1 cs2
solve s1@(_:cs1) s2@(_:cs2) s3@(_:cs3) = if
  | v1 <= v2 && v1 <= v3 -> count s1 s2 + solve cs1 s2 s3
  | v2 <= v3 -> count' s1 s2 s3 + solve s1 cs2 s3
  | otherwise -> count s2 s3 + solve s1 s2 cs3
  where
    v1 = count' cs1 s2 s3
    v2 = count' s1 cs2 s3
    v3 = count' s1 s2 cs3

main :: IO ()
main = do
  [h, w] <- map read . words <$> getLine
  c <- replicateM h getLine
  let
    s1 = reverse $ map head c
    s2 = reverse $ map (head . tail) c
    s3 = reverse $ map (head . tail . tail) c
  print $ if w == 2
    then 0
    else solve s1 s2 s3
