# frozen_string_literal: true

r, c = readline.split(' ').map(&:to_i)
table = Array.new(r + 1).map{Array.new(c + 1, 0)}
(0...r).each do |i|
  table[i] = readline.chomp.split(' ').map(&:to_i)
  table[i][c] = (0...c).sum { |j| table[i][j] }
  puts table[i].join(' ')
end
(0..c).each do |j|
  table[r][j] = (0...r).sum { |i| table[i][j] }
end
puts table[r].join(' ')

