Problem:
Spreadsheet Your task is to perform a simple table calculation. Write a program which reads the number of rows r , columns c and a table of r × c elements, and prints a new table, which includes the total sum for each row and column. Input In the first line, two integers r and c are given. Next, the table is given by r lines, each of which consists of c integers separated by space characters. Output Print the new table of ( r +1) × ( c +1) elements. Put a single space character between adjacent elements. For each row, print the sum of it's elements in the last column. For each column, print the sum of it's elements in the last row. Print the total sum of the elements at the bottom right corner of the table. Constraints  1 ≤ r , c ≤ 100  0 ≤ an element of the table ≤ 100  Sample Input 4 5
1 1 3 4 5
2 2 2 4 5
3 3 0 1 1
2 3 4 4 6 Sample Output 1 1 3 4 5 14
2 2 2 4 5 15
3 3 0 1 1 8
2 3 4 4 6 19
8 9 9 13 17 56

Solution (Python):
(r,c) = [int(i) for i in input().split()]

table = []
for rc in range(r):
    table.append([int(i) for i in input().split()])

table.append([0 for _ in range(c + 1)])

for rc in range(r):
    row_total = 0
    for cc in range(c):
        table[r][cc] += table[rc][cc]
        row_total += table[rc][cc]

    table[rc].append(row_total)
    table[r][c] += row_total

for row in table:
    print(' '.join([str(column) for column in row]))

Solution (Python):
(r, c) = [int(i) for i in input().split()]

table = []
for rc in range(r):
    table.append([int(i) for i in input().split()])

table.append([0 for _ in range(c + 1)])

for rc in range(r):
    row_total = 0
    for cc in range(c):
        table[r][cc] += table[rc][cc]
        row_total += table[rc][cc]

    table[rc].append(row_total)
    table[r][c] += row_total

for row in table:
        print(' '.join([str(column) for column in row]))

Solution (Python):
while True:
    try:
        r,c = map(int,raw_input().split())
        if r == c == 0:
            break
    except EOFError:
        break
    
    array = []
    for i in range(r):
        array.append(map(int,raw_input().split()))
    
    
    
    c_sum_row = [0 for ii in range(c)]
    for k in range(c):
        for j in range(r):
            c_sum_row[k] += array[j][k]
    array.append(c_sum_row)
    
    
    
    for k in range(r+1):
        array[k].append(sum(array[k]))
    
    for k in range(r+1):
        print ' '.join(map(str,array[k]))

Solution (Python):
def trans(A):
    """????¬??????????????????¢???"""
    return list(map(list, zip(*A)))

if __name__ == '__main__':
    r, c = [int(i) for i in input().split()]
    sheet = [[int(i) for i in input().split()] for y in range(r)]
    [sheet[y].append(sum(sheet[y])) for y in range(r)]
    sheet = trans(sheet)
    [sheet[y].append(sum(sheet[y])) for y in range(c+1)]
    sheet = trans(sheet)
    [print(' '.join([str(i) for i in sheet[y]])) for y in range(r+1)]

Solution (Python):


r,c = map(int,input().split())
sheet = [list(map(int,input().split())) for i in range(r)]

for row in sheet:
    row.append(sum(row))

s = [sum(list(row[i] for row in sheet)) for i in range(c + 1)]
sheet.append(s)

for row in sheet:
    print(*row)

Solution (Python):
import sys

r, c = [ int( val ) for val in sys.stdin.readline().split( " " ) ]
csums = [0]*c
rsum = 0
output = []
for k in range( r ):
	matrix = [ int( val ) for val in sys.stdin.readline().split( " " ) ]

	rsum = 0
	for i in range( c ):
		rsum += matrix[i]
		output.append( "{:d} ".format( matrix[i] ) )	
		csums[i] += matrix[i]
		
	output.append( "{:d}\n".format( rsum ) )

rsum = 0
for val in csums:
	rsum += val
	output.append( "{:d} ".format( val ) )	
	
output.append( "{:d}".format( rsum ) )
print( "".join( output ) )

Solution (Python):
import sys

r, c = [ int( val ) for val in sys.stdin.readline().split( " " ) ]
csums = [0]*c
rsum = 0
output = []
for k in range( r ):
	matrix = [ int( val ) for val in sys.stdin.readline().split( " " ) ]

	rsum = 0
	for i in range( c ):
		rsum += matrix[i]
		output.append( "{:d} ".format( matrix[i] ) )	
		csums[i] += matrix[i]
		
	output.append( "{:d}\n".format( rsum ) )

