import qualified Data.Vector as V isMas :: (Char, Char) -> Bool isMas x = x == ('M', 'S') || x == ('S', 'M') noEnds :: V.Vector a -> Int -> Int -> Int noEnds v i | i == 0 || i == V.length v - 1 = const 0 | otherwise = id countAll :: V.Vector (V.Vector Char) -> Int countAll v = V.ifoldl' (\acc x l -> acc + noEnds v x (V.ifoldl' (\acc2 y c -> acc2 + noEnds l y (if getAt x y == 'A' && isMas (getAt (x - 1) (y - 1), getAt (x + 1) (y + 1)) && isMas (getAt (x + 1) (y - 1), getAt (x - 1) (y + 1)) then 1 else 0)) 0 l)) 0 v where getAt x y = v V.! x V.! y main :: IO () main = print . countAll . V.fromList . map V.fromList . lines =<< readFile "data.txt"