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):
num1, num2 = map(int, input().split())

matrix = []

for i in range(num1):
    rows = list(map(int, input().split()))
    sum_rows = sum(rows)
    rows.append(sum_rows)
    matrix.append(rows)



row = []
for j in range(num2 + 1):
    sum_columns = 0
    for k in range(num1):
        sum_columns += matrix[k][j]
    row.append(sum_columns)

matrix.append(row)

for row in matrix:
    print(*row)

Solution (Python):
r,c = [int(i) for i in input().split()]
mat = [0 for i in range(r+1)]
for i in range(r):
    mat[i] = [int(i) for i in input().split()]
for i in range(r):
    mat[i].append(sum(mat[i]))
for j in range(c):
    mat[r] = [sum([mat[i][j] for i in range(r)]) for j in range(c+1)]
for i in range(r+1):
    print(*mat[i],sep=" ")

Solution (Python):
h,w=map(int, input().split())
a=[list(map(int, input().split())) for i in range(h)]
for i in range(h):
    a[i].append(sum(a[i]))
b=[]
for i in range(w+1):
    s=0
    for j in range(h):
        s+=a[j][i]
    b.append(s)
for i in a:print(*i)
print(*b)

Solution (Python):
import copy

def print_tab(tab):
    for row in tab:
        print(" ".join(map(str, row)))

def tab_calc(tab):
    for row in tab:
        row.append(sum(row))
    last_row = []
    for i in range(len(tab[0])):
        last_row.append(sum((r[i] for r in tab)))
    tab.append(last_row)

if __name__ == '__main__':
    r, c = [int(s) for s in input().split()]
    tab = []
    for _ in range(r):
        row = [int(s) for s in input().split()]
        assert len(row) == c
        tab.append(row)
    tab_calc(tab)
    print_tab(tab)

Solution (Python):
r,c = map(int,input().split())
spread = [list(map(int,input().split())) for i in range(r)]
for i in range(r):
    for j in range(c):
        print(spread[i][j],end=" ")
    print(sum(spread[i]))
for i in range(c):
    ans = 0
    for j in range(r):
        ans +=spread[j][i]
    print(ans,end=" ")
print(sum(map(sum,spread)))

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

r = int(r)
c1 = int(c)+1

table = [[0]*c1 for i in range(r+1)]


num = []

for a in range(r):
    
    num = input().split()

    for b in range(c1):

        if b != c1-1:
            table[a][b] = int(num[b])

        else:
            table[a][b] = sum(table[a])
    
    
for C in range(c1):
    total = 0
    for d in range(r):
        total += table[d][C]

    table[r][C] = total
        
for e in range(r+1):
    print(" ".join(map(str,table[e])))

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

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

for i in range(r) :
    L[i].append(sum(L[i]))
L2 = []
for i in range(c+1) :
    l2 = 0
    for j in range(r) :
        l2 += L[j][i]
    L2.append(l2)
    
L.append(L2)

for i in range(len(L)) :
    for j in range(len(L[i])) :
        L[i][j] = str(L[i][j])
for i in range(len(L)) :
    ans = " ".join(L[i])
    print(ans)

Solution (Python):




import math

r,c = map(int,input().split())
ans = [[0 for j in range(c+1)] for i in range(r+1)]
for i in range(r):
    sum = 0
    li = list(map(int,input().split()))
    for j in range(c):
        ans[i][j] = li[j]
        ans[r][j] += li[j]
        sum += li[j]
    ans[i][c] = sum
    ans[r][c] += sum

for i in range(0,r+1):
    print("%d"%(ans[i][0]),end="")
    for j in range(1,c+1):
        print(" %d"%(ans[i][j]),end = "")
    print()

Solution (Python):
import sys

r, c = map(int, sys.stdin.readline().strip().split())

col_sums = [0] * (c + 1)

for _ in range(r):
    line = sys.stdin.readline().strip()
    sum = 0
    for col, val in enumerate(map(int, line.split())):
        col_sums[col] += val
        sum += val
    col_sums[c] += sum
    print(f'{line} {sum}')

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

Solution (Python):
line = input()
num_lst = list(map(int, line.split()))
r = num_lst[0] 
c = num_lst[1] 

array = []
last_row = []
for i in range(r):
    line = input()
    lst = list(map(int, line.split()))
    
    num = sum(lst)
    lst.append(num)
    array.append(lst)


s = 0
for i in range(c+1):
    for j in range(r):
        s += array[j][i]
    last_row.append(s)
    s = 0

