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

a = [[0 for i in range(c+1)] for j in range(r+1)]

for i in range(r):

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

    for j in range(c):

        a[i][j] = l[j]

        a[i][c] += a[i][j]



for i in range(c+1):

    for j in range(r):

        a[r][i] += a[j][i]



for i in range(r+1):

    for j in range(c+1):

        if j == c:

            print(str(a[i][j]))

        else:

            print(str(a[i][j]) + " ", end="")

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

a = [[int(i) for i in input().split()] for j in range(r)]

b = [[0 for i in range(c+1)] for j in range(r+1)]

for i in range(r):

    for j in range(c):

        b[i][j] = a[i][j]

        b[i][c] += a[i][j]

    

for i in range(c+1):

    for j in range(r):

        b[r][i] += b[j][i]



for i in range(r+1):

    for j in range(c+1):

        if j == c:

            print(b[i][j])

        else:

            print("{0} ".format(b[i][j]), end = "")

r, c = map(int, input().split())
a = [[0 for i in range(c + 1)] for j in range(r + 1)]
for i in range(r):
    l = list(map(int, input().split()))
    for j in range(c):
        a[i][j] = l[j]
        a[i][c] += a[i][j]
for i in range(c + 1):
    for j in range(r):
        a[r][i] += a[j][i]
for i in range(r + 1):
    for j in range(c + 1):
        if j == c:
            print(str(a[i][j]))
        else:
            print(str(a[i][j]) + " ", end="")

r, c = map(int, input().split())
a = [[int(i) for i in input().split()] for j in range(r)]
b = [[0 for i in range(c + 1)] for j in range(r + 1)]
for i in range(r):
    for j in range(c):
        b[i][j] = a[i][j]
        b[i][c] += a[i][j]
for i in range(c + 1):
    for j in range(r):
        b[r][i] += b[j][i]
for i in range(r + 1):
    for j in range(c + 1):
        if j == c:
            print(b[i][j])
        else:
            print("{0} ".format(b[i][j]), end="")

-a = [[0 for i in range(c + 1)] for j in range(r + 1)] +a = [[int(i) for i in input().split()] for j in range(r)] +b = [[0 for i in range(c + 1)] for j in range(r + 1)] -    l = list(map(int, input().split())) -        a[i][j] = l[j] -        a[i][c] += a[i][j] +        b[i][j] = a[i][j] +        b[i][c] += a[i][j] -        a[r][i] += a[j][i] +        b[r][i] += b[j][i] -            print(str(a[i][j])) +            print(b[i][j]) -            print(str(a[i][j]) + " ", end="") +            print("{0} ".format(b[i][j]), end="")