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):
from itertools import chain

def main():
    inputs = []
    r, _ = map(int, input().split())
    for i in range(r):
        inputs.append(tuple(map(int, input().split())))

    row_sums = map(lambda row: sum(row), inputs)
    column_sums = map(lambda column: sum(column), map(lambda *x: x, *inputs))
    all_sum = sum(chain.from_iterable(inputs))

    for i, s in zip(inputs, row_sums):
        print(" ".join(map(lambda n: str(n), i)) + " {}".format(s))
    else:
        print(" ".join(map(lambda n: str(n), column_sums)) + " {}".format(all_sum))

if __name__ == "__main__":
    main()

Solution (Python):
def main():

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

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

    print((' '.join(map(str, total))) + ' ' + str(sum(total)))


main()

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

for i in range(r):
    l=list(map(int,input().split()))
    l.append(sum(l))
    print(' '.join(map(str,l)))
    s=[s[i]+l[i] for i in range(c+1)]
print(' '.join(map(str,s)))

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

for rc in range(r):
    in_row = [int(s) for s in input().split()]
    for cc, val in enumerate(in_row):
        rows[rc][cc] = val
        rows[rc][-1] += val
        rows[-1][cc] += val
        rows[-1][-1] += val
    
for row in rows:
    print(' '.join([str(i) for i in row]))

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

sumOfCols = [0] * c

for i in range(r):
    cols = list(map(int, input().split()))
    s = sum(cols)
    sumOfCols = [p + q for p,q in zip(sumOfCols, cols)]
    print(' '.join(map(str, cols)), end=' ')
    print(s)


s = sum(sumOfCols)
print(' '.join(map(str, sumOfCols)), end=' ')
print(s)

Solution (Python):


def main():

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

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

    rowSumList = []
    for i in range(c+1):
        rowSum = 0
        for j in range(r):
            rowSum += A[j][i]
        rowSumList.append(rowSum)

    A.append(rowSumList)

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

if __name__ == '__main__':
    main()

Solution (Python):
r,c=map(int,input().split())
q=range
b=[]
for i in q(r):
    w=[int(i) for i in input().split()]
    w.append(sum(w))
    b.append(w)
    print(*w)
l=[]
for i in q(c+1):
    x=0
    for a in b:x+=a[i]
    l.append(x)
print(*l)

Solution (Python):
import sys
s=input().split()
r=int(s[0]);c=int(s[1])
a=[[0 for j in range(c)] for i in range(r)]
b=[[0 for j in range(c+1)]for i in range(r+1)]
for i in range(r):
    t=input().split()
    for j in range(c):
        a[i][j]=int(t[j])
        b[i][j]=a[i][j]
        b[r][j]+=a[i][j]
        b[i][c]+=a[i][j]
        b[r][c]+=a[i][j]
for i in range(r+1):
    for j in range(c+1):
        sys.stdout.write("{0}".format(b[i][j]))
        if j==c:
            sys.stdout.write("\n")
        else:
            sys.stdout.write(" ")

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

    col_sums = [0] * c
    rows = [
        input()
        for n_r in range(r)
    ]

    for row in rows:
        row_sum = 0
        cols = map(int, row.split())
        for idx, col in enumerate(cols):
            col_sums[idx] += col
            row_sum += col

        print(row, end="")
        print("", row_sum)

    last_line = " ".join(map(str, col_sums))
    print(last_line, end="")
    print("", sum(col_sums))

if __name__ == "__main__":
    main()

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,input().split())
m=[[int(_) for _ in input().split()] for i in range(r)]
for i in range(r):m[i].append(sum(m[i]))
m.append([sum([m[i][j] for i in range(r)]) for j in range(c+1)])
for i in range(r+1):print(*m[i])

Solution (Python):
r,c=map(int,input().split())
m=[[int(_) for _ in input().split()] for i in range(r)]
for i in range(r):m[i].append(sum(m[i]))
m.append([sum([m[i][j] for i in range(r)]) for j in range(c+1)])
[print(*m[i]) for i in range(r+1)]

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

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

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

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

Solution (Python):
r,_=map(int, input().split())
a=[list(map(int, input().split())) for _ in range(r)]
[s.append(sum(s))or print(*s)for s in a]
print(*[sum(c)for c in zip(*a)])

