let words str =
  let len = String.length str in
  let buf = Buffer.create 128 in
  let rec aux i l =
    if i >= len then Buffer.contents buf :: l
    else 
      match str.[i] with
      | ' ' ->
        let s = 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.scanf "%d %d\n" (fun r c -> r,c) in
  let buf = Buffer.create 128 in
  let rec loop sum = function
    | 0 -> print_string (Buffer.contents buf)
    | i ->
      let line = read_line () in
      let s = List.fold_left (fun acc v -> acc + (int_of_string v))
          0 (words line) in
      Buffer.add_string buf
        (if i = 1 then Printf.sprintf "%s %d %d\n" line s (sum+s)
         else Printf.sprintf "%s %d\n" line s);
      loop (sum+s) (i-1)
  in loop 0 r