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 = map(int, input().split())
sheet = []

for i in range(r):
    num = list(map(int, input().split()))
    sheet.append(num)
    
for i in sheet:
    last = sum(i)
    i.append(last)
    
inner = []
x = 0

for i in range(c + 1):
    for j in range(r):
        x += sheet[j][i]
      
    inner.append(x)
    
    x =0
    
   
    
sheet.append(inner)

for i in sheet:
    print(" ".join(map(str, i)))

Solution (Python):
r,c = map(int, input().split())
a = []
for i in range(r):
    a.append(list(map(int, input().split())))
for i in range(r+1):
    if i != r:
        for j in range(c+1):
            if j != c:
                print(f'{a[i][j]} ', end='')
            else:
                print(sum(a[i]))
    else:
        for j in range(c+1):
            if j != c:
                print(f'{sum([b[j] for b in a])} ', end='')
            else:
                print(sum(map(sum, a)))

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

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

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):
r, c = map(int, input().split())
data = [list(map(int, input().split())) for _ in [0]*r]

[print(*data[i], sum(data[i])) for i in range(r)]
[print(sum([row[j] for row in data]), end = ' ') for j in range(c)]

s = 0
for k in range(r):
    s += sum(data[k])
print(s)

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

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

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

li = [list(map(int,input().split())) + [0] for _ in range(r)]
li = li+[[0]*(c+1)]

for i in range(c):
    tmp = 0
    for j in range(r+1):
        if j == r:
            li[j][i] = tmp
        tmp += li[j][i]

for i in range(r+1):
    li[i][c] = sum(li[i][:c])

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

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

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

for i in range(r):
    j = -1
    for strval in input().split():
        j += 1
        val = int(strval)
        data[i][j] = val
        sumr[i] += val
        sumc[j] += val

for i in range(r):
    for j in range(c):
        print(str(data[i][j]) + " ", end="")
    print(str(sumr[i]))

sumtotal = 0

for i in range(c):
    print(str(sumc[i]) + " ", end="")
    sumtotal += sumc[i]

print(sumtotal)

Solution (Python):
r,c=map(int,input().split())
l=[]
for i in range(r):
    l.append([int(x) for x in input().split()])
    l[i].append(sum(l[i]))
l.append([0 for x in range(c+1)])
for m in range(r):
    for n in range(c+1):
        l[-1][n]+=l[m][n]
for u in l:
    print(" ".join(map(str,u)))

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

table = []

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

lastrow=[0 for _ in range(c+1)]
for row in table:
    for i,cell in enumerate(row):
        lastrow[i]+=cell

table.append(lastrow)

for row in table:
    print(*row)

Solution (Python):
s = input().rstrip().split(' ')
h = int(s[0])
w = int(s[1])
lst = [[0] * w for i in range(h)]
for i in range(h):
  t = input().rstrip().split(' ')
  for j in range(w):
    lst[i][j] = int(t[j])
anssum = [0] * (w+1)
for k in range(h):
  ans = ""
  for l in range(w):
    ans += str(lst[k][l]) + " "
    anssum[l] += int(lst[k][l])
  ans += str(sum(lst[k]))
  anssum[w] += int(sum(lst[k]))
  print(ans)
ansbottom = ""
for m in range(w):
  ansbottom += str(anssum[m]) + " "
ansbottom += str(anssum[w])
print(ansbottom)

Solution (Python):
import math
import copy
from copy import deepcopy
import sys
import fractions

from functools import reduce

import decimal
import heapq
import collections
import itertools
sys.setrecursionlimit(100001)






def getInputInt():
    inputNum = int(input())
    return inputNum


def getInputListInt():
    outputData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData


def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList


def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList



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

d = getSomeInputListInt(r)
d.append([])

for i in range(c):
    tmp = 0
    for j in range(r):
        tmp += d[j][i]
    d[r].append(tmp)

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

d[r][c] = sum(d[r][0:c])

for tmp_d in d:
    print(*tmp_d, sep=" ")

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

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

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

a.append(b)

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

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

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

for data in dataset:
    data.append(sum(data))

total_data = []
for row in range(c+1):
    total_data.append(sum([data[row] for data in dataset]))
dataset.append(total_data)

for data in dataset:
    print(" ".join(map(str, data)))