Solution (Python):
n, p = [int(i) for i in input().split()]
a = [[] for _ in range(p+1)]

for _ in range(n):
    l = [int(i) for i in input().split()]
    l.append(sum(l))
    [a[i].append(j) for i, j in enumerate(l)]
    print(*l)

for h,i in enumerate(a):
    if h == len(a) - 1:
        print(sum(i))
    else:
        print(sum(i),end=' ')

Solution (Python):
l = input().split()
r = int(l[0])
c = int(l[1])

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

for i in range(len(a)):
    a[i].insert(c, sum(a[i]))

b = []
a.append(b)

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

for i in range(len(a)):
    print(*a[i])

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

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

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

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

b = [0 for _ in range(c+1)]
for j in range(c+1):
	for i in range(r):
		b[j] += a[i][j]
print " ".join(map(str, b))

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

a, btm = [], []

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

rotate = list(zip(*a))
for i in range(len(rotate)):
    btm.append(sum(rotate[i]))

print(*btm)

Solution (Python):

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

Solution (Python):

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

Solution (Python):
r, c = map(int, input().split())
MAP = [[] for _ in range(r)]
for i in range(r):
    MAP[i].extend(list(map(int, input().split())))
    MAP[i].append(sum(MAP[i]))
    print(*MAP[i])
li = []
for Z in zip(*MAP):
    li.append(sum(Z))
print(*li)

Solution (Python):
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):
    sumC = 0
    ar = input().split()
    for j in range(c):
        a[i][j] = int(ar[j])
        sumC += int(ar[j])
    a[i][c] = sumC

for k in range(c + 1):
    for m in range(r):
        a[r][k] += a[m][k]


for i in a:
    print(*i)

Solution (Python):
row, col = map(int, raw_input().split())

ret = []
for _ in range(row):
    _row = map(int, raw_input().split())
    _row += [sum(_row)]
    ret.append(_row)

for i in ret:
    print ' '.join(map(str, i))

ret_sum = []
for i in range(col+1):
    work = 0
    for j in ret:
        work += j[i]
    ret_sum.append(work)

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

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

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):

r,c = map(int,raw_input().split(" "))
mtrix = [map(int,raw_input().split()) for i in range(r)]
last_line = [0 for i in range(c+1)]
for i in range(r):
    mtrix[i].append(sum(mtrix[i]))
    last_line = [x+y for (x,y) in zip(mtrix[i],last_line)]
mtrix.extend([last_line])
for i in range(r+1):
    print " ".join(map(str,mtrix[i]))

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

Solution (Python):
n, m = map(int, input().split())
matrix = list()
for i in range(n):
    hoge = list()
    for elem in map(int, input().split()):
        hoge.append(elem)
    hoge.append(sum(hoge))
    matrix.append(hoge)
last_raw = list()
for j in range(m+1):
    hoge = 0
    for i in range(n):
        hoge += matrix[i][j]
    last_raw.append(hoge)
matrix.append(last_raw)
for i in range(n+1):
    print (' '.join(map(str, matrix[i])))

Solution (Python):
data = list(map(int, input().split()))
row = data[0]+1
column = data[1]+1
table = [[0 for j in range(column)] for i in range(row)]
for i in range(row-1):
    data_in = list(map(int, input().split()))
    for j in range(column-1):
        table[i][j] = data_in[j]
for i in range(row):
    sum_row = 0
    for j in range(column):
        sum_row += table[i][j]
    table[i][column-1] = sum_row
for i in range(column):
    sum_column = 0
    for j in range(row):
        sum_column += table[j][i]
    table[row-1][i] = sum_column
for i in range(row):
    for j in range(column):
        if(j==column-1):
            print(str(table[i][j]))
        else:
            print("{0}".format(table[i][j]), end=" ")

Solution (Python):
table = []
r,c = [int(x) for x in input().split( )]
for i in range(r):
    mini_table = [int(x) for x in input().split( )]
    table.append(mini_table)
for i in table:
    i.append(sum(i))
gyou = []
for i in range(c+1):
    counter = 0
    for j in range(r):
        counter += table[j][i]
    gyou.append(counter)
table.append(gyou)
for i in table:
    print(' '.join(str(j) for j in i))

