# 13-Structured_Program_II-Spreadsheet.py

# ??¨?¨????
# ??¨?¨??????????????°???????????????°???????????????????????????
# ??¨????????°r??¨?????°c???r ?? c ???????´????????????¨?????????????????§???
# ????????¨??????????¨?????????\????????°????????¨???????????????????????°?????????????????????????????????

# Input
# ???????????????r??¨c????????????????????§?????????????????????
# ?¶????r??????????????????c????????´??°????????????????????§?????????????????????

# Output
# (r+1) ?? (c+1) ?????°????????¨???????????????????????????
# ???????????£???????????´??°????????????????????§????????£??????????????????????????????????????¨??????????????????????¨?????????????????????????????????¨??????????????????????¨?????????????????????????????????¨??¨??????????¨????????????\??????????????????

# Constraints
# 1 ??? r, c ??? 100
# 0 ??? ????´? ??? 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


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

data = []
csum = [0]*c

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

# print(data)

for i in range( r ):
	for j in range(c):
		print("{0} ".format(data[i][j]), end="")
		csum[j] +=data[i][j]
	print( sum(data[i]) )

for j in range(c):
	print("{0} ".format( csum[j] ), end="" )

print( sum(csum) )



# ??? Aizu??§numpy ??????????????£??????
#---------------------------------------------------------------
# import numpy as np

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

# data = np.zeros((r,c))

# for i in range(r):
# 	data[i] = np.array( input().split() )

# data=data.astype("int")
# print("data",data)

# for i in range( r ):
# 	for j in range(c):
# 		print("{0} ".format(data[i][j]), end="")
# 	print( np.sum(data[i]) )

# for j in range(c):
# 	print("{0} ".format( np.sum(data, axis=0)[j] ), end="" )

# print( np.sum(data) )
#---------------------------------------------------------------

### FIXED CODE ###

# 12-Structured_Program_II-How_many_ways.py

# ????????????????????°
# 1 ?????? n ?????§?????°?????????????????????????????§???????????°?????????????????????????¨???? x ??¨??????
# ????????????????????°????±???????????????°?????????????????????????????????
# ????????°???1 ?????? 5 ?????§?????°???????????????????????§????????????????¨???? 9 ??¨???????????????????????????

# 1 + 3 + 5 = 9
# 2 + 3 + 4 = 9
# ??????????????????????????????

# Input
# ?????°??????????????????????????\?????¨???????????????????????????
# ???????????????????????§??????????????§??????????????? n???x ??? 1 ???????????????????????????

# n???x ?????¨?????? 0 ?????¨?????\?????????????????¨????????????
# n ??? 3 ??\??? 100?????\?????¨????????????

# Output
# ????????????????????????????????????????????????????????°????????????????????????????????????

# Sample Input
# 5 9
# 0 0
# Sample Output
# 2

result = []
n=[]
x=[]

while 1:
	temp = input().split() 
	if temp[0]=="0" and temp[1]=="0":
		break;
	else:
		n.append( int( temp[0] ))
		x.append( int( temp[1] ))

for i in range( len(n) ):
	count =0
	for n1 in range(1,n[i]+1-2):
		for n2 in range(n1+1, n[i]+1-1):
			for n3 in range(n2+1, n[i]+1):
				sum = n1+n2+n3
				if sum == x[i]:
					count+=1
	result.append(count)

for i in range( len(result) ):
	print(result[i])
	# if i == len(result)-1:
	# 	print(result[i])
	# else:
	# 	print("{0} ".format(result[i]), end="")