Solution (Python):
r, c = map(int, input().split())
A = []
B = []
C = []
for i in range(r):
    A.append(list(map(int, input().split())))
for i in range(r):
    b = [str(j) for j in A[i]]
    B.append(b)
    B[i].append(sum(A[i]))
for i in range(r):
    print(*B[i])

for i in range(c):
    S = 0
    for j in range(r):
        S += A[j][i]
    C.append(S)
C.append(sum(C))
print(*C)

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

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

t.append(s)

for i in range(r+1):
    print(" ".join(map(str, t[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())

table =[[0]*(C+1) for i in range(R+1)]

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

for row in range(R):
    for col in range(C):
        table[row][C] += table[row][col]

for col in range(C+1):
    for row in range(R):
        table[R][col] += table[row][col]

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

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

Solution (Python):
R, C = map(int, input().split())
grid = [list(map(int, input().split())) for _ in range(R)]
for i in range(R):
    grid[i].append(sum(grid[i]))

total_RC = []
for c in range(C):
    sum_C = 0
    for r in range(R):
        sum_C += grid[r][c]
    total_RC.append(sum_C)
total_RC.append(sum(total_RC))
grid.append(total_RC)
[print(*g) for g in grid]

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

ans = [[i[j] for i in ss] for j in range(c+1)]
ss.append([sum(i) for i in ans])
print("\n".join(" ".join(map(str, i)) for i in ss))

Solution (Python):
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(W):
        table[row][W] += table[row][col]

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

for row in range(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())
lst=[list(map(int,input().split()))for i in range(r)]
all_lst=[0 for i in range(c+1)]
for mini_lst in lst:
    for i in range(c):
        all_lst[i]+=mini_lst[i]
    mini_sum=sum(mini_lst)
    all_lst[-1]+=mini_sum
    mini_lst.append(mini_sum)
    print(' '.join(map(str,mini_lst)))
print(' '.join(map(str,all_lst)))

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

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

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

for i in t:
    print(*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):
  work = list(map(int, input().split()))
  for j in range(c):
    table[i][j] = work[j]

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

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

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

Solution (Python):
r,c = map(int,input().split())
table = list()
last_t = list()
for i in range(r):
    a = list(map(int,input().split()))
    table.append(a)

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

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

for i in range(r+1):
    for j in range(c+1):
        if j >=1 and j<c:
            print(" "+str(table[i][j]),end="")
        elif j == c:
            print(" "+str(table[i][j]))
        else:
            print(table[i][j],end="")

Solution (Python):
r, c = map(int, input().split())
row = []
for i in range(r):
  row.append(input().split())
  row[i] = [int(n) for n in row[i]]
  rowsum = 0
  for j in range(c):
    rowsum += row[i][j]
    
  row[i].append(rowsum)
  






calsum = []
for i in range(r):
  if calsum == []:
    for k in range(c+1):
      calsum.append(0)
  for j in range(c+1):
      calsum[j] += row[i][j]







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

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

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

col_sum_list = [0 for i in range(len(table[0]))]
for line in table:
    line_sum = 0
    for i in range(len(line)):
        line_sum += line[i]
        col_sum_list[i] += line[i]
    line.append(line_sum)
col_sum_list.append(sum(col_sum_list))

for line in table:
    line = [str(i) for i in line]
    print(' '.join(line))

col_sum_list = [str(i) for i in col_sum_list]
print(' '.join(col_sum_list))

Solution (Python):
n, m = map(int, input().split())
data = [list(map(int, input().split())) for i in range(n)]
count = 0
side = [0] * (m+1)
for i in data:
  total = 0
  for j in i:
    total += j
  data[count].append(total)
  count += 1
for k in data:
  side = [x + y for (x, y) in zip(side, k)]
data.append(side)

for i in data:
  ans = " ".join(map(str, i))
  print(ans)

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

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

na = []
for i in range(c + 1):
    na.append(sum([j[i] for j in a]))
a.append(na)

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

Solution (Python):

def outputTable (table):
  r = len(table)
  c = len(table[0])
  for i in range(r):
    for j in range(c-1):
      print(str(table[i][j])+' ',end='')
    print(str(table[i][j+1]))


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

ans_r = r+1
ans_c = c+1

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

for i in range(ans_c):
  table[r][i] = sum([row[i] for row in table[:][:r]])
outputTable(table)

Solution (Python):
spreadsheet = []
total = 0
r, c = list(map(int, input().split()))
for i in range(r):
    listTmp = []
    listTmp = list(map(int, input().split()))
    spreadsheet.append(listTmp)
for j in range(r):
    for k in range(c):
        print(spreadsheet[j][k], end=" ")
    print(sum(spreadsheet[j]))
for m in range(c):
    cTotal = 0
    for n in range(r):
        cTotal += spreadsheet[n][m]
    print(cTotal, end=" ")
    total += cTotal
print(total)

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[r][j] += l[j]
    A[i][c] = sum(l)
    A[r][c] += sum(l)

for i in range(r + 1):
    for j in range(c):
        print(A[i][j], "", end = "")
    print(A[i][c])

Solution (Python):
a,b=map(int,input().split())
bottom_list=[]

y=list(map(int,input().split()))
y.append(sum(y))
print(" ".join(list(map(str,y))))
for i in range(b):
  bottom_list.append(y[i])

for h in range (a-1):
  y=list(map(int,input().split()))
  y.append(sum(y))
  print(" ".join(list(map(str,y))))
  for i in range(b):
    bottom_list[i]+=y[i]
  
bottom_list.append(sum(bottom_list))
print(" ".join(list(map(str,bottom_list))))

Solution (Python):
import sys
input = sys.stdin.readline

def main():
    r, c = map(int, input().split())
    tab = [list(map(int, input().split())) for i in range(r)]

    col_sums = [0] * c
    
    for i in range(r):
        tmp = 0
        for j in range(c):
            tmp += tab[i][j]
            col_sums[j] += tab[i][j]
            print(tab[i][j], end=" ")
        print(tmp)
    
    tmp = 0
    for i in range(c):
        tmp += col_sums[i]
        print(col_sums[i], end=" ")
    print(tmp)


if __name__ == "__main__":
    main()

Solution (Python):
import sys

def sa(Type= int):
  return [Type(x) for x in input().split()]

def solve():
  r, c = sa()

  mat = []

  for _ in range(r):
    arr = sa()
    arr.append(sum(arr))

    mat.append(arr)

  arr = []
  for i in range(c+1):
    arr.append(sum([mat[x][i] for x in range(r)]))

  mat.append(arr)

  print('\n'.join([' '.join([str(s) for s in x]) for x in mat]))

if __name__ == '__main__':
  
  solve()

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]))
    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):
