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):
def mapt(fn, *args):
    return list(map(fn, *args))

def Array(listseq):
    return mapt(Vector, listseq)

def Vector(lines):
    return mapt(str, lines)
    
    
r, c = mapt(int, input().split(" "))
arrays = [mapt(int, input().split(" ")) for i in range(r)]

def calc(arrays, r):
    y_axis = [sum(i) for i in zip(*arrays)]
    arrays.append(y_axis)
    x_axis = [sum(i) for i in arrays]
    for i in range(r+1):
        arrays[i].append(x_axis[i])
    for row in Array(arrays):
        print(" ".join(row))

calc(arrays, r)

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

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

matrix = []
column_sum = [0]*(c+1)

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

for row in matrix:
    row_sum = 0
    for i in range(len(row)):
        row_sum += row[i]
        column_sum[i] += row[i]
    row.append(row_sum)
    column_sum[c] += row_sum
matrix.append(column_sum)

for row in matrix:
    print(' '.join(list(map(str, row))))

Solution (Python):
N,M=map(int,input().split())
W=[list(map(int,input().split())) for i in range(N)]
E=[0]*(M+1)
for h in range(N):
  a=sum(W[h])
  W[h].append(a)
for l in range(N):
  for g in range(M+1):
    E[g]+=W[l][g]
W.append(E)
for e in range(N+1):
  for b in range(M+1):
    if b!=M:
      print(W[e][b],end=" ")
    else:
      print(W[e][b])

Solution (Python):




def main():
    r, c = map(int, input().split())
    T = []
    S = [0 for _ in range(c+1)]
    for i in range(r):
        C = list(map(int, input().split()))
        C.append(sum(C))
        T.append(C)
    for i in range(r):
        for j in range(c+1):
            S[j] += T[i][j]
    T.append(S)
    for i in range(r+1):
        print(*T[i])


if __name__ == '__main__':
    main()

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

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

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

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

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

S = [0 for j in range(m)]
c = 0
for i in range(n):
   for j in range(m):
      print(A[i][j], end = "")
      print(" ", end = "")
   print(sum(A[i]))
   c += sum(A[i])

for j in range(m):
   sum = 0
   for i in range(n):
      sum += A[i][j]
   print(sum, end = "")
   print(" ", end = "")
print(c)

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


A = [[int(x) for x in input().split()] for l in range(r)]
newA = [[0]*(c+1) for i in range(r+1)]

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

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

Solution (Python):
n,m = map(int, input().split())
input_sheet = [[0]*m]*n
add_migi = [0]*n
add_shita = [0]*(m+1)
output_sheet = [[0 for i in range(m+1)] for j in range(n+1)]
for i in range(n):
  input_sheet[i] = list(map(int, input().split()))
  output_sheet[i][m] = sum(input_sheet[i])
  for j in range(m):
    output_sheet[i][j] = input_sheet[i][j]
for j in range(m):
  for i in range(n):
    output_sheet[n][j] += input_sheet[i][j]
output_sheet[n][m] = sum(map(sum, input_sheet))

for i in range(n+1):
  for j in range(m):
    print(output_sheet[i][j],end=' ')
  print(output_sheet[i][m],sep='')

Solution (Python):
r,c = map(int,input().split())
result = [0 for i in range(c+1)]
for _ in range(r):
    add = [int(x)for x in input().split()]
    add.append(sum(add))
    print(" ".join([str(x) for x in add]))
    result = [i + j for i,j in zip(result,add)]
print(" ".join([str(x) for x in result]))

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

a=[[0 for i in range(c)] for j in range(r)]
newa=[[0 for i in range(c+1)] for j in range(r+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):
    newa[i][c]+=a[i][j]
    newa[r][j]+=a[i][j]
    newa[r][c]+=a[i][j]
    newa[i][j]=a[i][j]

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

  print("")

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

Solution (Python):
r,c=map(int,input().split())
tbl=[[0 for i in range(c+1)] for j in range(r+1)]
for i in range(r):
    a = list(map(int,input().split()))
    a_1 = len(a)
    b = 0
    for j in range(a_1):
        tbl[i][j] = a[j]
        b += a[j]
    tbl[i][c] = b
for k in range(c+1):
    d = 0
    for l in range(r):
        d += tbl[l][k]
    tbl[r][k] = d
for m in tbl:
    print(*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):
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())
a=[[0 for i in range(c+1)] for j in range(r+1)]
lst=[[int(a) for a in input().split()] for x in range(r)]