Solution (Python):
a, b = map(int, input().split())
sheet = [list(map(int, input().split())) for i in range(a)]
for row in sheet:
    print(' '.join(map(str, row + [sum(row)])))
lastrow = [sum([j[i] for j in sheet]) for i in range(b)]
print(' '.join(map(str, lastrow + [sum(lastrow)])))

Solution (Python):
import sys
from functools import reduce

(row, column) = sys.stdin.readline().rstrip('\r\n').split(' ')
row = int(row)
column = int(column)

a = []

for ii in range(row):
  a.append([int(x) for x in input().rstrip('\r\n').split(' ')])

for ii in range(len(a)):
  for aa in a[ii]:
    print(str(aa) + ' ', end='')
  print(reduce(lambda sum, x: sum + x, a[ii]))

total = 0
for ii in range(column):
  sum = 0
  for jj in range(row):
    sum += a[jj][ii]
    total += a[jj][ii]
  print(str(sum) + ' ', end='')
print(total)

Solution (Python):
n, m = map(int, input().split())
mat = [[0 for x in range(m+1)] for y in range(n+1)]

for i in range(n):
    r = list(map(int,input().split()))
    for j in range(m):
        mat[i][j] = r[j]

for i in range(n):
    for j in range(m):
        mat[i][-1] += mat[i][j]
        mat[-1][j] += mat[i][j]
    mat[-1][-1] += mat[i][-1]

for i in range(n+1):
    print(*mat[i])

Solution (Python):
r,c=map(int,input().split())
s=[]
t=[]
for i in range(r):
    s.append(list(map(int,input().split())))
    s[i].append(sum(s[i]))
for j in range(c+1):
    u = 0
    for k in range(r):
        u += s[k][j]
    t.append(u)
    s.append(t)
for i in range(r+1):
    w = str(s[i][0])
    for j in range(1,c+1):
        w += " "+str(s[i][j])
    print(w)

Solution (Python):



import sys

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

for i in range(r):
    _line = list(map(int, sys.stdin.readline().split()))
    _line.append(sum(_line))
    matrix.append(_line)

_line = list()
for col in range(c+1):  
    val = 0
    for row in range(r):
        val += matrix[row][col]
    _line.append(val)

matrix.append(_line)
for _line in matrix:
    str_x = list(map(str, _line))
    print(' '.join(str_x))

Solution (Python):
(r,c) = tuple(map(int,input().split()))
graph = []
for i in range(r):
  lst = list(map(int,input().split()))
  lst.append(sum(lst))
  graph.append(lst)
lsf = []
for i in range(c + 1):
  s = 0
  for j in range(r):
    s += graph[j][i]
  lsf.append(s)
for l in graph:
  print(' '.join(map(str,l)))
print(' '.join(map(str,lsf)))

Solution (Python):
n,m=map(int,input().split())
a = []
for i in range(n):
    a.append(list(map(int, input().split())))
    a[i]+=[sum(a[i])]
    print(*a[i])
a=list(zip(*a[::-1]))
for i in range(m):print(sum(a[i]),end=' ')
print(sum(a[m]))

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

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

c_line_sum = []
for i in range(c): 
    t = []
    for j in range(r):
        t.append(x[j][i])
    c_line_sum.append(sum(t))

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

c_line_sum.append(sum(c_line_sum))
x.append(c_line_sum)

for m in x:
    print(*m)

Solution (Python):
from operator import add
from functools import reduce

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

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


















b = []

for j in range(c):
    a = [x[i][j] for i in range(len(x))]
    b.append(reduce(add,a))



x.append(b)

for i in range(len(x)):
    y = reduce(add,x[i])
    x[i].append(y)
    
    
z = [list(map(str,x[i])) for i in range(len(x))]




w = [' '.join(z[i]) for i in range(len(x))]

[print(w[i]) for i in range(len(w))]

Solution (Python):
def calc_sums(tbl):
    """calculate the total sum for each row and column

    >>> calc_sums([[1, 2], [2, 1]])
    [[1, 2, 3], [2, 1, 3], [3, 3, 6]]
    """
    new_tbl = [row + [0] for row in tbl] + [[0] * (len(tbl[0])+1)]

    for i, row in enumerate(tbl):
        for j, val in enumerate(row):
            new_tbl[i][-1] += val
            new_tbl[-1][j] += val

    new_tbl[-1][-1] = sum(new_tbl[-1][:-1])

    return new_tbl