H,W = map(int,input().split())

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

for i in range(H):
    for j in range(W):
        print(a[i][j],end=' ')
    print(sum(a[i]))

for i in range(W):
    print(sum([row[i] for row in a]),end=' ')
print(sum(map(sum, a)))

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):
    a = list(map(int, input().split()))
    for j in range(c):
        table[i][j] = a[j]
for i in range(r):
    for j in range(c):
        table[i][-1] += table[i][j]
        table[-1][j] += table[i][j]
    table[-1][-1] += table[i][-1]
for i in range(r+1):
    for j in range(c+1):
        if j > 0:
            print(" ", end="")
        print(table[i][j], end="")
    print()

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

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

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

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

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

Solution (Python):
a,b=map(int,input().split())

table=[[0]*b for i in range(a)]

for j in range(a):
    l=list(map(int,input().split()))
    for k in range(b):
        table[j][k]=l[k]

for j in range(a):
    sum1=0
    for k in range(b):
        print("%d " %(table[j][k]), end="")
        sum1+=table[j][k]
    print("%d" %(sum1))

sum3=0
for k in range(b):
    sum2=0
    for j in range(a):
        sum2+=table[j][k]
    sum3+=sum2
    print("%d " %(sum2), end="")
print(sum3)

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):
    lis = list(map(int, input().split()))
    for j in range(c):
        matrix[i][j] = lis[j]

for i in range(r):
    sum = 0
    for j in range(c):
        sum += matrix[i][j]
    matrix[i][c] = sum

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

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

Solution (Python):
def main(r, c, inputs):
    csum = [0]*(c+1)
    for input in inputs:
        input.append(sum(input))
        for i in range(len(input)):
            csum[i] += input[i]
    inputs.append(csum)
    row = [" ".join(map(str, i)) for i in inputs]
    return '\n'.join(map(str, row))