for k in range(r):
    for l in range(c):
        a[k][l]=lst[k][l]
        
for n in range(r+1):
    for m in range(c,c+1):
        a[n][m]=sum(a[n])
        
for n in range(c):
    s=0
    for m in range(r):
        s+=a[m][n]
    a[r][n]=s
a[r][c]=sum(a[r])
for n in range(r+1):
    print(*a[n])

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

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

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

Solution (Python):



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

for i in range(r):
    b = list(map(int,input().split()))
    for j in range(c):
        a[i][j] = b[j]
for i in range(r):
    for j in range(0,c):
        a[i][c] += a[i][j]
for i in range(0,c+1):
    for j in range(0,r):
        a[r][i] += a[j][i]
for i in range(0,r+1):
    print(a[i][0], end = "")
    for j in range(1,c+1):
        print("", a[i][j], end = "")
    print()

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):
row, col = map(int, input().split())
table = [list(map(int, input().split())) for _ in range(row)]
ans   = [[0 for c in range(col+1)] for r in range(row+1)]

for r in range(row):
    for c in range(col+1):
        if c == col:
            ans[r][c] = sum(table[r][:])
        else:
            ans[r][c] = table[r][c]
        ans[row][c] += ans[r][c]
        
    print(*ans[r][:])
print(*ans[row][:])

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

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

Solution (Python):
num=list(map(int,input().split(" ")))
matrix=[list(map(int,input().split(" "))) for i in range(num[0])]
tmp=list(map(lambda x:x.append(sum(x)),matrix))
a=[sum([i[j] for i in matrix])for j in range(num[1]+1)]
matrix.append(a)
for i in matrix:
    print(" ".join(map(str,i)))

Solution (Python):
r,c = map(int, input().split())
s=[]
T=[]
t=0
for k in range(r):
   x = list(map(int, input().split()))
   x.append(sum(x))
   s.append(x)
for i in range(c+1):
   t=0
   for j in range(r):
      t += s[j][i]
   T.append(t)
s.append(T)
for p in range(r+1):
   print(*s[p])

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

def num_get(r):
    nums = []
    for i in range(r):
        num = list(map(int, input().split()))
        nums.append(num)
    return nums

def hori_sum(num, r):
    nums = []
    for i in range(r):
        sum_i = sum(num[i])
        num[i].append(sum_i)
        print(" ".join([str(j) for j in num[i]]))
        nums.append(num[i])
    
    return nums

def verti_sum(nums, r, c):
    sums = []
    sum_v = 0
    c = c + 1
    for i in range(c):
        
        for j in range(r):
            
            sum_v = sum_v + nums[j][i]
        sums.append(sum_v)
        sum_v = 0
    print(" ".join([str(i) for i in sums]))
    return sums

nums = num_get(r)
hori = hori_sum(nums, r)

verti_sum(hori, r, c)

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

Solution (Python):
r,c=map(int,input().split())
M=[]
N=[]
S=0
e=0

for i in range(r):
    L=list(input().split())
    for j in range(c):
        S+=int(L[j])
    L.append(S)
    M.append(L)
    S=0
    
for k in range(r):
    d=M[k]
    
    for l in range(c+1):
        if l<c:
            print(d[l],end=' ')
        else:
            print(d[l])
            
for m in range(c+1):
    for n in range(r):
        a=M[n]
        e+=int(a[m])
    if m<c:
        print(e,end=' ')
        e=0
    else:
        print(e)
        e=0

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

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

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

b=[[0]*(c+1) for i 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):
    print(*b[i])

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

Solution (Python):
r,c = map(int,input().split())
l=[]
SY = 0
for i in range(r):
    a = list(map(int,input().strip().split()))
    l.append(a)
for j in l:
    j.append(0)
l.append([0]*(c+1))

for i in range(r):
    sx = 0 
    for j in range(c):
        sx +=l[i][j]
    l[i][c] = sx
   
for j in range(c):
    sy = 0
    for i in range(r):
        sy +=l[i][j]
    l[r][j] =sy
    SY +=sy
l[r][c] = SY

for k in l:
    print(*k)

Solution (Python):

