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

a,b=[],[0]*c

for i in range(r):

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

    print(*x, end=' ')

    print(sum(x))

    for j in range(c):

        b[j]+=x[j]

print(*b, end=' ')

print(sum(b))

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

a=[0]*c

for i in range(r):

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

    for j in range(c):

        a[j]+=x[j]

    print(*x, end=' ')

    print(sum(x))

print(*a, end=' ')

print(sum(a))

r, c = map(int, input().split())
a, b = [], [0] * c
for i in range(r):
    x = list(map(int, input().split()))
    print(*x, end=" ")
    print(sum(x))
    for j in range(c):
        b[j] += x[j]
print(*b, end=" ")
print(sum(b))

r, c = map(int, input().split())
a = [0] * c
for i in range(r):
    x = list(map(int, input().split()))
    for j in range(c):
        a[j] += x[j]
    print(*x, end=" ")
    print(sum(x))
print(*a, end=" ")
print(sum(a))

-a, b = [], [0] * c +a = [0] * c +    for j in range(c): +        a[j] += x[j] -    for j in range(c): -        b[j] += x[j] -print(*b, end=" ") -print(sum(b)) +print(*a, end=" ") +print(sum(a))