let words str =
  let len = String.length str in
  let buf = Buffer.create 128 in
  let rec aux i l =
    if i >= len then (int_of_string (Buffer.contents buf))::l
    else 
      match str.[i] with
      | ' ' ->
        let s = int_of_string (Buffer.contents buf) in
        Buffer.clear buf;
        aux (i+1) (s :: l)
      | c   ->
        Buffer.add_char buf c;
        aux (i+1) l
  in aux 0 []

let () =
  let r,c = Scanf.sscanf (read_line ()) "%d %d" (fun r c -> r,c) in
  let buf = Buffer.create 128 in
  let rec loop l sum = function
    | 0 ->
      let sl = List.fold_left (fun a b -> List.map2 (+) a b)
          (List.hd l) (List.tl l) in
      print_string (Buffer.contents buf);
      List.iter (fun x -> Printf.printf "%d " x) (List.rev sl);
      Printf.printf "%d\n" sum;
    | i ->
      let line = read_line () in
      let rl = words line in
      let s = List.fold_left (fun acc v -> acc + v) 0 rl in
      Buffer.add_string buf (Printf.sprintf "%s %d\n" line s);
      loop (rl::l) (sum+s) (i-1)
  in loop [] 0 r