#!/usr/bin/env python

# -*- coding:utf-8 -*-

import time

import sys

import io

import re

import math

#start = time.time()

start = time.clock()

l=[]

m=[]

s=[]

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

for i in range(r):

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

### ???????????則??\?????????

for cc in range(c):

    a=0

    for rr in range(r):

        a+=int(l[rr][cc])

#        print int(l[rr][cc])

    s.append(a)

for j in range(r):

    print(' '.join(map(str,l[j])),str(sum(l[j])))

print(' '.join(map(str,s)),str(sum(s)))
r,c=list(map(int,input().split()))

l=[]

for i in range(r):l.append(list(map(int,input().split())))

l.append([0]*(c+1))

for i in range(c+1):

    for j in range(r):

        if i==0:l[j].append(sum(l[j]))

        l[-1][i]+=l[j][i]

for i in range(r+1):print(' '.join(map(str,l[i])))
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import time
import sys
import io
import re
import math

# start = time.time()
start = time.clock()
l = []
m = []
s = []
(r, c) = list(map(int, input().split()))
for i in range(r):
    l.append(list(map(int, input().split())))
### ???????????則??\?????????
for cc in range(c):
    a = 0
    for rr in range(r):
        a += int(l[rr][cc])
    #        print int(l[rr][cc])
    s.append(a)
for j in range(r):
    print(" ".join(map(str, l[j])), str(sum(l[j])))
print(" ".join(map(str, s)), str(sum(s)))

r, c = list(map(int, input().split()))
l = []
for i in range(r):
    l.append(list(map(int, input().split())))
l.append([0] * (c + 1))
for i in range(c + 1):
    for j in range(r):
        if i == 0:
            l[j].append(sum(l[j]))
        l[-1][i] += l[j][i]
for i in range(r + 1):
    print(" ".join(map(str, l[i])))

-#!/usr/bin/env python -# -*- coding:utf-8 -*- -import time -import sys -import io -import re -import math - -# start = time.time() -start = time.clock() +r, c = list(map(int, input().split())) -m = [] -s = [] -(r, c) = list(map(int, input().split())) -### ???????????則??\????????? -for cc in range(c): -    a = 0 -    for rr in range(r): -        a += int(l[rr][cc]) -    #        print int(l[rr][cc]) -    s.append(a) -for j in range(r): -    print(" ".join(map(str, l[j])), str(sum(l[j]))) -print(" ".join(map(str, s)), str(sum(s))) +l.append([0] * (c + 1)) +for i in range(c + 1): +    for j in range(r): +        if i == 0: +            l[j].append(sum(l[j])) +        l[-1][i] += l[j][i] +for i in range(r + 1): +    print(" ".join(map(str, l[i])))