rsum = 0
for val in csums:
	rsum += val
	output.append( "{:d} ".format( val ) )	
	
output.append( "{:d}".format( rsum ) )
print( "".join( output ) )

Solution (Python):
x,y=map(int,raw_input().split())
a=[map(int,raw_input().split()) for i in range(x)]
for i in range(x):
	a[i].append(sum(a[i]))
	for j in range(y+1):
		print a[i][j],
	print
for i in range(y+1):
	print sum([row[i] for row in a]),

Solution (Python):
r, c = map(int, input().split())
matrix = []
for i in range(r):
    l = list(map(int, input().split()))
    l.append(sum(l))
    matrix.append(l)
matrix.append([sum(i) for i in zip(*matrix)])
for l in matrix:
    print(" ".join(str(n) for n in l))

Solution (Python):
if __name__ == "__main__":

    n, m = map(int, raw_input().split())

    s = [0] * n * m

    for c in xrange(n):
        s[c*m:c*m+m] = map( int, raw_input().split())

    csum = [0] * n
    rsum = [0] * m
    for c in xrange(n):
        for r in xrange(m):
            csum[c] += s[m*c + r]
            rsum[r] += s[m*c + r]
            print s[m*c + r],
        print csum[c]
    asum = 0
    for i in xrange(m):
        asum += rsum[i]
        print rsum[i],
    print asum

Solution (Python):



def main():
    r, c = map(int, raw_input().split())
    rSum = [0] * (c + 1)
    for _ in xrange(r):
        row = map(int, raw_input().split() + ['0'])
        row[-1] = sum(row)
        print ' '.join(map(str, row))
        for i in xrange(c + 1):
            rSum[i] += row[i]
    print ' '.join(map(str, rSum))

if __name__ == '__main__':
    main()

Solution (Python):
r, c = map(int, input().split())
matrix = []
for _ in range(r):
    L = list(map(int, input().split()))
    matrix.append(L + [sum(L)])
matrix.append([sum(column) for column in zip(*matrix)])
for row in matrix:
    print(' '.join(map(str, row)))

Solution (Python):
(r,c) = [int(i) for i in input().split()]
 
table = []
for rc in range(r):
    table.append([int(i) for i in input().split()])
 
table.append([0 for _ in range(c + 1)])
 
for rc in range(r):
    row_total = 0
    for cc in range(c):
        table[r][cc] += table[rc][cc]
        row_total += table[rc][cc]
 
    table[rc].append(row_total)
    table[r][c] += row_total
 
for row in table:
    print(' '.join([str(column) for column in row]))

Solution (Python):
raws, columns = map(int, input().strip().split())
a = []
for i in range(raws):
    a.append(list(map(int, input().strip().split())))
for i in range(raws):
    a[i].append(sum(a[i]))
a.append([sum(i) for i in zip(*a)])
for r in a:
    print(' '.join(str(x) for x in r))

Solution (Python):
( r, c) = [int(i) for i in  input().split()]
num = []
for i in range(r):
    num.append( [ int(i) for i in input().split()])
    
    num[i].append( sum( num[i]))


last_row = [ 0 for _ in range( c+1)]
for rc in range(r):
    for cc in range( c+1):
        last_row[cc] += num[rc][cc]
num.append( last_row)
'''
for i in range(r):
     print( sum( [ num[js] for js in range(c)])
    
'''

for rc in range( r+1):
    print( ' '.join( [ str(a) for a in num[rc]]))

Solution (Python):
(r, c) = [int(i) for i in input().split()]
src = []
for _ in range(r):
    row = [int(i) for i in input().split()]
    row.append(sum(row))
    src.append(row)
last_row = [0 for _ in range(c + 1)]
for rc in range(r):
    for cc in range(c+1):
        last_row[cc] += src[rc][cc]
src.append(last_row)

for rc in range(r+1):
    print(' '.join([str(a) for a in src[rc]]))

Solution (Python):
(r, c) = [int(i) for i in input().split()]
src = []
for _ in range(r):
    row = [int(i) for i in input().split()]
    row.append(sum(row))
    src.append(row)
last_row = [0 for _ in range(c + 1)]
for rc in range(r):
    for cc in range(c+1):
        last_row[cc] += src[rc][cc]
src.append(last_row)
 
for rc in range(r+1):
    print(' '.join([str(a) for a in src[rc]]))

Solution (Python):
(r, c) = [int(i) for i in input().split()]
src = []
for _ in range(r):
    row = [int(i) for i in input().split()]
    row.append(sum(row))
    src.append(row)

