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):
table = []
r, c = map(int, input().strip().split())
[table.append(input().strip().split()) for i in range(r)]
table.append([])

for j in range(r + 1):
    for k in range(c + 1):
        if j == r and k != c:
            lastrow = sum(map(int, [table[x][k] for x in range(r)]))
            table[-1].append(lastrow)
        if k == c:
            print(sum(map(int, table[j])))
            break
        print(table[j][k],end=' ')

Solution (Python):
iranai = [int(i) for i in input().split()]
iru = [[int(i) for i in input().split()] for i in range(iranai[0])]
mikitani = []
son = []
num = 0
mikicount = 0
for i in range(iranai[0]):
    for i2 in range(iranai[1]):
        num += iru[i][i2]
    mikitani.append(num)
    num = 0

for i in range(iranai[1]):
    for i2 in range(iranai[0]):
        num += iru[i2][i]
    son.append(num)
    num = 0

for i in range(iranai[0]):
    for i2 in range(iranai[1]):
        print(iru[i][i2],end=" ")
        if i2 == iranai[1]-1:
            print(mikitani[i])
    mikicount += mikitani[i]
son.append(mikicount)

for i in range(len(son)):
    if i == len(son)-1:
        print(son[i])
    else:
        print(son[i],end=" ")

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

for temp in range(c + 1) : res1.append(0)

for tem in range(r) :
    tem1 = [temp for temp in input().split()]
    tem2 = list(map(int, tem1))
    print(' '.join(tem1) + ' ' + str(sum(tem2)))
    for mak in range(c) :
        res1[mak] = res1[mak] + tem2[mak]
    res1[c] += sum(tem2)

res2 = list(map(str, res1))
print(' '.join(res2))

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

matrix = []
for i in range(r):
    matrix.append([int(x) for x in input().split()])
    
for i in range(r):
    for j in range(c):
        print(matrix[i][j], '', end='')
    print(sum(matrix[i]))

crsum = 0
for j in range(c):
    csum = 0
    for i in range(r):
        csum += matrix[i][j]
    print(csum, '', end='')
    crsum += csum
print(crsum)

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

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

for rows in matrix:
    rows.append(sum(rows))

column_sum = []
for j in range(c+1):
    element_sum = 0
    for k in range(r):
        element_sum += matrix[k][j]
    column_sum.append(element_sum)

for p in matrix:
    print(" ".join(map(str,p)))
    
print(" ".join(map(str,column_sum)))

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

Solution (Python):


import sys
import os


def input_to_list():
    return list(map(int, input().split()))

H, W = input_to_list()
M = []
for i in range(H):
    lis = input_to_list()
    s = sum(lis)
    lis.append(s)
    M.append(lis)


last = []
for i in range(W + 1):
    s = 0
    for j in range(H):
        s += M[j][i]
    last.append(s)
M.append(last)


for i in range(H + 1):
    print(*M[i])

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

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

rsum = [0 for i in range(r)]
csum = [0 for i in range(c)]
for i in range(r):
    rsum[i] = sum(a[i])
    for j in range(c):
        csum[j] += a[i][j]
allsum = sum(rsum)

for i in range(r):
    for j in range(c):
        print(a[i][j], end = " ")
    print(rsum[i])
for i in range(c):
    print(csum[i], end = " ")
print(allsum)

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

Solution (Python):


 
import math
 
def func():
    try:
        (rows,columns) = [ int(item) for item in input().rstrip().split(" ") ]
        data = [ [ int(item) for item in input().rstrip().split(" ") ] for i in range(rows) ]
    except:
        return inputError()
        
    [item.append(sum(item)) for item in data]
    data.append( [sum( [ item[i] for item in data ] ) for i in range(columns+1) ])
    
    result=""
    for row in data:
        for item in row:
            result += str(item)+" "
        result=result.rstrip()
        result += "\n"
    print(result.rstrip())

def inputError():
    '''
    print("input Error")
    return -1
    '''
 
    print("input Error")
    return -1
     