if __name__ == "__main__":
    r, c = map(int, input().split())
    inputs = [list(map(int, input().split())) for i in range(r)]
    print(main(r, c, inputs))

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

list_2d = []
for i in range(r):
    list_1d = [int(x) for x in input().split()]
    list_2d.append(list_1d)
    
for i in range(r):
    sum_r = sum(list_2d[i])
    list_2d[i].append(sum_r)

list_2d_last = []
for i in range(c):
    sum_c = 0
    for k in range(r):
        sum_c += list_2d[k][i]
    list_2d_last.append(sum_c)
    
list_2d.append(list_2d_last)
list_2d[r].append(sum(list_2d[r]))

for a in list_2d:
    print(*a)

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

for i in range(r):
    sc = input().split()
    for j in range(c):
        a[i][j] = int(sc[j])
for i in range(r):
    for j in range(c):
        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 == 0:
            print(a[i][j], end = '')
        else:
            print(' {}'.format(a[i][j]), end = '')
    print('')

Solution (Python):
r,c = map(int,input().split())
sheet = [list(map(int,input().split())) for i in range(r)]
for j in range(r):
         sheet[j].append(sum(sheet[j]))
newrow = []
for k in range(c+1):
         tmp = 0
         for l in range(r):
                  tmp  += sheet[l][k]
         newrow.append(tmp)
sheet.append(newrow)
for m in range(r+1):
         print(*sheet[m])

Solution (Python):
import sys
import itertools



read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

r, c = map(int, readline().split())
A = []
for _ in range(r):
    x = [int(i) for i in readline().split()]
    x.append(sum(x))
    A.append(x)

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

for i in A:
    print(*i)

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

Solution (Python):


r, c = list(map(int, input().split()))
A = [[0 for _ in range(c + 1)] for _ in range(r + 1)]

total = 0
for row in range(r):
    Arow = list(map(int, input().split()))
    for col in range(c):
        total += Arow[col]
        A[row][col] = Arow[col]
        A[r][col] += Arow[col]
        A[row][c] += Arow[col]
A[r][c] = total

for row in range(r + 1):
    for col in range(c + 1):
        if col < c:
            print("{} ".format(A[row][col]), end="")
        else:
            print(A[row][col], end="")
    print()

Solution (Python):
d = {}
r, c = [int(i) for i in input().split()]
a = [input() for i in range(r)]
for i in range(r):
    d[i] = [int(j) for j in a[i].split()]

for i in range(r):
    print(" ".join(map(str,d[i]))+" ", end="")
    print(sum(d[i]))

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

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

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

for i in range(r+1):
  for j in range(c):
    print(cells[i][j], end=" ")
  print(cells[i][c])

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

col = []
for i in range(c + 1) :
    element = 0
    for j in range(r) :
        element += ans[j][i]
    col.append(element)

ans.append(col)

for col in ans :
    print(" ".join(map(str, col)))