r,c=map(int,input().split())
cc=[]
for l in range(c+1):
    cc.append(0)
    
for i in range(r):
    rr=list(map(int,input().split()))

    x=0
    for j in rr:
        x+=j
    rr.append(x)
    print(*rr)
    
    for k in range(c+1):

        cc[k]+=rr[k]
    
print(*cc)

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

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

m=[]    
for i in range(a):
    k=list(map(int,input().split()))
    k.append(sum(k))
    m.append(k)
    print(' '.join(map(str,k)))

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

Solution (Python):



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

arr = []
col_t = [0]*(c)

for _ in range(r):
    arr.append(list(map(int, input().split())))

for row in range(r):
    row_t = 0
    for col in range(c):
        row_t += arr[row][col]
        col_t[col] += arr[row][col]
    arr[row].append(row_t)

total = 0
for v in col_t:
    total += v
col_t.append(total)
arr.append(col_t)

for v in arr:
    print(' '.join(map(str, v)))

Solution (Python):

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

Sum = 0

c_sum = [0 for i in range(c)]

for i in range(r):
    temp = list(map(int, input().split()))
    Sum += sum(temp)
    r_sum = 0
    for j in range(c):
        print(temp[j], end=' ')
        r_sum += temp[j]
        c_sum[j] += temp[j]
    print(r_sum)
    
for i in range(c):
    print(c_sum[i], end=' ')
    
print(Sum)

Solution (Python):
r,c = map(int,input().split())
A = [0 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]))
B = []
for i in range(c+1):
    x = 0
    for j in range(r):
        x += A[j][i]
    B.append(x)
A[r] = B    

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

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

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

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

Solution (Python):
r, c = map(int, input().split())
matrix = []
for _ in range(r):
    data_row = list(map(int, input().split()))
    sum_row = 0
    for num_row in data_row:
        sum_row += num_row
    data_row.append(sum_row)
    matrix.append(data_row)

data_col = []
for i in range(c + 1):
    sum_col = 0
    for j in range(r):
        sum_col += matrix[j][i]
    data_col.append(sum_col)
matrix.append(data_col)
    
for row in matrix:
    for i, num in enumerate(row):
        if i > 0:
            print(" ", end="")
        print(num, end="")    
    print()

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

Solution (Python):
R,C,*T = list(map(int, open(0).read().split()))
r = [0] * C
for i in range(0, len(T), C):
    for j in range(C):
        print(T[i+j], end="")
        print(" ", end="")
        r[j] += T[i+j]
    print(sum(T[i:i+C]))
r.append(sum(r))
print(" ".join(map(str, r)))

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

lst.append([sum([x[j] for x in lst]) for j in range(c)])

for i in range(r+1):
    lst[i].append(sum(lst[i]))
    print(" ".join([str(x) for x in lst[i]]))

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

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

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

numbers_list = []

for _ in range(rows):
  numbers = [int(y) for y in input().split()]
  numbers.append(sum(numbers))

  numbers_list.append(numbers)

column_sums = [sum(x) for x in zip(*numbers_list)]

numbers_list.append(column_sums)

string_list = []

for number_list in numbers_list:
  string_list.append([str(x) for x in number_list])

for number_list in string_list:
  print(" ".join(number_list))

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

for i in range(r):
    h[i].append(sum(h[i]))
    
for i in range(c+1):
    t = 0
    for j in range(r):
        t += h[j][i]
    total.append(t)
    
for i in range(r):
    print(*h[i])

print(*total)

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

res = [0 for i in range(c)]
for i in range(r):
    li = [int(x) for x in list(input().split())]
    for j, k in enumerate(li):
        print("{} ".format(k), end="")
        res[j] += k
    print(sum(li), end="")
    print("")

for rr in res:
    print("{} ".format(rr), end="")
print(sum(res))

Solution (Python):
a,b=map(int,input().split())
c = []
for i in range(a):
    c.append(list(map(int,list(input().split()))))
    c[i].append(sum(c[i]))
e=[]
for i in range(b):
    total=0
    for j in range(a):
        total+=c[j][i]
    e.append(total)
e.append(sum(e))
c.append(e)
"""
for i in range(a+1):
    for j in range(b+1):
        print(c[i][j],end=" ")
    print()
"""
for row in range(0,a+1):
    print("%d"%(c[row][0]),end="")
    for col in range(1,b+1):
        print(" %d"%(c[row][col]),end ="")
    print()