last_row = [0 for _ in range(c+1)]
for rc in range(r):
    for cc in range(c+1):
        last_row[cc] += src[rc][cc]
src.append(last_row)


for rc in range(r+1):
    print(' '.join([str(a) for a in src[rc]]))

Solution (Python):
(r, c) = [int(i) for i in input().split()]
src = []
for i in range(r):
    row = [int(i) for i in input().split()]
    row.append(sum(row))
    src.append(row)

last_row = [0 for _ in range(c+1)]
for rc in range(r):
    for cc in range(c+1):
        last_row[cc] += src[rc][cc]
src.append(last_row)
for rc in range(r+1):
    print(' '.join([str(a) for a in src[rc]]))

Solution (Python):
(r, c) = [int(i) for i in input().split()]
src = []
for _ in range(r):
    row = [int(i) for i in input().split()]
    row.append(sum(row))
    src.append(row)

last_row = [0 for _ in range(c + 1)]
for rc in range(r):
    for cc in range(c+1):
        last_row[cc] += src[rc][cc]

src.append(last_row)


for rc in range(r + 1):
    print(' '.join([str(a) for a in src[rc]]))

Solution (Python):
(r, c) = [int(i) for i in input().split()]
last_row = [0 for _ in range(c+1)]

for _ in range(r):
    row = [int(i) for i in input().split()]
    for cc in range(c):
        last_row[cc] += int(row[cc])
        print(row[cc], end=' ')

    print(sum(row))

last_row[-1] = sum(last_row)
print(' '.join([str(a) for a in last_row]))

Solution (Python):
(r, c) = [int(i) for i in input().split()]
last_row = [0 for _ in range(c+1)]
 
for _ in range(r):
    row = [int(i) for i in input().split()]
    for cc in range(c):
        last_row[cc] += int(row[cc])
        print(row[cc], end=' ')
 
    print(sum(row))
 
last_row[-1] = sum(last_row)
print(' '.join([str(a) for a in last_row]))

Solution (Python):
(r, c) = [int(i) for i in input().split()]
last_row = [0 for _ in range(c+1)]
 
for _ in range(r):
    row = [int(i) for i in input().split()]
    for cc in range(c):
        last_row[cc] += int(row[cc])
        print(row[cc], end=' ')
 
    print(sum(row))
 
last_row[-1] = sum(last_row)
print(' '.join([str(a) for a in last_row]))

Solution (Python):
rc = [int(i) for i in input().split()]
data = []

for i in range(rc[0]):
    sum_c = [int(i) for i in input().split()]
    sum_c.append(sum(sum_c))
    data += [sum_c]
    print(" ".join(map(str,sum_c)))

hoge = [[row[i] for row in data] for i in range(rc[1])]

sum_r = [sum(hoge[i]) for i in range(rc[1])]
sum_r.append(sum(sum_r))
print(" ".join(map(str,sum_r)))

Solution (Python):
input_list = raw_input().split(" ")
r = int(input_list[0])
c = int(input_list[1])

i = 0
array = []
while i < r:
  input_line = raw_input().split(" ")
  array.append(map(int, input_line))
  i += 1



for i in xrange(len(array)):
  sum = 0
  for j in xrange(len(array[i])):
    sum += array[i][j]
  array[i].append(sum)



list = []
for i in xrange(c + 1):
  sum = 0
  for j in xrange(r):
    sum += array[j][i]
  list.append(sum)
array.append(list)

for i in xrange(r + 1):
  output = ""
  for j in xrange(c + 1):
    output += " " + str(array[i][j])
  print output.strip()

Solution (Python):
rc = [int(i) for i in input().split()]
data = []
 
for i in range(rc[0]):
    sum_c = [int(i) for i in input().split()]
    sum_c.append(sum(sum_c))
    data += [sum_c]
    print(" ".join(map(str,sum_c)))
 
hoge = [[row[i] for row in data] for i in range(rc[1])]
 
sum_r = [sum(hoge[i]) for i in range(rc[1])]
sum_r.append(sum(sum_r))
print(" ".join(map(str,sum_r)))

Solution (Python):
(r, c) = [int(i) for i in input().split()]
src = []
for _ in range(r):
    row = [int(i) for i in input().split()]
    row.append(sum(row))
    src.append(row)
last_row = [0 for _ in range(c + 1)]
for rc in range(r):
    for cc in range(c+1):
        last_row[cc] += src[rc][cc]
src.append(last_row)
 
for rc in range(r+1):
    print(' '.join([str(a) for a in src[rc]]))