array.append(last_row)

for line in array:
    t = " ".join([str(n) for n in line])
    print(t)

Solution (Python):
r, c = map(int, input().split())
sheet = [[0 for x in range(c+1)] for y in range(r+1)]
for j in range(r):
    for i,x in enumerate(list(map(int, input().split()))):
        sheet[j][i] = x
        sheet[-1][i] += x
    sheet[j][-1] = sum(sheet[j][:-1])

sheet[-1][-1] = sum(sheet[-1][:-1])
print('\n'.join([' '.join(map(str,x)) for x in sheet]))

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

for i in range(r):
    l.append([int(j) for j in input().split()])
    l[i].append(sum(l[i]))

l.append([sum(k) for k in zip(*l)])

for v in l:
    print(*v)

Solution (Python):
def main():
	row, column = (int(a) for a in input().split())
	sheet = []
	for i in range(row):
		c = []
		for m in input().split():
			c.append(int(m))
		sheet.insert(i, c)
	for i in range(row):
		sheet[i].append(sum(sheet[i]))
		string = map(str, sheet[i])
		printColumn(sheet[i])
	l = []
	for m in range(column + 1):
		s = 0
		for r in range(row):
			s += sheet[r][m]
		l.append(s)
	printColumn(l)

def printColumn(list):
	string = map(str, list)
	print(' '.join(string))

main()

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

matrix = [list(map(int, input().split())) for _ in range(r)]

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

matrix.append(list(map(sum, zip(*matrix))))


for row in matrix:
    print(' '.join(str(e) for e in row))

Solution (Python):
rc=list(map(int,input().split()))
r=rc[0]
c=rc[1]

data=[]
data_retu=[]

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

data_retu=[[data[i][j] for i in range(r)] for j in range(c)]
retu_sum=[sum(data_retu[i]) for i in range(c)]
data.append(retu_sum)
for i in range(r+1):
    for j in range(c):
        print(str(data[i][j])+" ",end="")
    print(sum(data[i]))

Solution (Python):


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

for i in range(r):
  r_sum = 0
  for j in range(c):
    r_sum += table[i][j]
  table[i][c] = r_sum

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

for i in range(r+1):
  print(*table[i])

Solution (Python):
m=[[*map(int,input().split())]for _ in[0]*int(input().split()[0])]
for s in m:s.append(sum(s))or print(*s)
print(*map(sum,zip(*m)))

Solution (Python):
from functools import reduce

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

l = []
for i in range(0,r):
    t = list(map(int,input().split()))
    t.append(reduce(lambda a,b:a+b,t))
    l.append(t)

l2 = []
for i in list(map(list, zip(*l))):
    l2.append(reduce(lambda a,b:a+b,i))
l.append(l2)

for i in range(0,r+1):
    print (' '.join(map(str,l[i])))

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

l2=[]
for i in range(c):
    l1=[]
    for j in range(r):
        l1.append(l[j][i])
    l2.append(l1)
l3=[sum(l2[i]) for i in range(c)]

l.append(l3)
for i in range(r+1):
    l[i].append(sum(l[i]))
    print(" ".join(map(str,l[i])))

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

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

for i in range(r):
    s = sum(mat[i])
    mat[i].append(s)

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

mat[r][c] = (sum(mat[r]))

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

Solution (Python):
r, c = map(int, input().split())
sum_list = [0]*(c+1)
for i in range(r):
    r_i = list(map(int, input().split()))
    r_i.append(sum(r_i))
    sum_list = [a+b for a, b in zip(r_i, sum_list)]
    print(" ".join([str(k) for k in r_i]))
print(" ".join([str(k) for k in sum_list]))

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

col_sum = [0 for i in range(c)]
for i in range(r):
    b = input()
    list = b.split(' ')
    
    row_sum = 0
    sum_str = ''
    for j in range(c):
        row_sum += int(list[j])
        sum_str = sum_str + list[j] + ' '
        col_sum[j] += int(list[j])
    sum_str = sum_str + str(row_sum)
    print(sum_str)
    
sum = 0
sum_str = ''
for i in range(c):
    sum += col_sum[i]
    sum_str = sum_str + str(col_sum[i]) + ' '
sum_str = sum_str + str(sum)
print(sum_str)

Solution (Python):
(row, col) = map(int,input().split())
matrix=[0]*row
last = [0]*(col+1)
for r in range(row):
    line = list(map(int, input().split()))
    line.append(sum(line))
    print(*line, sep=" ")
    for j in range(col+1):
        last[j]+=line[j]
        

