r, c = map(int, input().split())
last = [0 for _ in range(c)]
for _ in range(r):
    ri = list(map(int, input().split()))
    for j in range(c):
        last[j] += ri[j]
    print(*ri, sum(ri))
print(*last, sum(last))