Solution (Python):
(r, c) = [int(i) for i in input().split()]
src = []
for _ in range(r):
    row = [int(i) for i in input().split()]
    row.append(sum(row))
    src.append(row)
 
last_row = [0 for _ in range(c + 1)]
for rc in range(r):
    for cc in range(c+1):
        last_row[cc] += src[rc][cc]
 
src.append(last_row)
 
 
for rc in range(r + 1):
    print(' '.join([str(a) for a in src[rc]]))

Solution (Python):
raws, columns = map(int, input().strip().split())
a = []
for i in range(raws):
    a.append(list(map(int, input().strip().split())))
for i in range(raws):
    a[i].append(sum(a[i]))
a.append([sum(i) for i in zip(*a)])
for r in a:
    print(' '.join(str(x) for x in r))

Solution (Python):
(r, c) = [int(i) for i in input().split()]
src = []
for i in range(r):
    row = [int(i) for i in input().split()]
    row.append(sum(row))
    src.append(row)
 
last_row = [0 for _ in range(c+1)]
for rc in range(r):
    for cc in range(c+1):
        last_row[cc] += src[rc][cc]
src.append(last_row)
for rc in range(r+1):
    print(' '.join([str(a) for a in src[rc]]))

Solution (Python):
(r, c) = [int(i) for i in input().split()]
last_row = [0 for _ in range(c+1)]
 
for _ in range(r):
    row = [int(i) for i in input().split()]
    for cc in range(c):
        last_row[cc] += int(row[cc])
        print(row[cc], end=' ')
 
    print(sum(row))
 
last_row[-1] = sum(last_row)
print(' '.join([str(a) for a in last_row]))

Solution (Python):
r,c=map(int,raw_input().split())
a=[map(int,raw_input().split()) for _ in xrange(r)]
sums,l=0,[]
for i in xrange(r):
	a[i].append(sum(a[i]))
for i in xrange(c+1):
	for j in xrange(r):
		sums+=a[j][i]
	l.append(sums)
	sums=0
a.append(l)
for i in a:
	for j in xrange(c+1):
		if j==c: print i[j]
		else: print i[j],

Solution (Python):
r, c =map(int, raw_input().split())
m = []
for i in range(r):
    m.append(map(int, raw_input().split()))
for i in range(r):
    m[i].append(sum(m[i]))
n = []
for j in range(c + 1):
    k = 0
    for i in range(r):
        k += m[i][j]
    n.append(k)
m.append(n)
for i in range(r + 1):
    print ' '.join(map(str,m[i]))

Solution (Python):
(r, c) = [int(i) for i in input().split()]
src = []
for _ in range(r):
    row = [int(i) for i in input().split()]
    row.append(sum(row))
    src.append(row)
last_row = [0 for _ in range(c + 1)]
for rc in range(r):
    for cc in range(c+1):
        last_row[cc] += src[rc][cc]
src.append(last_row)
 
for rc in range(r+1):
    print(' '.join([str(a) for a in src[rc]]))

Solution (Python):
R, C = map(int, raw_input().split())
chart = []
for r in range(R):
    row = map(int, raw_input().split())
    chart.append(row)
col = [0] * C
for r in range(R):
    row = chart[r]
    row.append(sum(row))
    print ' '.join(map(str, row))
    for c in range(C):
        col[c] += row[c]
for c in range(C):
    print col[c],
print sum(col)

Solution (Python):
R, C = map(int, raw_input().split())
chart = [map(int, raw_input().split()) for r in range(R)]
col = [0] * C
for r in range(R):
    row = chart[r]
    row.append(sum(row))
    print ' '.join(map(str, row))
    for c in range(C):
        col[c] += row[c]
for c in range(C):
    print col[c],
print sum(col)

Solution (Python):
(r, c) = [int(i) for i in input().split()]
src = []
for _ in range(r):
    row = [int(i) for i in input().split()]
    row.append(sum(row))
    src.append(row)
last_row = [0 for _ in range(c + 1)]
for rc in range(r):
    for cc in range(c+1):
        last_row[cc] += src[rc][cc]
src.append(last_row)
  
for rc in range(r+1):
    print(' '.join([str(a) for a in src[rc]]))

Solution (Python):
import sys

r,c = map(int,raw_input().split())
table_list = [[0 for i in range(c)]for j in range(r)]
for k in range(r):
	Sum = 0
	table_list[k] = map(int,raw_input().split())
	for l in table_list[k]:
		Sum += l
	table_list[k].append(Sum)
end_list = []
for m in range(c):
	Sum = 0
	for n in range(r):
		Sum += table_list[n][m]
	end_list.append(Sum)