def run():
    n, m = [int(v) for v in input().split()]
    table = []

    for i in range(n):
        table.append([int(v) for v in input().split()])

    for row in calc_sums(table):
        print(" ".join([str(i) for i in row]))


run()

Solution (Python):
r, c = map(int, input().split())
a = []
for i in range(r) :
    a.append(list(map(int, input().split())))
    a[i] += [sum(a[i])]
    print(*a[i], sep = ' ')
a = list(zip(*a))
for i in range(c) :
    print(sum(a[i]), end = ' ')
print(sum(a[c]))

Solution (Python):
r, c = map(int, input().split())
table = []
for i in range(r):
    table.append(list(map(int, input().split())))
for i in table:
    i.append(sum(i))
b = []
for i in range(c):
    x = 0
    for k in range(r):
        x += table[k][i]
    b.append(x)
b.append(sum(b))
for i in table:
    print(" ".join(map(str, i)))
print(" ".join(map(str, b)))

Solution (Python):
ans = ''
r, c = map(int, input().split(' '))
i = 0
L = [0] * (c + 1)
while i < r:
    a = list(map(int, input().split(' ')))
    atot = 0
    j = 0
    while j < c:
        atot += a[j]
        L[j] += a[j]
        j += 1
    ans += ' '.join(map(str, a)) + ' ' + str(atot) + '\n'
    L[c] += atot
    i += 1
if ans != '':
    ans += ' '.join(map(str, L))
    print(ans)

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

for i in range(r):
    args.append(list(map(int, input().split())))
    r_sum = 0
    for j in args[i]:
        r_sum += j
    args[i].append(r_sum)

l_row = []

for k in range(c + 1):
    c_sum = 0
    for l in range(r):
        c_sum += args[l][k]
    l_row.append(c_sum)        

for m in range(r):
    print(' '.join(map(str, args[m])))

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

Solution (Python):


r, c = (int(i) for i in input().split())

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

for i in range(r):
    s[i] = input().split()
    s[i].append(0)

for i in range(r):
    for j in range(c):
        s[i][j] = int(s[i][j])

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

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

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

Solution (Python):
r, c = map(int, input().split())
output = []
for _ in range(r):
  l = list(map(int, input().split()))
  s = sum(l)
  l.append(s)
  output.append(l)
final = []
for i in range(c):
  t = 0
  for j in range(r):
    t += output[j][i]
  final.append(t)
s = sum(final)
final.append(s)
output.append(final)
for i in output:
  print(*i)

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

Solution (Python):
import sys

l = []
for i in sys.stdin:
    l.append(i.split())

for i in range(0,len(l)):
    for j in range(0,len(l[i])):
        l[i][j] = int(l[i][j])

for i in range(1,len(l)):
    l[i].append(sum(l[i]))

result = []
for i in range(0,l[0][1]+1):
    count = 0
    for j in range(1,l[0][0]+1):
        count += l[j][i]
    result.append(count)
l.append(result)

for i in range(1,len(l)):
    for j in range(0,len(l[i])):
        print(l[i][j],end='')
        if j < len(l[i]) - 1:
            print(" ",end='')
    print()

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

mathlist_change = list(map(list, zip(*mathlist)))
mathlist_last = 0

for y in mathlist:
    for z in y:
        print('{a} '.format(a=z), end='')
    print(sum(y))

for i in mathlist_change:
    print('{b} '.format(b=sum(i)), end='')
    mathlist_last += sum(i)

print(mathlist_last)

Solution (Python):
r,c = map(int,input().split())
num=[]
r_sum =[]
for i in range(r):
    num.append([int(i) for i in input().split()])
for i in range(r):
    line_sum = 0
    for j in range(c):
        line_sum += num[i][j]
    num[i].append(line_sum)
for i in range(c+1):
    row_sum = 0
    for j in range(r):
        row_sum += num[j][i]
    r_sum.append(row_sum)
num.append(r_sum)
for i in range(len(num)):
    print(' '.join(str(i) for i in num[i]))

Solution (Python):
r,c = map(int,input().split())
num=[]
r_sum =[]
for i in range(r):
    num.append([int(i) for i in input().split()])