func()

Solution (Python):


ss = []
sumf = []

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

for i in range(n):
    r = input()
    rSplit = r.split()
    for j in range(m):
        rSplit[j] = int(rSplit[j])
    ss.append(rSplit)

for i in range(m):
    summ = 0
    for j in range(n):
        summ += ss[j][i]
    sumf.append(summ)

ss.append(sumf)

for x in ss:
    summ = sum(x)
    x.append(summ)

for i in range(n + 1):
    for j in range(m + 1):
        ss[i][j] = str(ss[i][j])

for x in ss:
    print(" ".join(x))

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

for i in range(r):
    row = [int(n) for n in input().split(" ")]
    row.append(sum(row))
    print(" ".join( [ str(s) for s in row ] ) )
    s_row = [ x + y for x, y in zip(s_row, row)]
print(" ".join( [str(s1) for s1 in s_row] ))

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

Solution (Python):
r,c = map(int, input().split())
L=[]
for i in range(r):
 al = input()
 print(al, end="")
 AL = list(map(int, al.split()))
 S = sum(AL)
 print(" " + str(S))
 AL. append(S)
 L.append(AL)

for i in range(c):
 S = 0
 for j in range(r):
  S += L[j][i]
 print(str(S) + " ", end="")

S = 0
for j in range(r):
 S += L[j][-1]
print(str(S))

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

matrix = []
total_cols = [0 for i in range(c+1)]

for i in range(r):
    rows  = list(map(int, input().split(' ')));

    total = sum(rows)
    rows.append(total)

    total_cols = [ total_cols[i] + x for i, x in enumerate(rows) ]

    matrix.append(rows)

matrix.append(total_cols)

for row in matrix:
    print(' '.join([str(i) for i in row]))

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

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

Solution (Python):


r , c = [int(i) for i in input().rstrip().split()]
hyou =[]
r1=[]


for i in range(r):
    a = [int(i) for i in input().rstrip().split()]
    gou = sum(a)
    a.append(gou)
    print(" ".join(list(map(str ,a))))
    hyou.append(a)
    gou =0


for i in range(c+1):
    for j in range(r):
       gou += hyou[j][i]


    r1.append(gou)
    gou = 0



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

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

Solution (Python):


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

row_lists = list()
for _ in range(r):
    row_list = list(map(int, input().strip().split(" ")))
    row_list.append(sum(row_list))
    row_lists.append(row_list)

lastrow = [sum(i) for i in zip(*row_lists)]

row_lists.append(lastrow)

for r in row_lists:
    print(" ".join(map(str, r)))

Solution (Python):
r, c = map(int, raw_input().split())
matrix = []
sum_line = [0] * c
for i in range(r):
    line = map(int, raw_input().split())
    sum = 0
    for j in range(c):
        sum += line[j]
        sum_line[j] += line[j]
    line.append(sum)
    matrix.append(line)
    

sum = 0
for i in range(r):
    print " ".join(map(str, matrix[i]))
    sum += matrix[i][c]
sum_line.append(sum)
print " ".join(map(str, sum_line))

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

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

Solution (Python):

height, width = list(map(int, input().split(" ")))
answer = [[0 for _ in range(width + 1)] for _ in range(height + 1)]

for row in range(height):
    column = 0
    sumOfColumn = 0
    for value in list(map(int, input().split(" "))):
        answer[row][column] = value
        sumOfColumn += value
        column += 1
    answer[row][column] = sumOfColumn
    
for columnIndex in range(width + 1):
    sumOfRow = 0
    for rowIndex in range(height):
        value = answer[rowIndex][columnIndex]
        sumOfRow += value
    answer[height][columnIndex] = sumOfRow

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

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

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

Solution (Python):
r, c = map(int, input().split())
last = [0] * (c + 1)

for _ in range(r):
    row = list(map(int, input().split()))
    row += [sum(row)]

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

    for x in range(c + 1):
        last[x] += row[x]

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

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

