import Control.Applicative
import Control.Monad
import Data.List

main = do
    n <- getSplittedLine
    let r = n !! 0
        c = n !! 1
    matrix <- forM [1..r] $ \_ -> do
        getSplittedLine
    let rMatrix = rowCalc matrix
        cMatrix = colCalc c rMatrix
        results = rMatrix ++ [cMatrix]
    mapM_ (\x -> putStrLn x) $ rowShow results


getSplittedLine :: IO [Int]
getSplittedLine = fmap (read :: String -> Int) . words <$> getLine

rowCalc :: [[Int]] -> [[Int]]
rowCalc matrix = foldr (\x acc -> (x ++ [sum x]) : acc) [] matrix

colCalc :: Int -> [[Int]] -> [Int]
colCalc n matrix = foldr (\x acc -> zipWith (+) x acc) (replicate (n+1) 0) matrix

rowShow matrix = foldr (\x acc -> (unwords x) : acc) [] $ map (\x -> map (\y -> show y) x) matrix