Solution (Python):

import copy
r, c = map(int, input().split())
a = [input().split() for i in range(r)]

M=copy.copy(a)
tmp = []
all = 0

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

    M[i].append(s_r)

for i in range(c):
    s_c = 0
    for j in range(r):
        s_c += int(a[j][i])
    tmp.append(s_c)
    all += s_c
tmp.append(all)
M.append(tmp)

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

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

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

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

print(*cl, sep=" ")

Solution (Python):
r,c = map(int,input().split())
table = [None] * r
sum_c = [0] * (c+1)
for i in range(r):
    tmp = list(map(int,input().split()))
    sum_r = sum(tmp)
    tmp.append(sum_r)
    for j in range(c+1):
        sum_c[j] += tmp[j]
    table[i] = tmp
table.append(sum_c)

for line in table:
    print(" ".join(map(str, line)))

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

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

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

for j in range(c) :
	print(c_sum[j],end=" ")
	all += c_sum[j]
print(all)

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

    sum_c = [0 for _ in range(c)]
    for i in range(r):
        row = list(map(int, input().split()))
        for j, x in enumerate(row):
            print("{} ".format(x), end='')
            sum_c[j] += row[j]
        print(sum(row))

    [print("{} ".format(x), end="") for x in sum_c]
    print(sum(sum_c))


resolve()

Solution (Python):
r, c = map(int, input().split())
mat = [list(map(int, input().split())) for _ in range(r)]
for i in range(r):
    mat[i].append(sum(mat[i]))
new_row = []
for i in range(c):
    total_c = 0
    for j in range(r):
        total_c += mat[j][i]
    new_row.append(total_c)
new_row.append(sum(new_row))
mat.append(new_row)
for i in range(r+1):
    for j in range(c):
        print(f'{mat[i][j]} ', end='')
    print(mat[i][c])

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

table = []
for i in range(r):
    table.append([])
    dataset = input().split(" ")
    for j in range(c):
        table[i].append(int(dataset[j]))


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


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

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

for row in range(r+1):
    for col in range(c+1):
        if col != c:
            print("%d " % table[row][col], end="")
        else:
            print("%d" % table[row][col])

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

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

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

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

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

Solution (Python):
r, c = list(map(int, input().split(' ')))
l = []
for i in range(r):
    l.append(list(map(int, input().split(' '))))
yl = []
xl = []
for y in range(r):
    xl.append(0)
    for x in range(c):
        xl[y] += l[y][x]
    l[y].append(xl[y])
t = [0] * len(l[0])
for y in range(r):
    for x in range(len(t)):
        t[x] += l[y][x]
l.append(t)
for a in l:
    print(*a)

Solution (Python):
n,m=map(int,input().split(' '))
a=[]
for i in range(n):
  tmp=list(map(int,input().split(' ')))
  tmp.append(sum(tmp))
  a.append(tmp)
al=[]
for i in range(m+1):
  tmp=0
  for j in range(n):
    tmp+=a[j][i]
  al.append(tmp)
a.append(al)

for i in a:
  s=''
  for j in range(len(i)):
    if j==len(i)-1:
      s+=str(i[j])
    else:
      s+=str(i[j])+' '
  print(s)

Solution (Python):


def main():
    lines=[]
    rows,coloums = map(int,input().split())
    lines=[list(map(int,input().split())) for _ in range(rows)]
    colmus_total=[0]*coloums
    for line in lines:
        row_total=0
        row_output=" ".join(map(str,line))
        for col_index,data in enumerate(line):
            row_total+=data
            colmus_total[col_index]+=data
        print(row_output,row_total)
    col_output=" ".join(map(str,colmus_total))
    print(col_output,sum(colmus_total))

if __name__=="__main__":
    main()

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

for i in range(r):
    line = [int(i) for i in input().strip().split()]
    for j in range(c):
        table[i][j] = line[j]

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

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

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

Solution (Python):
H, W = map(int, input().split())
sheet = [list(map(int,input().split())) for j in range(H)]
for i in range(H):
    sheet[i].append(sum(sheet[i]))
sheet.append([0 for i in range(W+1)])
for i in range(W+1):
    tmp = 0
    for j in range(H):
        tmp+=sheet[j][i]
    sheet[H][i] = tmp
