l,c = gets.chomp.split.map(&:to_i)
ary_c = Array.new
sum_y_col = Array.new(c,0)
l.times{|li|
  ary_li = gets.chomp.split.map(&:to_i)
  c.times{|ci|
    sum_y_col[ci] += ary_li[ci]
  }
  ary_li << ary_li.inject(:+)
  ary_c << ary_li

 }
sum_y_col << sum_y_col.inject(:+)
ary_c << sum_y_col

ary_c.each_with_index{ |y,yi|
    y.each_with_index{|x,xi|
      print x
      print " " if xi != c
    }
    puts
}

