(r, c) = [int(i) for i in input().split()]

 

ct = [0 for d in range(c)]

tmp = []

for rc in range(r):

    tmp = [int(i) for i in input().split()]

    total = 0

    for cc in range(c):

        ct[cc] += tmp[cc]

        total += tmp[cc]

        print(tmp[cc], end=' ')

    print(total)

total = sum(ct)

print(' '.join([str(i) for i in ct]), total)

(r, c) = [int(i) for i in input().split()]

last_row = [0 for _ in range(c+1)]



for _ in range(r):

    row = [int(i) for i in input().split()]

    for cc in range(c):

        last_row[cc] += int(row[cc])

        print(row[cc], end=' ')



    print(sum(row))



last_row[-1] = sum(last_row)

print(' '.join([str(a) for a in last_row]))

(r, c) = [int(i) for i in input().split()]
ct = [0 for d in range(c)]
tmp = []
for rc in range(r):
    tmp = [int(i) for i in input().split()]
    total = 0
    for cc in range(c):
        ct[cc] += tmp[cc]
        total += tmp[cc]
        print(tmp[cc], end=" ")
    print(total)
total = sum(ct)
print(" ".join([str(i) for i in ct]), total)

(r, c) = [int(i) for i in input().split()]
last_row = [0 for _ in range(c + 1)]
for _ in range(r):
    row = [int(i) for i in input().split()]
    for cc in range(c):
        last_row[cc] += int(row[cc])
        print(row[cc], end=" ")
    print(sum(row))
last_row[-1] = sum(last_row)
print(" ".join([str(a) for a in last_row]))

-ct = [0 for d in range(c)] -tmp = [] -for rc in range(r): -    tmp = [int(i) for i in input().split()] -    total = 0 +last_row = [0 for _ in range(c + 1)] +for _ in range(r): +    row = [int(i) for i in input().split()] -        ct[cc] += tmp[cc] -        total += tmp[cc] -        print(tmp[cc], end=" ") -    print(total) -total = sum(ct) -print(" ".join([str(i) for i in ct]), total) +        last_row[cc] += int(row[cc]) +        print(row[cc], end=" ") +    print(sum(row)) +last_row[-1] = sum(last_row) +print(" ".join([str(a) for a in last_row]))