Sum = 0
for o in end_list:
	Sum += o
end_list.append(Sum)
table_list.append(end_list)
for p in range(r+1):
	for q in range(c+1):
			if q==c:
				print '%d' %table_list[p][q]
			else :
				sys.stdout.write(str(table_list[p][q]))
				sys.stdout.write(' ')

Solution (Python):
r, c = map(int, input().split())

sum_row = []

for i in range(c + 1):
    sum_row.append(0)

for i in range(r):
    tmp_row = tuple(map(int, input().split()))
    tmp_row_sum = sum(tmp_row)
    tmp_row += tmp_row_sum,
    print(" ".join(map(str, tmp_row)))
    for j in range(c + 1):
        sum_row[j] += tmp_row[j]

print(" ".join(map(str, sum_row)))

Solution (Python):
in_line = raw_input().split()
r = int(in_line[0])
c = int(in_line[1])
data = []
for i in range(0,r):
    in_line = raw_input().split()
    data.append([])
    for j in range(0,c):
        data[i].append(int(in_line[j]))
for data_r in data:
    sum_r = 0
    for data_c in data_r:
        sum_r += data_c
        print data_c,
    data_r.append(sum_r)
    print sum_r
for j in range(0,c+1):
    sum_c = 0
    for i in range(0,r):
        sum_c += data[i][j]
    print sum_c,

Solution (Python):
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)

Solution (Python):
r, c = map(int, raw_input().split(" "))
table1 = [[0 for j in range(c)] for i in range(r)]
for i in range(r):
    table1[i] = map(int, raw_input().split(" "))

table2 = [[0 for j in range(c+1)] for i in range(r+1)]
for i in range(r):
    for j in range(c):
        table2[i][j] = table1[i][j]

for i in range(r):
    for j in range(c):
        table2[i][c] += table2[i][j]
        table2[r][j] += table2[i][j]
    table2[r][c] += table2[i][c]

ans = ""
for i in range(r+1):
    for j in range(c+1):
        ans += str(table2[i][j])
        if j!=c:
            ans += " "
    if i!=r:
        ans += "\n"

print ans

Solution (Python):
r, c = map(int, raw_input().split(" "))
table1 = [[0 for j in range(c)] for i in range(r)]
for i in range(r):
    table1[i] = map(int, raw_input().split(" "))

table2 = [0 for j in range(c+1)]
ans = ""
for i in range(r):
    tmp = 0
    for j in range(c):
        ans += str(table1[i][j]) + " "
        tmp += table1[i][j] 
        table2[j] += table1[i][j]
    ans += str(tmp) + "\n"
for j in range(c+1):
    ans += str(table2[j])
    table2[c] += table2[j]
    if j!=c:
        ans += " "

print ans

Solution (Python):
def show2dlist(nlist,r,c):
    for x in xrange(0,r):
        nlist[x] = map(str,nlist[x])
        ans = " ".join(nlist[x])
        print ans
def showlist(nlist,nlistlen):
    for x in xrange(nlistlen):
        print nlist[x],

r,c = map(int,raw_input().split())
table = [[0 for x in xrange(c)] for y in xrange(r)]
i = 0
for x in xrange(r):
    table[i] = map(int,raw_input().split())
    i+=1

csumlist =[0 for x in xrange(0,c+1)]
for x in xrange(0,r):
    table[x].append(sum(table[x]))
    for y in xrange(0,c+1):
        csumlist[y] += table[x][y]
show2dlist(table,r,c+1)
showlist(csumlist,c+1)

Solution (Python):


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

cells = []

for r in range(row):
    temp_col = list(map(int, input().split()))
    temp_col.append(sum(temp_col))
    cells.append(temp_col)

cells.append([0] * (col + 1))
for c in range(col + 1):
    for r in range(row):
        cells[row][c] += cells[r][c]

for r in range(row+1):
    print(' '.join(map(str, cells[r])))

Solution (Python):
rc = input().split()
r, c = map(int, rc)

ss= [[int(i) for i in input().split()] for j in range(r)]
colsum = [0 for i in range(c+1)]

for row in ss:
    row.append(sum(row))
    colsum = [x+y for (x, y) in zip(colsum, row)]
    print(' '.join(map(str, row)))

print(' '.join(map(str, colsum)))

Solution (Python):
data = []
r, c = [int(n) for n in input().split()]
result = [0 for n in range(c)]

for i in range(r):
    data.append([int(n) for n in input().split()])
    for j in range(c):
        result[j] += data[i][j]