for i in range(r):
    line_sum = 0
    for j in range(c):
        line_sum += num[i][j]
    num[i].append(line_sum)
for i in range(c+1):
    row_sum = 0
    for j in range(r):
        row_sum += num[j][i]
    r_sum.append(row_sum)
num.append(r_sum)
for i in range(len(num)):
    print(' '.join(str(i) for i in num[i]))

Solution (Python):
n,m=map(int,input().split())
a = []
for i in range(n):
    a.append(list(map(int, input().split())))
    a[i]+=[sum(a[i])]
    print(*a[i])
a=list(zip(*a[::-1]))
for i in range(m):print(sum(a[i]),end=' ')
print(sum(a[m]))

Solution (Python):


r, c = list(map(int, input().split()))
a = []
line_sum = 0

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

for i in range(r):
    for j in range(c + 1):
        if j == c:
            print('{0}'.format(sum(a[i])))
            line_sum += sum(a[i])
        else:
            print('{0} '.format(a[i][j]), end='')

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

    print('{0} '.format(column_sum), end='')

print(line_sum)

Solution (Python):
import functools, operator
r,c = tuple(int(n) for n in input().split())
A = [[int(a) for a in input().split()] for i in range(r)]
for a in A:
    a.append(sum(a))
R = [functools.reduce(operator.add, x) for x in zip(*A)]
A.append(R)
for j in A:
    print(" ".join(map(str,j)))

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

Solution (Python):
r, c = map(int, input().split())
matrix = [[0 for i in range(c+1)] for j in range(r+1)]
for i in range(r):
    a = list(map(int, input().split()))
    for j in range(c):
        matrix[i][j] = a[j]
    matrix[i][c] = sum(a)
for l in range(c+1):
    for m in range(r):
        matrix[r][l] += matrix[m][l]

for cv in range(r+1):
    for fv in range(c+1):
        print(matrix[cv][fv], end='')
        if fv != c:
            print(" ", end='')
    print()

Solution (Python):



r, c = [int(x) for x in input().split(' ')]

sheet = [[int(x) for x in input().split(' ')] for i in range(r)]
for i in range(r):
    sheet[i].append(0)
sheet.append([0 for i in range(c+1)])

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

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

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

Solution (Python):
n,m = map(int,input().split())
matrix =[[0 for i in range(m+1)] for j in range(n+1)]

for i in range(n):
    a = list(map(int,input().split()))
    for j in range(m):
        matrix[i][j] = a[j]
        matrix[i][m] += a[j]
        matrix[n][j] += a[j]
        matrix[n][m] += a[j]

for i in range(0,n+1):
    print(' '.join(str(x) for x in matrix[i]))

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

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

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

for i in range(r):
    print(" ".join(list(map(str, a[i]))))

csum = []
for j in range(c+1):
    su = 0
    for i in range(r):
        su += a[i][j]
    csum.append(su)

print(" ".join(list(map(str, csum))))

Solution (Python):
size = [int(i) for i in input().split()]
sheet =[ [0 for i in range(size[1])] for j in range(size[0])]
for gyou in range(size[0]):
    x = [int(i) for i in input().split()]
    sheet[gyou] = x
    sheet[gyou].append(sum(sheet[gyou]))

for gyou in range(size[0]):
    for retsu in range(size[1]+1):
        print(sheet[gyou][retsu],end="")
        if retsu != size[1]:
            print(" ",end="")
    print("")
for retsu in range(size[1]+1):
    sum2 = 0
    for gyou in range(size[0]):
        sum2 += sheet[gyou][retsu]
    print(sum2, end="")
    if retsu != size[1]:
        print(" ",end="")
print("")

Solution (Python):
l=input().split()
r=int(l[0])
c=int(l[1])


i=0
b=[]
while i<r:
    a=input().split()
    q=0
    su=0
    while q<c:
        b.append(int(a[q]))
        su+=int(a[q])
        q=q+1
    b.append(su)
    i=i+1





x=0
while x<c+1:
    d=0
    z=0
    while z<r:
        d+=b[x+z*(c+1)]
        z+=1
    b.append(d)
    x=x+1




C=[]
y=0
while y<r+1:
    z=0
    Ans=str(b[y*(c+1)+z])
    while z<c:
        z+=1
        Ans=Ans+" "+str(b[y*(c+1)+z])
    C.append(Ans)
    y+=1

