-- Try yukicoder -- author: Leonardone @ NEETSDKASU main = interact $ show . solve . tail . lines solve vs = g vs where g [] = 0 g ("":ts) = g ts g ss@((e:_):_) = w + g zs + g ys where (xs, ys) = span (f e) ss f _ "" = False f c (h:_) = c == h (zs, w) = case length xs of 0 -> ([], 0) 1 -> ([], length $ head xs) len -> (rs, foldl1 (+) $ zipWith (*) [len,len-1..1] [1..len]) where rs = map tail xs