let split s = Str.split (Str.regexp_string " ") s;;
let (r,c) = Scanf.scanf "%d %d\n" (fun x y->(x,y))

let total = Array.init (c+1) (fun x->0);;
let rec loop (sh:'a array list) = function
    0 -> sh@[total]
  | i ->
     let line = read_line () |> split
                |> List.map int_of_string in
     let l = line @ [(List.fold_left (+) 0 line)] |> Array.of_list in
     for i = 0 to (Array.length l)-1 do
       total.(i) <- total.(i) + l.(i)
     done;
     loop (sh@[l] ) (i-1);;

let rec print_list = function
    [] -> ()
  | (h:'a array)::t -> print_array h;print_list t and
    print_array a =
      Array.fold_left (fun x y -> (if x="" then "" else x^" ")^(string_of_int y)) "" a
      |> print_endline;;

let () = loop [] r |> print_list;;