print(*last, sep=" ")

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

for i in range(r):
    num_list.append(list(map(int,input().split())))
    num_list[i].append(sum(num_list[i]))

col_sum_list = [0]*(c+1)
for i in range(c+1):
    for j in range(r):
        col_sum_list[i] += num_list[j][i]

num_list.append(col_sum_list)
for num in num_list:
    ans = [str(chrs) for chrs in num]
    print(' '.join(ans))

Solution (Python):
n,m=map(int,input().split())
A=[[0]*m for i in range(0,n)]
for i in range(0,n):
    A[i]=list(map(int,input().split()))
for i in range(0,n):
    sum=0
    for j in range(0,m):
        sum+=A[i][j]
    A[i].append(sum)
x=[0]*(m+1)
total=0
for j in range(0,m):
    sum=0
    for i in range(0,n):
        sum+=A[i][j]
    x[j]=sum
    total+=sum
x[m]=total
A.append(x)
for i in range(0,n+1):
    print(' '.join(map(str,A[i])))

Solution (Python):
row, col = map(int, input().split())
li = [list(map(int, input().split())) for i in range(row)]
last_row = []

for i in range(col):
    s = 0
    for j in range(row):
        s += li[j][i]
    last_row.append(s)

for r in range(row):
    print(" ".join(map(str, li[r])), sum(li[r]))
print(" ".join(map(str, last_row)), sum(last_row))

Solution (Python):
r, c = map(int, input().split())
t = [list(map(int, input().split())) for i in range(r)]
t.append([0]*c)
for i in t:
    i.append(sum(i))
for co in range(c+1):
    la = 0
    for ro in range(r):
        la += t[ro][co]
    t[r][co] = la
for l in t:
    print(*l)

Solution (Python):
import sys
 
BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001
 
 
 
H,W = map(int,input().split())
 
table = [[0]*(W+1) for i in range(H+1)]
 
for row in range(H):
    work = list(map(int,input().split())) 
    for col in range(W):
        table[row][col] = work[col]
 
 
for row in range(H):
    for col in range(0,W):
        table[row][W] += table[row][col]
 
 
for col in range(0,W+1):
    for row in range(0,H):
        table[H][col] += table[row][col]
 
 
for row in range(0,H+1):
    print("%d"%(table[row][0]),end="")
    for col in range(1,W+1):
        print(" %d"%(table[row][col]),end = "")
    print()

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

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

for c in cells:
    print(' '.join(map(str, c)))

Solution (Python):
num=list(map(int,input().split()))
sheet=[[0 for i in range(num[1])]for j in range(num[0]+1)]
for k in range(num[0]):
	sum=0
	kazu=list(map(int,input().split()))
	for l in range(num[1]):
		sheet[k][l]=kazu[l]
		sum+=kazu[l]
	sheet[k].append(sum)	
sum_ma=0
for m in range(num[1]):
	sum_b=0
	for n in range(num[0]):
		sum_b+=sheet[n][m]
	sheet[num[0]][m]=sum_b
	sum_ma+=sheet[num[0]][m]
sheet[num[0]].append(sum_ma)
for o in range(num[0]+1):
	for p in range(num[1]+1):
		print(sheet[o][p],end='')
		if p==(num[1]):
			print('')
		else:
			print(' ',end='')

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


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


ary.append([0]*c)


tmp = 0


for i2 in range(c):
    for i1 in range(r):
        tmp += ary[i1][i2]
    ary[i1+1][i2] = tmp
    tmp = 0




for i1 in range(r+1):
    for i2 in range(c):
        tmp += ary[i1][i2]
    ary[i1].append(tmp)
    tmp = 0

for i1 in range(r+1):
    print(" ".join(map(str, ary[i1])))

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

for i in range(0, r):
    temp = input().split()
    temp.append(0)
    A.append(temp)

col_sum = []
for i in range(0 , c + 1):
    col_sum.append(0)

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

A.append(col_sum)

for i in range(0, r + 1):
    for j in range(0, c + 1):
        if (j < c):
            print(A[i][j], end = ' ')
        else:
            print(A[i][j])

Solution (Python):





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

M=list()
for i in range(y):
    R=list(map(int,input().split()))
    M.append(R)

for m in M:
    m.append(sum(m))

T=list()
for k in range(x+1):
    sum=0
    for j in range(y):
        sum+=M[j][k]
        
    
    T.append(sum)

M.append(T)
    