A_sum = 0
for i in range(r):
    a_line = list(map(int, input().split(" ")))
    for j, a in enumerate(a_line):
        print("%d "%a, end="")
        A[i][j] = a
        A_sum += a
    print("%d"%sum(a_line))

A_T = list(map(list, zip(*A)))
for a_t in A_T:
    print ("%d "%(sum(a_t)), end="")

print (A_sum)

Solution (Python):
y,x=[int(i) for i in input().split()]
s=[[int(0)for i in range(x+1)]for j in range(y+1)]
for i in range(y):
    k=input().split()
    for j in range(x):
        s[i][j]=int(k[j])
for i in range(y):
    for j in range(x):
        s[i][x]=s[i][x]+s[i][j]
        s[y][j]=s[y][j]+s[i][j]
for i in range(y):
    s[y][x]=s[y][x]+s[i][x]
for i in range(y+1):
    for j in range(x):
        print(s[i][j],"",end="")
    print(s[i][x])

Solution (Python):
r,c = [int(s) for s in input().split(' ')]
data = [[int(s) for s in input().split(' ')] for i in range(r)]
[row.append(sum(row)) for row in data]
T = [[row[i] for row in data] for i in range(c+1)]
bottom = [sum(t) for t in T]
data += [bottom]
data_s = [' '.join([str(i) for i in row]) for row in data]
[print(row) for row in data_s]

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)
sums = tuple(sum(table[y][x] for y in range(r)) for x in range(c + 1))
print(*sums)

Solution (Python):


"""
??´?????????
"""
inp = input().strip().split(" ")
row = int(inp[0])
col = int(inp[1])

cells = [[0 for r in range(col+1)] for c in range(row+1)]
for r in range(row):
    inp = input().strip().split(" ")
    for c in range(col):
        cells[r][c] = int(inp[c])
        cells[row][c] += cells[r][c]
        cells[r][col] += cells[r][c]
    cells[row][col] += cells[r][col]

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

Solution (Python):
r,c = [eval(x) for x in input().split()]
table = [[eval(x) for x in input().split()] for _ in range(r)]
[table[i].append(sum(table[i])) for i in range(r)]
table.append([sum([table[i][j] for i in range(r)]) for j in range(c+1)])
[print(''.join([str(table[i][j]) + (' ' if j != c else '') for j in range(c+1)])) for i in range(r+1)]

Solution (Python):
n,m = map(int,(input().split()))
a = []
s = []
for i in range(m):
    s.append(0)

for i in range(n):
    k = [int(i) for i in input().split()]
    k.append(sum(k))
    a.append(k)
    for j in range(m):
        s[j] += k[j]

s.append(sum(s))
a.append(s)

for j in a:
    print(' '.join(map(str, j)))

Solution (Python):
rc=list(map(int,input().split()))
sheet=[list(map(int,input().split()))+[0] for i in range(rc[0])]
sheet.append([0 for i in range(rc[1]+1)])
for s in sheet:
    s[-1]=sum(s[0:-1])
for i in range(rc[1]+1):
    r=sum([n[i] for n in sheet[0:-1]])
    sheet[-1][i]=r
for sh in sheet:
    print(*sh)

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

for i in range(r):
  tblS = input()
  tbl[i] = list(map(int, tblS.split()))
  print(tblS, end = '')
  print(' ', sum(tbl[i]), sep = '')

  for j in range(c):
    cSum[j] += tbl[i][j]
  cSum[c] += sum(tbl[i])

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

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

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


for s in range(r):
    a[s] = [int(x) for x in input().split()]

for t in range(r):
    a_sum = sum(a[t])
    a[t].append(a_sum)

lastline = [0 for i in range(c+1)]
for t in range(r):
    for s in range(c+1):
        lastline[s] += a[t][s]
a.append(lastline)

for i in a:
    output = [str(j) for j in i]
    print(' '.join(output))

Solution (Python):
r, c = [int(i) for i in input().split()]
 
data = []
sum_row = [0] * (c + 1)
 