for i in range(H+1):
    print(" ".join(map(str,sheet[i])))

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

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

calc_c_lst = []
for i in range(c):
    calc_c = 0
    for j in range(r):
        calc_c += nums[j][i]
    calc_c_lst.append(calc_c)
sum_c = sum(calc_c_lst)
calc_c_lst.append(sum_c)
ans_lst = nums + [calc_c_lst]

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

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

Solution (Python):
rows,cols = map(int, input().split())
matrix = [[0 for cols in range(cols+1)] for rows in range(rows+1)]

for row in range(rows):
    vs = list(map(int, input().split()))
    for col in range(cols):
        matrix[row][col] = vs[col]
    matrix[row][cols] = sum(vs)

for col in range(cols + 1):
    total = 0
    for row in range(rows):
        total += matrix[row][col]
    matrix[rows][col] = total

for row in range(rows+1):
    for col in range(cols+1):
        if col == 0:
            print(f"{matrix[row][col]}", end="")
        else:
            print(f" {matrix[row][col]}", end="")
    print("")

Solution (Python):

a = []
i = 0
j = 0
sum = 0
sumsum = 0

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

while i < r:
    a.append(list(map(int, input().split())))
    i += 1

for i in range(r + 1):
    sum = 0

    if i < r:
        for j in range(c + 1):
            if j < c:
                print("%s " % a[i][j], end="")
                sum += a[i][j]
            else:
                print("%s" % sum)
    else:
        for j in range(c + 1):
            sum = 0

            if j < c:
                for i in range(r):
                    sum += a[i][j]
                    sumsum += a[i][j]
            
                print("%s " % sum, end="")
            else:
                print("%s" % sumsum)

Solution (Python):

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

for j in range(r):
    s = 0
    print(*l[j],end=" ")
    print(sum(l[j]))
    s = sum(l[j])
    a += s
    

for n in range(c):
    c = 0
    for m in range(r):
        c += l[m][n]
    print(c,end=" ")
print(a)

Solution (Python):
n,m=map(int,input().split())

a=[list(map(int,input().split())) for i in range(n)]
b=[[0]*(m+1) for i in range(n+1)]

for i in range(n):
    for j in range(m):
        b[i][j]=a[i][j]

for i in range(n):
    tmp=0
    for j in range(m):
        b[i][m]+=b[i][j]
    print(*b[i])
    
for i in range(m+1):
    tmp=0
    for j in range(n):
        b[n][i]+=b[j][i]
print(*b[n])

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

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

ans = 0
for i in range(r):
    for j in range(c):
        print('{} ' .format(table[i][j]), end='')
        ans += table[i][j]
    print(ans)
    ans = 0

total = 0
for i in range(c):
    for j in range(r):
        ans += table[j][i]
    print('{} ' .format(ans), end='')
    total += ans
    ans = 0
print(total)

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

ans = []
for i in range(r):
    tmp = list(map(int, input().split()))
    tmp.append(sum(tmp))
    ans.append(tmp)
    str_tmp = [str(s) for s in tmp]
    str_tmp = " ".join(str_tmp)
    print(str_tmp)

tmp = []
for i in range(c+1):
    total = 0
    for j in ans:
        total += j[i]
    tmp.append(total)
    
str_tmp = [str(s) for s in tmp]
str_tmp = " ".join(str_tmp)
print(str_tmp)

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

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,input().split())

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

y=list()
for j in range(c+1):
    z=0
    for i in range(r):
        z+=x[i][j]
    y.append(z)

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

Solution (Python):
r, c = list(map(int, input().split()))
t = [[int(x) for x in input().split()] for _ in range(r)]
for i in t:
    i.append(sum(i))
u = list(map(list, zip(*t)))
for i in u:
    i.append(sum(i))
for i in list(map(list, zip(*u))):
    print(' '.join(map(str, i)))

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

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

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

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

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

for _ in range(r):
    box.append(list(map(int, input().split())))

for i in range(len(box)):
    box[i].append(sum(box[i]))
    count = len(box[i])
    for a in box[i]:
        if count == 1:
            print(a)
        else:
            print(a, end = ' ')
        count -= 1
            

for x in range(c):
    result = 0
    for y in range(r):
        result += box[y][x]
    final += result
    print(result, end = ' ')

print(final)