import Control.Monad
import Control.Applicative

main = do
  contents <- init . lines <$> getContents
  putStr $ unlines $ map (init . unlines . (map (unwords . (map to5Char))) . addColumn . map addRow) (make2DArrayList contents)

addRow :: [Int] -> [Int]
addRow x = x ++ ((foldr (+) 0 x):[])

addColumn :: [[Int]] -> [[Int]]
addColumn x = x ++ ((foldr1 (zipWith (+)) x):[])

make2DArrayList :: [String] -> [[[Int]]]
make2DArrayList [] = []
make2DArrayList (x:xs) =
  let rows = (read :: String -> Int) x
  in ((map (map (read :: String -> Int) . words)) $ take rows xs) : (make2DArrayList $ drop rows xs)

to5Char :: Int -> String
to5Char x =
  let num = show x
      digits = length num
  in replicate (5 - digits) ' ' ++ num