import Control.Applicative
import Control.Monad
import Data.Array
import Data.Array.ST
import Data.List
import qualified Data.Map as M
import qualified Data.Set as S
import Text.Printf

readInts :: IO [Int]
readInts = map read . words <$> getLine

main :: IO ()
main = do
  [r,c] <- readInts
  ary <- listArray ((1,1),(r,c)) . join <$> replicateM r readInts
  forM_ [1..r] $ \i -> do
    forM_ [1..c] $ \j -> do
      printf "%d " $ ary ! (i, j)
    printf "%d\n" $ sum [ ary ! (i, j) | j <- [1..c] ]
  forM_ [1..c] $ \j -> do
    printf "%d " $ sum [ ary ! (i, j) | i <- [1..r] ]
  printf "%d\n" $ sum [ ary ! (i, j) | i <- [1..r], j <- [1..c] ]

