main = do
    fs <- getLine
    ss <- getContents
    let ls = map (map (\x -> read x :: Int)) $ map words $ lines ss
    let tbl = (map showCalcedRow ls) ++ [showCalcedCol ls]
    mapM_ putStrLn tbl

showCalcedRow :: [Int] -> String
showCalcedRow l = unwords $ map show $ l ++ [sum l]

showCalcedCol :: [[Int]] -> String
showCalcedCol ls = unwords $ map show total
  where
    total = subtotal ++ [sum subtotal]
    subtotal = map (sumCol ls) [0..((length $ ls!!0)-1)]
    sumCol ls idx = sum $ map (\line -> line!!idx) ls