data.append(result)
for i in range(r + 1):
    data[i].append(sum(data[i]))
    print(' '.join([str(n) for n in data[i]]))

Solution (Python):
r, c = map(int, input().split())
table = [cc + [sum(cc)] for cc in (list(map(int, input().split())) for _ in range(r))]
table.append(list(map(sum, zip(*table))))
for line in table:
    print(*line)

Solution (Python):
import sys

printf = sys.stdout.write


mat = []
new_map = []

r,c = map(int, raw_input().split())
for i in range(r):
    mat = map(int, raw_input().split())
    new_map.append(mat)


for i in range(r):
    new_map[i].append(sum(new_map[i][:]))
    for j in range(c):
        print new_map[i][j],
    printf(" " + str(new_map[i][j + 1]) + "\n")


for j in range(c + 1):
    a = 0
    for i in range(r):
        a += new_map[i][j]
    if j == c:
        printf(" " + str(a) + "\n")
        break
    else:
        print a,

Solution (Python):
height,width=map(int,input().split())
A=[]
for i in range(height):
	A.append([int(j) for j in input().split()])


for i in range(height):
	print(' '.join(map(str,A[i])),str(sum(A[i])))

B=[]
C=[0 for _ in range(width)]
for i in range(width):
	for j in range(height):
		s=0
		s+=A[j][i]
		B.append(s)
	C[i]=sum(B)
	B=[]
print(' '.join(map(str,C)),str(sum(C)))

Solution (Python):
r, c = (int(i) for i in input().split())
table = tuple([int(x) for x in input().split()] for _ in range(r))
for cols in table:
  cols.append(sum(cols))
  print(*cols)
else:
  sums = tuple(sum(table[y][x] for y in range(r)) for x in range(c + 1))
  print(*sums)

Solution (Python):
r,c = map(int,raw_input().split())
cels = []
for i in xrange(r):
    cols = map(int, raw_input().split())
    cels.append(cols)

for i in xrange(r):
    cels[i].append(sum([ j for j in cels[i]]))

sum_c=[]
for i in xrange(c):
    sum_c.append(sum([cels[n][i] for n in xrange(r)]))

cels.append(sum_c)
cels[r].append(sum(cels[r]))

for i in xrange(r+1):
    print " ".join(map(str,cels[i]))

Solution (Python):


r, c = map(int, raw_input().split())
mat = {}
for i in range(1, r+1):
    col = map(int, raw_input().split())
    for j in range(1, c+1):
        mat[(i, j)] = col[j-1]

for i in range(1, r+1):
    mat[(i, c+1)] = 0
    for j in range(1, c+1):
        mat[(i, c+1)] += mat[(i, j)]

for j in range(1, c+2):
    mat[(r+1, j)] = 0
    for i in range(1, r+1):
        mat[(r+1, j)] += mat[(i, j)]

for i in range(1, r+2):
    buf = ""
    for j in range(1, c+1):
        buf += str(mat[(i, j)]) +" "
    buf += str(mat[(i, c+1)])
    print buf

Solution (Python):
r, c = map(int, input().split())
A = []
for line in range(r):
    A.append(list(map(int, input().split())))

[A[i].append(sum(A[i])) for i in range(r)]

trans = []
for i in range(c+1):
    trans.append([A[j][i] for j in range(r)])
    trans[i].append(sum(trans[i]))

ret = []
for i in range(r+1):
    ret.append([trans[j][i] for j in range(c+1)])
    print(' '.join(map(str, ret[i])))

Solution (Python):
r,c = map(int,raw_input().split())
a = [map(int,raw_input().split()) for _ in range(r)]
b = [0 for _ in range(c)]

for i in range(r) :
    s = 0
    for j in range(c) :
        s = s + a[i][j]
        b[j] = b[j] + a[i][j]
    a[i].append(s)
    print ' '.join(map(str,a[i]))
b.append(sum(b))
print ' '.join(map(str,b))

Solution (Python):
r,c = map(int,raw_input().split())
a = [map(int,raw_input().split()) for _ in range(r)]
b = [0 for _ in range(c)]

for i in range(r) :
    for j in range(c) :
        b[j] = b[j] + a[i][j]
    a[i].append(sum(a[i]))
    print ' '.join(map(str,a[i]))
b.append(sum(b))
print ' '.join(map(str,b))

Solution (Python):
r, c = map(int, input().split())
sheet = [list(map(int, input().split())) for _ in range(r)]

for i in range(r):
    sheet[i].append(sum(sheet[i]))
new_row = []
for j in range(c+1):
    sum_col = 0
    for i in range(r):
        sum_col += sheet[i][j]
    new_row.append(sum_col)
