r,c = gets.split(" ").map(&:to_i)
matrix = r.times.map { gets.split(" ").map(&:to_i) }
sum_rows = matrix.map {|xs| xs.reduce(:+) }
sum_cols = c.times.map {|j|
  r.times.map {|i| matrix[i][j] }.reduce(:+)
}

sum_cols << sum_cols.inject(:+)
r.times {|i| matrix[i] << sum_rows[i] }
matrix << sum_cols

puts matrix.map {|xs| xs.join(" ") }.join("\n")