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

a = [0]*(c+1)

for _ in range(r):

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

    v.append(sum(v))

    print((*v))

    a = [x+y for x, y in zip(a, v)]

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

a = [0]*(c+1)

for _ in range(r):

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

    s = 0

    for i, e in enumerate(v):

        s+=e

        a[i]+=e

    v.append(s)

    print((*v))

    a[-1]+=s

print((*a))
r, c = list(map(int, input().split()))
a = [0] * (c + 1)
for _ in range(r):
    v = list(map(int, input().split()))
    v.append(sum(v))
    print((*v))
    a = [x + y for x, y in zip(a, v)]
print((*a))

r, c = list(map(int, input().split()))
a = [0] * (c + 1)
for _ in range(r):
    v = list(map(int, input().split()))
    s = 0
    for i, e in enumerate(v):
        s += e
        a[i] += e
    v.append(s)
    print((*v))
    a[-1] += s
print((*a))

-    v.append(sum(v)) +    s = 0 +    for i, e in enumerate(v): +        s += e +        a[i] += e +    v.append(s) -    a = [x + y for x, y in zip(a, v)] +    a[-1] += s