sheet.append(new_row)

for i in range(r+1):
    print(' '.join(str(x) for x in sheet[i]))

Solution (Python):
import sys

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

c_sum = 0
sum_vector = [0 for i in xrange(c+1)]

for i in xrange(r):
	temp = map(int, raw_input().split())
	temp.append(sum(temp))
	for j in xrange(c):
		sum_vector[j] += temp[j]
		sys.stdout.write(str(temp[j])+" ")
	else:
		sum_vector[c] += temp[c]
		print temp[c]

for i in xrange(c):
	sys.stdout.write(str(sum_vector[i])+" ")
print sum_vector[c]

Solution (Python):
r, c = map(int, input().split())

data = []

for y in range(r):
    row = list(map(int, input().split()))
    data.append(row)

colsum = [0 for i in range(c + 1)]
for y in range(r):
    data[y].append(sum(data[y]))
    for x in range(c + 1):
        colsum[x] += data[y][x]

data.append(colsum)

for y in range(r + 1):
    print(*data[y])

Solution (Python):
r, c = map (int, input().split())

data = []

for y in range(r):
    row = list(map(int, input().split()))
    data.append(row)
    
colsum = [0 for i in range(c + 1)]
for y in range(r):
    data[y].append(sum(data[y]))
    for x in range(c + 1):
        colsum[x] += data[y][x]
    
data.append(colsum)

for y in range(r + 1):
    print(*data[y])

Solution (Python):

r, c = map(int, input().split())
spreadSheet = [[0 for i in range(c + 1)] for j in range(r + 1)]

for i in range(0, r):
    spreadSheet[i][:c] = map(int, input().split())
    

for i in range(0, r):
    for j in range(0, c):
        spreadSheet[i][c] += spreadSheet[i][j]

for i in range(0, c + 1):
    for j in range(0, r):
        spreadSheet[r][i] += spreadSheet[j][i]

for i in range(0, r + 1):
    for j in range(0, c + 1):
        print("{0}".format(spreadSheet[i][j]), end="")
        if j != c:
            print(" ",end="")
        else:
            print("")

Solution (Python):

row, cow = (int(i) for i in input().split())

array = []

for i in range(row):
    swap_list = input().split()
    array.append(swap_list)

row_sum = [0 for i in range(row)]
cow_sum = [0 for i in range(cow)]


for i in range(row):
    for j in range(cow):
        row_sum[i] += int(array[i][j])
        cow_sum[j] += int(array[i][j])

all_sum = sum(row_sum)


for i in range(row):
    
    print(" ".join(map(str,array[i][:])),end = "")
    print(" {0}".format(row_sum[i]))

print(" ".join(map(str,cow_sum)),end = "")
print(" {0}".format(all_sum))

Solution (Python):

























































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

data = []
csum = [0]*c

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

for i in range( r ):
	for j in range(c):
		print("{0} ".format(data[i][j]), end="")
		csum[j] +=data[i][j]
	print( sum(data[i]) )

for j in range(c):
	print("{0} ".format( csum[j] ), end="" )

print( sum(csum) )

Solution (Python):
r,c = map(int,input().split())
matrix = [list(map(int,input().split())) for i in range(r)]
for i in range(r):
    matrix[i].append(sum(matrix[i]))

row = []

for i in range (c+1):
    row_sum = 0
    for j in range(r):
        row_sum += matrix[j][i]
    row.append(row_sum)

matrix.append(row)
for i in range(r+1):
    for j in range(c+1):
        print(matrix[i][j],end="")
        if j != c:
            print(" ",end="")
        if j == c :
            print("")

Solution (Python):
r,c=map(int,input().split())
mat = [ list(map(int,input().split())) for _ in range(0,r) ]
for xs in mat: xs.append(sum(xs))
ll = [ sum([ xs[i] for xs in mat ]) for i in range (0,c+1) ]
for xs in mat: print(' '.join(map(str,xs)))
print(' '.join(map(str,ll)))

Solution (Python):
r,c = [int(x) for x in input().split()]
s_row = [0 for x in range(c+1)]
for i in range(r):
    row = [int(x) for x in input().split()]
    row.append(sum(row))
    print(" ".join([str(x) for x in row]))
    s_row = [x+y for x,y in zip(s_row,row)]
print(" ".join([str(x) for x in s_row]))

Solution (Python):
r,c=map(int,raw_input().split())
m=[]
ans=[]