for k in C:
    print(k)

Solution (Python):
r,c=map(int,input().split())
w=[0]*c
q=[]
for i in range(r):
    b=input().split()
    e=[int(s) for s in b]
    q.append(e)
for i in range(r):
    for j in range(c):
        w[j]+=q[i][j]
        print(q[i][j],end=' ')
    print(sum(q[i]))
for j in range(c):
    print(w[j],end=' ')
print(sum(w))

Solution (Python):
r, c = map(int, input().split())
matrix = [[int(n) for n in input().split()] for row in range(r)]

for i, row in enumerate(matrix):
    matrix[i].append(sum(row))

end = [0 for _ in range(c+1)]
for i in range(c+1):
    for row in matrix:
        end[i] += row[i]

matrix.append(end)

for row in matrix:
    row = map(str, row)
    print(" ".join(row))

Solution (Python):
r, c = map(int, input().split())
last = [0]*(c+1)
for i in range(r):
    columns = list(map(int, input().split()))
    for j in range(c):
        last[j] += columns[j]
    total = sum(columns)
    last[c] += total
    columns.append(total)
    print(' '.join(map(str, columns)))
print(' '.join(map(str,last)))

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

Solution (Python):
def data_assignment():
    for i in range(r):
        for j, e in enumerate(map(int, input().split())):
            table[i][j] = e

def total_rows():
    for i, line in enumerate(table):
        table[i][c] = sum(line[:c])


def total_columns():
    for i, line in enumerate(table):
        if i == r:
            break
        for j, element in enumerate(line):
            table[r][j] += element


def table_print():
    for line in table:
        for i, element in enumerate(line):
            if i == c:
                print(element)
            else:
                print(element, end=' ')


r, c = map(int, input().split())
table = [[0 for i in range(c+1)] for j in range(r+1)]
data_assignment()
total_rows()
total_columns()
table_print()

Solution (Python):


def numinput():
    a = input().split()
    for i in range(len(a)):
        a[i] = int(a[i])
    return a

def main():
    a = numinput()
    r = a[0]; c = a[1]
    M = []
    for i in range(r):
        M.append(numinput())  
        sum = 0
        for j in range(c): sum += M[i][j]
        M[i].append(sum)
    list = []
    for j in range(c + 1):
        sum = 0
        for i in range(r): sum += M[i][j]
        list.append(sum)
    M.append(list)

    for i in range(r + 1):
        _str_ = ""
        for j in range(c):
            _str_ += str(M[i][j]) + " "
        _str_ += str(M[i][c])
        print(_str_)

if __name__ == "__main__":
    main()

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

Solution (Python):
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):
    for j, x in enumerate(input().split()):
        a[i][j] = int(x)


for i, line in enumerate(a):
    a[i][c] = sum(line)


a = list(zip(*a))


for i, line in enumerate(a):
    a[i] = list(line)
    a[i][r] = sum(a[i])


a = list(zip(*a))

for line in a:
    for i, x in enumerate(line):
        if i == c:
            print(x)
        else:
            print(x, end=" ")

Solution (Python):
import sys

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

matrix = [[0 for x in range(m + 1)] for y in range(n + 1)]

for i in range(n):
	arr = list(map(int, input().split()))
	for j in range(m):
		matrix[i][j] = arr[j]
		
for i in range(n):
	sum = 0
	for j in range(m):
		sum += matrix[i][j]
	matrix[i][m] = sum

for i in range(m + 1):
	sum = 0
	for j in range(n):
		sum += matrix[j][i]
	matrix[n][i] = sum
	
for i in range(n + 1):
	for j in range(m + 1):
		sys.stdout.write(str(matrix[i][j]))
		if j != m:
			sys.stdout.write(' ')
	print()

Solution (Python):
r, c = map(int, input().split())
table = []
for _ in range(r):
    table.append(list(map(int, input().split())))
for row in range(r):
    print(' '.join(map(str, table[row])), sum(table[row]), sep=' ')

col_sum = [sum([table[row][col] for row in range(r)]) for col in range(c)]
print(' '.join(map(str, col_sum)), sum(col_sum), sep=' ')

Solution (Python):
r,c=map(int,raw_input().split())
l=[]
for i in range(r):l.append(map(int,raw_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]))