calcRow :: [Int] -> [Int]
calcRow nums = nums ++ [foldl (+) 0 nums]

calcTotals :: [[Int]] -> [Int] -> [Int]
calcTotals [] t = t
calcTotals (x:xs) t = calcTotals xs $ sumEach t x
  where
    sumEach :: [Int] -> [Int] -> [Int]
    sumEach [] [] = []
    sumEach (x:xs) (y:ys) = (x+y):(sumEach xs ys)

printMatrix :: [[Int]] -> IO ()
printMatrix = mapM_ (\n -> putStrLn $ unwords [show x | x <- n])

main :: IO ()
main = do
    [r, c] <- getLine >>= return.(map $ \n -> read n :: Int).words
    matrix <- getMatrix r []
    let totals = calcTotals matrix [0 | _ <- [1..c+1]]
    printMatrix $ matrix ++ [totals]
  where
    getMatrix :: Int -> [[Int]] -> IO [[Int]]
    getMatrix 0 m = return m
    getMatrix n m = do
        nums <- getLine >>= return.(map $ \n -> read n :: Int).words
        getMatrix (n - 1) $ m ++ [calcRow nums]

