let split str delim =
  let rec doit s acc =
    match
      try Some (String.index s delim)
      with _ -> None
    with
    | None -> s :: acc
    | Some i ->
        let s1 = String.sub s 0 i in
        let s2 = String.sub s (i + 1) (String.length s - String.length s1 - 1) in
        doit s2 (s1 :: acc)
  in
  List.rev (doit str [])

let () =
  let l = read_line () in
  match List.map int_of_string (split l ' ') with
  | [] | [_] -> exit 1
  | r :: c :: _ ->
      let ary = Array.make_matrix (r + 1) (c + 1) 0 in
      for i = 1 to r do
        let l = read_line () in
        let row = Array.of_list (List.map int_of_string (split l ' ')) in
        for j = 1 to c do
          ary.(i - 1).(j - 1) <- row.(j - 1);
          ary.(i - 1).(c) <- ary.(i - 1).(c) + row.(j - 1);
        done;
      done;
      for i = 1 to c do
        for j = 1 to r do
          ary.(r).(i - 1) <- ary.(r).(i - 1) + ary.(j - 1).(i - 1);
        done;
        ary.(r).(c) <- ary.(r).(c) + ary.(r).(i - 1);
      done;
      for i = 0 to r do
        for j = 0 to c do
          if j = 0 then Printf.printf "%d" ary.(i).(j)
          else if j = c then Printf.printf " %d\n" ary.(i).(j)
          else Printf.printf " %d" ary.(i).(j)
        done;
      done