for s in range(y+1):
    print(" ".join(list(map(str,M[s]))))

Solution (Python):
r,c=map(int,input().split())
ans=[[0 for i in range(c+1)] for j in range(r+1)]
for i in range(r):
	temp=list(map(int,input().split()))
	for j in range(c):
		ans[i][j]=temp[j]
	ans[i][c]=sum(temp)
for i in range(c+1):
	for j in range(r):
		ans[r][i]+=ans[j][i]
for i in range(r+1):
	for j in range(c+1):
		print(ans[i][j],end="")
		if j==c:
			print("")
		else:
			print(" ",end="")

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

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

for j in range(c+1):
    result = 0
    for i in range(r):
        result += table[i][j] 
    table[r].append(result)

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

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

array = []
for _ in range(r):
    x = list(map(int, input().split()))
    x.append(sum(x))
    array.append(x)

column = [list(ary) for ary in zip(*array)]
row = [sum(col) for col in column]
array.append(row)

for ary in array:
    print(' '.join(map(str, ary)))

Solution (Python):
import sys

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001



H,W = map(int,input().split())

table = [[0]*(W+1) for i in range(H+1)]

for row in range(H):
    work = list(map(int,input().split())) 
    for col in range(W):
        table[row][col] = work[col]


for row in range(H):
    for col in range(0,W):
        table[row][W] += table[row][col]


for col in range(0,W+1):
    for row in range(0,H):
        table[H][col] += table[row][col]


for row in range(0,H+1):
    print("%d"%(table[row][0]),end="")
    for col in range(1,W+1):
        print(" %d"%(table[row][col]),end = "")
    print()

Solution (Python):
a, b = map(int, input().split())
L = []
for i in range(a):
    A = list(map(int, input().split()))
    L.append(A)

for i in range(a):
    X = 0
    for k in range(b):
        X = X +L[i][k]
    L[i].append(X)

l = []
for k in range(b):
    X = 0
    for i in range(a):
        X = X +L[i][k]
    l.append(X)

Sum = sum(l)
for i in range(a):
    Sum = Sum + L[i][b]
l.append(Sum//2)

L.append(l)
for i in range(a+1):
    word = str(L[i][0])
    for k in range(1,b+1):
        if k < b+1:
            word = word +" "+str(L[i][k])
        else:
            word = word + str(L[i][k])
    print(word)

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

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

for i in range(r):
    temp = [int(x) for x in input().split()]
    for j in range(c+1):
        if not j == c:
            table[i][j] = temp[j]
            table[i][c] += temp[j]
        table[r][j] += table[i][j]

for i in range(r+1):
    for j in range(c+1):
        if j != c:
            print(table[i][j],end=' ')
        else:
            print(table[i][j])

Solution (Python):


if __name__=='__main__':
    r, c = map(int, raw_input().split())

    bottom = [0]*c
    for _ in range(r):
        s = raw_input()
        n = map(int, s.split())
        print '{} {}'.format(s, sum(n))
        for j in range(c):
            bottom[j] += n[j]

    print '{} {}'.format(' '.join(map(str, bottom)), sum(bottom))

Solution (Python):
r, c = list(map(int, input().split()))
t = [[0]*(c+1) for i in range(r+1)]
for i in range(r):
  a = list(map(int, input().split()))
  for j in range(c):
    t[i][j] = a[j]
    t[r][j] += a[j]
  t[i][c] = sum(a)
t[r][c] = sum(t[r])
for i in range(r+1):
  print(" ".join(map(str, t[i])))

Solution (Python):
r,c = map(int, tuple(input().split(' ')))
rc = [[int(s) for s in input().split(' ')] for _ in range(r)]
for row in rc: row.append(sum(row))
to = []
for col in range(c+1):
	su = 0
	for a in range(r): su += rc[a][col]
	to.append(su)
rc.append(to)
for row in rc: 
	ss = map(str, row)
	print(' '.join(ss))

Solution (Python):
arg1,arg2 = map(int, input().split())
data = []
data2 = []
tmp = []
for x in range(0,arg1):
    tmp = input().split()
    data.append([int(tmp[i]) for i in range(arg2)])
    data[x].append(sum(data[x]))
data.append([(sum(data[i][j] for i in range(arg1))) for j in range(arg2)])
data[arg1].append(sum(data[arg1][i] for i in range(arg2)))
for x in range(0,arg1+1):
    for y in range(0,arg2+1):
        if y != arg2:
            print(data[x][y],end=' ')
        else:
            print(data[x][y])