for ri in range(r):
    data.append([int(i) for i in input().split()])
    data[ri].append(sum(data[ri]))
    print(" ".join([str(d) for d in data[ri]]))
    for ci in range(c + 1):
        sum_row[ci] += data[ri][ci]
print(" ".join([str(s) for s in sum_row]))

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

for j in range(0, r):
    ls[j].append(sum(ls[j]))

for j in range(0, c+1):
    num = 0
    for k in range(0, r):
        num += ls[k][j]
    ls2.append(num)
ls.append(ls2)
for z in range(0, r+1):
    print(*ls[z])

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

for j in range(0, c+1):
    num = 0
    for k in range(0, r):
        num += ls[k][j]
    ls[r].append(num)
for z in range(0, r+1):
    print(*ls[z])

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

for j in range(0, r):
    ls[j].append(sum(ls[j]))

for j in range(0, c+1):
    num = 0
    for k in range(0, r):
        num += ls[k][j]
    ls2.append(num)
ls.append(ls2)
for z in range(0, r+1):
    print(*ls[z])

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

coltotal = [0 for i in range(c + 1)]
for a in range(r):
	array[a].append(sum(array[a]))
	for x in range(c + 1):
		coltotal[x] += array[a][x]

array.append(coltotal)

for a in range(r + 1):
	print(*array[a])

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

for row in range(r):
    bufarr = [int(x) for x in input().split()]
    bufarr.append(sum(bufarr))
    sheet.append(bufarr)

bufarr = []
for col in range(c + 1):
    bufarr.append(sum(sheet[row][col] for row in range(r)))
sheet.append(bufarr)

for row in range(r + 1):
    print(" ".join(str(sheet[row][col]) for col in range(c + 1)))

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

Solution (Python):
r, c = map(int, raw_input().split())
sheet = list()
for i in range(r):
    sheet.append(map(int, raw_input().split()))
for i in range(r):
    sheet[i].append(sum(sheet[i]))
for i in sheet:
    print(" ".join(str(j) for j in i))
temp = list()
for i in range(c):
    result = 0
    for j in range(r):
        result += sheet[j][i]
    temp.append(result)
temp.append(sum(temp))
print(" ".join(str(i) for i in temp))

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

for col in rc: print(*col)

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

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([sum(i) for i in zip(*matrix)])

