input = STDIN.gets.split(' ')
r = input[0].to_i
c = input[1].to_i

ss = Array.new(r + 1) {|i|
        Array.new(c + 1) {|j| 0 }
}
r.times {|i|
        input = STDIN.gets.split(' ')
        c.times{|j|
                ss[i][j] = input[j].to_i
        }
}

r.times {|i|
        c.times{|j|
                ss[i][c] += ss[i][j]
                ss[r][j] += ss[i][j]
        }
}
ss[r][c] = ss[r].inject(:+)

for col in ss
        puts col.join(' ')
end