Solution (Python):
r, c = map(int, input().split())
x = [[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): x[i][j] = a[j]
    x[i][c] = sum(a)
for j in range(c + 1):
    s = 0
    for i in range(r):
        s += x[i][j]
    x[r][j] = s
for i in range(r + 1):
    for j in range(c + 1):
        if j == c:
            print(x[i][j])
        else:
            print(x[i][j], "", end = "")

Solution (Python):
r,c = map(int,input().split())
excel = [list(map(int,input().split())) for _ in range(r)]
ans =  [[0]*(c+1) for _ in range(r+1)]
for i,x in enumerate(excel):
    x.append(sum(x))
    ans[i] = x
_sum = 0
for i in range(c+1):
    for j in range(r):
        _sum += excel[j][i]
    ans[r][i] = _sum
    _sum = 0
for y in ans:
    print(*y)

Solution (Python):
rc = input()
rc = rc.split()
r,c = int(rc[0]),int(rc[1])
rc_list = list()
c_list = [[] for i in range(c)]
for i in range(r):
    rc_set = input()
    rc_set = rc_set.split()
    rc_list.append([])
    r_text = ''
    for j,n in enumerate(rc_set):
        rc_list[-1].append(int(n))
        r_text = r_text + n + ' '
        c_list[j].append(int(n))
    else:
        r_text = r_text + str(sum(rc_list[-1]))
        print(r_text)
else:
    c_text = ''
    t_sum = 0
    for c in c_list:
        s = sum(c)
        t_sum += s
        c_text = c_text + str(s) + ' '
    else:
        c_text = c_text + str(t_sum)
        print(c_text)

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

tmp = []
for i in range(c):
    sum_h = 0
    for j in range(r):
        sum_h += rc[j][i]
    tmp.append(sum_h)
tmp.append(sum(tmp))
rc.append(tmp)


for i in rc:
    print(*i)

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

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

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

Solution (Python):
r, c = map(int, input().split())
sheet = [list(map(int,input().split())) for _ in range(r)]
for num, rec in enumerate(sheet):
    sheet[num] += [sum(rec)]
    print(*sheet[num])
print(*map(sum,zip(*sheet)))

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(*row)

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

Solution (Python):
a, b = map(int,input().split())

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

for i in range(b+1):
    aa.append(sum([a[i] for a in bs]))
bs.append(aa)

for i in bs:
    print(" ".join(map(str,i)))

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

col_sum = [0]*(c+1)
for li in l:
    li.append(sum(li))
    for i in range(c+1):
        col_sum[i] += li[i]

for li in l:
    print(*li)
print(*col_sum)

Solution (Python):
r, c = map(int, input().split())
t = []
l = [0]*(c+1)
for _ in range(r):
    *_r, = map(int, input().split())
    _r.append(sum(_r))
    t.append(_r)

    l = [_l+__r for _l, __r in zip(l, _r)]

for _t in t:
    print(*_t)
print(*l)

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

for i in range(r):
    tmp=sum(spl[i])
    spl[i].append(tmp)

cnt=[]
for j in range(c+1):
    tmp=0
    for k in range(r):
        tmp+=spl[k][j]
    cnt.append(tmp)

spl.append(cnt)

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

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

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

for i in range(r):
    row = list(map(int, input().split()))
    table[i][-1]=sum(row)
    for j in range(c):
        table[-1][j] += row[j]
        table[i][j] = row[j]
    table[-1][-1] += sum(row)

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

Solution (Python):
r,c=map(int,input().split())
A=[[0 for i in range(c)] for j in range(r)]
for i in range(r):
    A[i]=list(map(int,input().split()))
tate=[0 for i in range(r)]
yoko=[0 for i in range(c)]
for i in range(r):
    for j in range(c):
        tate[i]+=A[i][j]
for i in range(c):
    for j in range(r):
        yoko[i]+=A[j][i]

for i in range(r):
    for j in range(c):
        print(A[i][j],end=" ")
    print(tate[i])
for i in range(c):
    print(yoko[i],end=" ")
print(sum(tate))

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

s = ([0 for x in range(c)])

for i in range(r):
    cell = ([int(x) for x in input().split()])
    
    cell_sum = 0
    for j in range(c):
        print(cell[j], end=' ')
        
        s[j] += cell[j]
        cell_sum += cell[j]
    print(cell_sum)

total = 0;
for i in s:
    print(i, end=' ')
    
    total += i
    
print(total)

Solution (Python):
import sys

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

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

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

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

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

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

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

for i in range(r):
    val = list(map(int, input().split()))
    val.append(sum(val))
    answer.append(val)

sumLine = [0] * (c + 1)
for i in range(len(answer)):
    for j in range(c + 1):
        sumLine[j] += answer[i][j]

answer.append(sumLine)

[print(*answer[i]) for i in range(len(answer))]

Solution (Python):
r,c=(int(x) for x in input().split())
A=[[0 for i in range(c)] for j in range(r)]
b=[0]*r
d=[0]*(c+1)
for i in range(r):
    A[i]=[int(x) for x in input().split()]
for i in range(r):
    for j in range(c):
        b[i]+=A[i][j]
for i in range(c):
    for j in range(r):
        d[i]+=A[j][i]
for i in range(r):
    d[c]+=b[i]
for i in range(r):
    for j in range(c+1):
        if j!=c:
            print(A[i][j],end=" ")
        else:
            print(b[i])
for i in range(c):
    print(d[i],end=" ")
print(d[c])