for m in matrix:
    print(' '.join(str(e) for e in 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 m in matrix:
    print(' '.join(str(e) for e in m))

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

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,c=map(int,input().split())
a=[]
for i in range(r):
    a+=[list(map(int,input().split()))]
    a[i].append(sum(a[i]))
for i in range(len(a)):
    print(" ".join(list(map(str,a[i]))))
b=[]
t=[]
d=0
for i in range(c+1):
    for j in range(r):
        b+=[a[j][i]]
    t+=[sum(b)] 
    b=[]
print(" ".join(list(map(str,t))))

Solution (Python):


import sys, math, os


PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


n, m = [int(_) for _ in input().split()]
A = [[int(_) for _ in input().split()] for __ in range(n)]

for row in range(n):
    A[row].append(sum(A[row]))

for row in A:
    print(' '.join([str(r) for r in row]))

last = []
for col in range(m):
    last.append(sum([A[row][col] for row in range(n)]))
last.append(sum(last))
print(' '.join([str(l) for l in last]))

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):
    j = 0
    for line in input().split():
        a[i][j] = int(line)
        a[i][c] += a[i][j]
        j += 1
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(a[i][j])
        else:
            print(a[i][j], end=" ")

Solution (Python):
m,n=map(int,input().split())
hozon=[]
for i in range(n):
    hozon.append(0)
for _ in range(m):
    x = input()
    gyou = list(map(int,x.split()))
    print(x+" {0}".format(sum(gyou)))
    for num in range(n):
        hozon[num] += gyou[num]

print(" ".join(map(str,hozon))+" {0}".format(sum(hozon)))

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

Solution (Python):
y, x = map(int, input().split())
box = []
y_sum = [[]]

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

box_replace = list(map(list, zip(*box)))
[y_sum[0].append(sum(box_replace[i])) for i in range(x+1)]
box += y_sum

[print( " ".join(map(str, box[i])) ) for i in range(y+1)]

Solution (Python):
h, w = map(int, input().split())
sheet = []

for j in range(h):
	lista = list(map(int, input().split()))
	lista.append(sum(lista))
	sheet.append(lista)

lista = []

for i in range(w+1):
	sum = 0
	for j in range(h):
		sum += sheet[j][i]
	lista.append(sum)
	
sheet.append(lista)

for i in range(h+1):
	text = ""
	for j in range(w+1):
		text += str(sheet[i][j]) + " "
	print(text[:-1])

Solution (Python):
list = [input().split()]
r = int(list[0][0])
c = int(list[0][1])
i = 0
k = 0
sum2 = 0
list3 = [0] * c
while i < r:
    sum = 0
    list2 = [input().split()]
    for j in list2[0]:
        print(j, end=' '),
        sum += int(j)
    for k in range(c):
        list3[k] += int(list2[0][k])
    print(sum)
    sum2 += sum
    i += 1
for l in list3:
    print(l, end=' ')
print(sum2)

Solution (Python):
I = input().split()
r = int(I[0])
c = int(I[1])
Rsum = 0
Csum = (c + 1) * [0]

o = [[0 for i2 in range(c + 1)] for i1 in range(r + 1)]

for i in range(r + 1):
    if i < r:
        Irc = input().split()
    for j in range(c):
        if i < r:
            o[i][j] = int(Irc[j])
            Rsum += o[i][j]
            Csum[j] += o[i][j]
        else:
            o[i][j] += Csum[j]
            Rsum += o[i][j]
    o[i][c] = Rsum
    Csum[c] = o[i][c]
    Rsum = 0

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

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

list1 = []
for j in range(c+1):
    temp = 0
    for k in range(r):
        temp += data[k][j]
    list1.append(temp)

for l in data:
    print(*l)
print(*list1)

Solution (Python):
m, n = map(int, input().split())
sheet = [ list(map(int, input().split())) for i in range(m)]
for i in range(m):
    sheet[i].append(sum(sheet[i]))
    if i == 0:
        sheet.append(list(sheet[i]))
    else :
        for j in range(n):
            sheet[-1][j] += sheet[i][j]
sheet[-1][-1] = sum(sheet[-1]) - sheet[0][-1]
for tmp_list in sheet:
    print(" ".join(str(x) for x in tmp_list))

Solution (Python):
class Reader:
    def __init__(self, r, c):
        self.r = r
        self.c = c
        self.buf = [0 for _ in range(c+1)]

    def __call__(self, s):
        total = 0
        for i, v in enumerate(map(int, s.split())):
            self.buf[i] += v
            total += v
        self.buf[-1] += total
        return " ".join((s, str(total)))

    def __str__(self):
        return " ".join(map(str, self.buf))

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

for _ in range(r):
    print(reader(input()))
print(reader)

Solution (Python):
import sys
write=sys.stdout.write

number_of_numbers=[]
numbers=[]

number_of_numbers=input().split()
number_of_numbers[0]=int(number_of_numbers[0])
number_of_numbers[1]=int(number_of_numbers[1])



for i in range(0,number_of_numbers[0]):
    line_nunmbers=[]
    line_numbers=input().split()
    for j in range(0,len(line_numbers)):
        line_numbers[j]=int(line_numbers[j])
    numbers.append(line_numbers)

right_end_numbers=[]
under_end_numbers=[]

for i in range(0,len(numbers)):
    right_end_numbers.append(sum(numbers[i]))

for i in range(0,len(numbers[0])):
    under_end_number=0
    for j in range(0,len(numbers)):
        under_end_number+=numbers[j][i]
    under_end_numbers.append(under_end_number)

for i in range(0,len(numbers)):
    for j in range(0,len(numbers[0])):
        write(str(numbers[i][j]))
        write(' ')
    print(right_end_numbers[i])

for i in range(0,len(numbers[0])):
    write(str(under_end_numbers[i]))
    write(' ')

print(sum(under_end_numbers))

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

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

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

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

for i in range(r):
    r_total = sum(a[i])
    a[i].append(r_total)

c_total = []

for j in range(c+1):
    s = 0
    for k in range(r):
        s += a[k][j]
    c_total.append(s)

a.append(c_total)

for z in range(r+1):
    for w in range(c+1):
        print(a[z][w],end="")
        if w != c:
            print(" ",end="")
    print()

Solution (Python):
N, M=map(int, input().split())
s_row = [0 for x in range(M+1)]
for n in range(N):
 arr = list(map(int, input().split()))
 arr.append(sum(arr))
 s_row = [x+y for x,y in zip(s_row, arr)]
 print(" ".join([str(x) for x in arr]))
print(" ".join([str(x) for x in s_row]))

Solution (Python):
r, c = map(int, input().split())
array = [[0 for i in range(c+1)] for j in range(r+1)]
for i in range(r):
    for j, num in enumerate(input().split()):
        array[i][j] = int(num)
for i in range(r):
    array[i][c] = sum(array[i])
for i in range(c+1):
    for j in range(r):
        array[r][i] += array[j][i]
for i in range(r+1):
    for j in range(c+1):
        if j == c:
            print(array[i][j])
        else:
            print(array[i][j], end=' ')

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

Solution (Python):

r, c = [int(x) for x in input().split()]
q = []
sumr = [0]*c
tempr = 0
for i in range(r):
	col = [int(x) for x in input().split()]
	for j in range(c):
		sumr[j] += col[j]
	col.append(sum(col))
	q.append(col)
sumr.append(sum(sumr))
q.append(sumr)
for i in range(r+1):
	for j in range(c+1):
		print(q[i][j], end='')
		if (j != (c)):
			print(' ', end='')
	
	print()

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

mat = []
for i in range(row):
    line = map(int, raw_input().split())
    mat.append(line + [sum(line)])

row2 = [0 for i in range(col+1)]
for i in range(col+1):
    for j in range(row):
        row2[i] += mat[j][i]
mat.append(row2)
for i in mat:
    print " ".join(map(str,i))

Solution (Python):
(r, c) = map(int, input().split())
a = [[v for v in list(map(int, input().split()))] for r in range(r)]
add_c = [sum(l) for l in a]
for i in range(r):
    a[i].append(add_c[i])
add_r = [sum([a[i][j] for i in range(r)]) for j in range(c + 1)]
a.append(add_r)
for i in range(r + 1):
    print(' '.join(map(str, a[i])))

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

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

    for x in range(r):
        hoge = 0
        for y in range(c):
            hoge += A[x][y]
        A[x].append(hoge)

    B = []
    for x in range(c):
        hoge = 0
        for y in range(r):
            hoge += A[y][x]
        B.append(hoge)

    total = 0
    for b in B:
        total += b
    
    B.append(total)
    A.append(B)

    for x in range(len(A)):
        for y in range(len(A[x])):
            if y != len(A[x]) - 1:
                print(A[x][y], end = " ")
            else:
                print(A[x][y])

if __name__ == "__main__":
    main()

Solution (Python):
import copy
import sys

array_data = map(int, raw_input().split())
r = array_data[0]
c = array_data[1]

array = []
for i in range(r):
	partial_array = map(int, raw_input().split())
	array.append(partial_array)

ans = copy.deepcopy(array)
for i in range(r):
	ans[i].append(sum(array[i]))
ans.append([])
for j in range(c+1):
	wa = 0
	for i in range(r):
		wa += ans[i][j]
	ans[r].append(wa)

for i in range(len(ans)):
	for j in range(len(ans[i])):
		if j != len(ans[i])-1:
			sys.stdout.write("{:} ".format(ans[i][j]))
		else:
			print(ans[i][j])