r, c = map(int, input().split())



table = []

for _ in range(r):

    table.append(list(map(int, input().split())))



for i in range(r):

    print(' '.join(map(str, table[i])), end='')

    print(' {}'.format(sum(table[i])))



total = 0

for j in range(c):

    column_sum = 0

    column_sum = sum([row[j] for row in table])

    print('{} '.format(column_sum), end='')

    total += column_sum

print(total)

r, c = list(map(int, input().split()))

table = []

for i in range(r):

    row = list(map(int, input().split()))

    row.append(sum(row))

    table.append(row)



total_row = []

for col in range(c+1):

    column_sum = 0

    for row in range(r):

        column_sum += table[row][col]

    total_row.append(column_sum)

table.append(total_row)



for i in range(r+1):

    print((' '.join(map(str, table[i]))))



r, c = map(int, input().split())
table = []
for _ in range(r):
    table.append(list(map(int, input().split())))
for i in range(r):
    print(" ".join(map(str, table[i])), end="")
    print(" {}".format(sum(table[i])))
total = 0
for j in range(c):
    column_sum = 0
    column_sum = sum([row[j] for row in table])
    print("{} ".format(column_sum), end="")
    total += column_sum
print(total)

r, c = list(map(int, input().split()))
table = []
for i in range(r):
    row = list(map(int, input().split()))
    row.append(sum(row))
    table.append(row)
total_row = []
for col in range(c + 1):
    column_sum = 0
    for row in range(r):
        column_sum += table[row][col]
    total_row.append(column_sum)
table.append(total_row)
for i in range(r + 1):
    print((" ".join(map(str, table[i]))))

-r, c = map(int, input().split()) +r, c = list(map(int, input().split())) -for _ in range(r): -    table.append(list(map(int, input().split()))) -    print(" ".join(map(str, table[i])), end="") -    print(" {}".format(sum(table[i]))) -total = 0 -for j in range(c): +    row = list(map(int, input().split())) +    row.append(sum(row)) +    table.append(row) +total_row = [] +for col in range(c + 1): -    column_sum = sum([row[j] for row in table]) -    print("{} ".format(column_sum), end="") -    total += column_sum -print(total) +    for row in range(r): +        column_sum += table[row][col] +    total_row.append(column_sum) +table.append(total_row) +for i in range(r + 1): +    print((" ".join(map(str, table[i]))))