for i in range(r):
	a=map(int,raw_input().split())
	m.append(a)
	sum=0
	for j in range(c):
		print(a[j]),
		sum+=a[j]
	print(sum)
total=0
for i in range(c):
	sum=0
	for j in range(r):
		sum+=m[j][i]
	print(sum),
	total+=sum
print(total)

Solution (Python):
import sys
import math

r, c = map(int, raw_input().split())
b = [0 for i in xrange(c+1)]

for i in xrange(r):
    a = map(int, raw_input().split())

    for j in xrange(c):
        b[j] += a[j]
        sys.stdout.write(str(a[j]) + " ")
    print sum(a)

    b[c] += sum(a)

for j in xrange(c+1):
    sys.stdout.write(str(b[j]))
    if j < c:
        sys.stdout.write(" ")
print

Solution (Python):
N = input().split()
r = int(N[0])
c = int(N[1])
d = [[0 for i in range(c)] for j in range(r)]
for i in range(r):
    data = input().split()
    for j in range(c):
        d[i][j] = int(data[j])
sum2 = [0 for i in range(c)]
for i in range(r):
    line = ''
    for j in range(c):
        line += str(d[i][j])
        line += ' '
        sum2[j] += d[i][j]
    line += str(sum(d[i]))
    print(line)
line2 = ''
for i in range(c):
    line2 += str(sum2[i])
    line2 += ' '
print('{0}{1}'.format(line2, sum(sum2)))

Solution (Python):
r,c = list(map(int,input().split()))
matrix = []
for i in range(r):
	matrix.append(list(map(int,input().split())))
	matrix[i].append(sum(matrix[i]))
add_list = []
for j in range(c+1):
	num = 0
	for k in range(r):
		num += matrix[k][j]
	add_list.append(num)
matrix.append(add_list)

for i in range(r+1):
	out = ''
	for m in matrix[i]:
		out += str(m) + ' '
	print(out[:-1])

Solution (Python):

r,c = [int(a) for a in input().split(' ')]
arr = []
Hsum=0
for x in range(r):
    i =[int(a) for a in input().split(' ')]
    i.append(sum(i))
    arr.append(i)
arr2=[]
for y in range(c+1):
    for z in range(r):
        Hsum += arr[z][y]
    arr2.append(Hsum)
    Hsum=0
arr.append(arr2)

for y in range(r+1):
    for z in range(c+1):
        if z == c:
            print(arr[y][z])
        else:
            print(arr[y][z],end = " ")

Solution (Python):
k=map(int,raw_input().split(" "))
vec=k[0]
col=k[1]

omatrix=[]
for i in range(vec+1):
	omatrix+=[[0]*(col+1)]

for i in range(vec):
	ipt=map(int,raw_input().split(" "))
	for j in range(col):
		omatrix[i][j]+=ipt[j]

for i in range(vec):
	omatrix[i][col]+=sum(omatrix[i])


for j in range(col+1):
	vsum=0
	for i in range(vec):
		vsum+=omatrix[i][j]
	omatrix[vec][j]+=vsum


for i in range(vec+1):
	for j in range(col+1):
		if j==col:
			print omatrix[i][j]
		else:
			print omatrix[i][j],

Solution (Python):
rc=list(map(int,input().split()))
a=[[0 for x in range(rc[1]+1)]for x in range(rc[0]+1)]
for x in range(rc[0]):
    code=list(map(int,input().split()))
    result=0
    for y in range(rc[1]):
        a[x][y]=code[y]
        result+=code[y]
    a[x][rc[1]]=result

for y in range(rc[1]+1):
    result=0
    for x in range(rc[0]):
        result+=a[x][y]
    a[rc[0]][y]=result

for x in range(rc[0]+1):
    string=""
    for y in range(rc[1]+1):
        string+=str(a[x][y])
        if (y!=rc[1]):
            string+=" "
    print(string)

Solution (Python):
r, c = map(int, input().split())
last_list = [0] * (c + 1)
for _ in range(r):
    n = 0
    x = input()
    sum_list = list(map(int, x.split()))
    for i in range(c):
        last_list[i] += sum_list[i]
        n += sum_list[i]
    print(x, n)

last_list[-1] = sum(last_list)
print(*last_list)

Solution (Python):
n,m = map(int, input().split())
c = []
d = [0 for i in range(m+1)]
for i in range(n):
    arr = input().split()
    sum = 0
    for j in range(m):
        sum += int(arr[j])
        d[j] += int(arr[j])
    arr.append(sum)
    d[m] += sum
    c.append(arr)
c.append(d)
for i in range(n+1):
    print(" ".join(map(str,c[i])))