People do many crazy things to stand out in a crowd. Some of them dance, some learn by heart rules of Russian language, some try to become an outstanding competitive programmers, while others collect funny math objects.

Alis is among these collectors. Right now she wants to get one of k-special tables. In case you forget, the table n × n is called k-special if the following three conditions are satisfied:  every integer from 1 to n^2 appears in the table exactly once;  in each row numbers are situated in increasing order;  the sum of numbers in the k-th column is maximum possible. 

Your goal is to help Alice and find at least one k-special table of size n × n. Both rows and columns are numbered from 1 to n, with rows numbered from top to bottom and columns numbered from left to right.


-----Input-----

The first line of the input contains two integers n and k (1 ≤ n ≤ 500, 1 ≤ k ≤ n) — the size of the table Alice is looking for and the column that should have maximum possible sum.


-----Output-----

First print the sum of the integers in the k-th column of the required table.

Next n lines should contain the description of the table itself: first line should contains n elements of the first row, second line should contain n elements of the second row and so on.

If there are multiple suitable table, you are allowed to print any.


-----Examples-----
Input
4 1

Output
28
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

Input
5 3

Output
85
5 6 17 18 19
9 10 23 24 25
7 8 20 21 22
3 4 14 15 16
1 2 11 12 13

Difficulty: EASY
Tags: Implementation

Constructive algorithms

Starter code:

Solutions:
(n, k) = map(int, input().split(' '))
a = [[] for i in range(n)]
t = 1
for i in range(n):
	for j in range(k - 1):
		a[i].append(t)
		t += 1
for i in range(n):
	for j in range(k - 1, n):
		a[i].append(t)
		t += 1
print(sum([a[i][k - 1] for i in range(n)]))
for i in range(n):
	print(' '.join(map(str, a[i])))


(a, b) = map(int, input().split())
s = [[0 for n in range(a)] for n in range(a)]
x = a * a
res = 0
for n in range(a):
	for k in range(a - b + 1):
		s[n][k] = x
		x -= 1
	res += s[n][a - b]
	s[n] = s[n][::-1]
x = 1
for n in range(a):
	for k in range(b - 1):
		s[n][k] = x
		x += 1
print(res)
for n in s:
	for k in n:
		print(k, end=' ')
	print()


(n, k) = map(int, input().split())
n2 = n * n
shift = n - k + 1
kth_column = [n2 + 1 - i * shift for i in range(1, n + 1)]
table = [[0] * n for i in range(n)]
left_part_cell = 1
for i in range(n):
	for j in range(n):
		if j < k - 1:
			table[i][j] = left_part_cell
			left_part_cell += 1
		elif j == k - 1:
			table[i][j] = kth_column[i]
		else:
			table[i][j] = table[i][j - 1] + 1
print(sum(kth_column))
[print(' '.join(map(str, row))) for row in table]


(n, k) = map(int, input().split())
a = [[0] * n for _ in range(n)]
s = 0
c = n * n
for i in range(n):
	for j in range(n - 1, k - 2, -1):
		a[i][j] = c
		if j == k - 1:
			s += c
		c -= 1
for i in range(n):
	for j in range(k - 2, -1, -1):
		a[i][j] = c
		c -= 1
print(s)
print('\n'.join([' '.join(map(str, _)) for _ in a]))


(n, k) = [int(t) for t in input().split()]
k -= 1
lst = [[0 for t in range(n)] for t in range(n)]
for col in range(n - 1, k - 1, -1):
	for row in range(n):
		lst[row][col] = n * n - (n - 1 - col) - row * (n - k)
for col in range(k - 1, -1, -1):
	for row in range(n):
		lst[row][col] = n * k - (k - 1 - col) - row * k
print(sum([lst[i][k] for i in range(n)]))
for i in range(n):
	line = ' '.join((str(v) for v in lst[i]))
	print(line)


(n, k) = map(int, input().split())
sol = [[0] * n for _ in range(n)]
res = 0
c = 1
for j in range(k - 1):
	for i in range(n):
		sol[i][j] = c
		c += 1
for i in range(n):
	for j in range(k - 1, n):
		sol[i][j] = c
		c += 1
for i in range(n):
	res += sol[i][k - 1]
print(res)
for line in sol:
	for col in line:
		print(col, end=' ')
	print()


(n, k) = map(int, input().split())
left = 1
right = n ** 2
sum = right - (n - k)
s = sum
for _ in range(n - 1):
	s -= n - k + 1
	sum += s
print(sum)
for _ in range(n):
	ans = []
	if k > 1:
		ans.append(' '.join(map(str, range(left, left + (k - 1)))))
	ans.append(' '.join(map(str, range(right - (n - k), right + 1))))
	print(' '.join(ans))
	left += k - 1
	right -= n - k + 1


R = range
p = ''
(n, k) = map(int, input().split())
s = l = 0
r = n * n
for _ in R(n):
	v = []
	for m in R(k - 1):
		l += 1
		v += [str(l)]
	r -= n - k + 1
	s += r
	for i in R(n - k + 1):
		v += [str(r + i + 1)]
	p += '\n' + ' '.join(v)
print(str(s + n) + p)


(n, m) = map(int, input().split())
a = [[0] * n for _ in range(n)]
for i in range(n * n):
	if i < (m - 1) * n:
		a[i // (m - 1)][i % (m - 1)] = i + 1
	else:
		a[(i - (m - 1) * n) // (n - m + 1)][(i - (m - 1) * n) % (n - m + 1) + m - 1] = i + 1
r = 0
for j in range(n):
	r += a[j][m - 1]
print(r)
for i in range(n):
	for j in range(n):
		print(a[i][j], end=' ')
	print()


readInts = lambda : map(int, input().split())
(n, k) = readInts()
g = [[0] * n for i in range(n)]
num = 1
for i in range(n):
	for j in range(k - 1):
		g[i][j] = num
		num = num + 1
sum = 0
for i in range(n):
	for j in range(k - 1, n):
		g[i][j] = num
		num = num + 1
	sum = sum + g[i][k - 1]
print(sum)
for i in range(n):
	print(*g[i])


import sys

def main():
	(n, k) = (int(_) for _ in input().split())
	ans = []
	for i in range(0, n):
		ans.append([])
	for a in ans:
		for i in range(0, n):
			ans[i].append(0)
	sum = 0
	for i in range(0, n):
		temp = n * n - i * (n - k) - i
		for j in range(0, n - k + 1):
			ans[i][n - j - 1] = temp - j
		sum += ans[i][k - 1]
		temp = i * (k - 1) + 1
		for j in range(0, k - 1):
			ans[i][j] = temp + j
	print(sum)
	for a in ans:
		for v in a:
			print(str(v), end=' ')
		print()
main()


(n, k) = [int(t) for t in input().split()]
k -= 1
lst = [[0 for t in range(n)] for t in range(n)]
for col in range(n - 1, k - 1, -1):
	for row in range(n):
		lst[row][col] = n * n - (n - 1 - col) - row * (n - k)
for col in range(k - 1, -1, -1):
	for row in range(n):
		lst[row][col] = n * k - (k - 1 - col) - row * k
print(sum([lst[i][k] for i in range(n)]))
for i in range(n):
	for k in range(n):
		print(lst[i][k], end=' ')
	print()


(a, b) = map(int, input().split(' '))
ans = [[0] * a for _ in range(a)]
curr = 1
b -= 1
for i in range(b):
	for j in range(a):
		ans[j][i] = curr
		curr += 1
for i in range(a):
	for j in range(a):
		if ans[i][j] == 0:
			ans[i][j] = curr
			curr += 1
tot = 0
for i in range(a):
	tot += ans[i][b]
print(tot)
for i in ans:
	print(' '.join([str(x) for x in i]))


(n, k) = map(int, input().split())
array = list()
array1 = []
d = 0
for c in range(n):
	array1 = []
	for f in range(n):
		array1.append(0)
	array.append(array1)
count = n * n
count1 = 1
for c in range(n):
	for d in range(n - k):
		array[c][n - d - 1] = count
		d = d + 1
		count = count - 1
	array[c][k - 1] = count
	count = count - 1
	for y in range(k - 1):
		array[c][y] = count1
		count1 = count1 + 1
summa = 0
for c in range(n):
	summa = summa + array[c][k - 1]
print(summa)
for c in range(n):
	for h in range(n):
		print(array[c][h], end=' ')
	print()


(n, k) = map(int, input().split())
a = [i + 1 for i in range(n * n)]
b = [[0]]
k -= 1
for _ in range(n):
	b += [a[:k] + a[k - n:]]
	a = a[k:k - n]
	b[0][0] += b[-1][k]
for _ in b:
	print(*_)


(n, k) = map(int, input().split())
matrix = [[0] * n for _ in range(n)]
value = 1
for i in range(n):
	for j in range(k - 1):
		matrix[i][j] = value
		value += 1
for i in range(n):
	for j in range(k - 1, n):
		matrix[i][j] = value
		value += 1
ans = 0
for i in range(n):
	ans += matrix[i][k - 1]
print(ans)
for i in range(n):
	for j in range(n):
		print(matrix[i][j], end=' ')
	print()


def read_list():
	return [int(i) for i in input().split()]

def new_list(n):
	return [0 for i in range(n)]

def new_matrix(n, m=0):
	return [[0 for i in range(m)] for i in range(n)]
(n, k) = read_list()
a = new_matrix(n, n)
cmi = 1
cma = n ** 2
for i in range(n):
	for j in range(k - 1):
		a[i][j] = cmi
		cmi += 1
	for j in range(n - 1, k - 2, -1):
		a[i][j] = cma
		cma -= 1
s = 0
for i in range(n):
	s += a[i][k - 1]
print(s)
for i in range(n):
	for j in range(n):
		print(a[i][j], end=' ')
	print()


(n, k) = map(int, input().split())
x = n - k + 1
first = n * n - x * n + 1
table = [[0] * n for i in range(n)]
a = 1
k -= 1
for i in range(n):
	for j in range(k):
		table[i][j] = a
		a += 1
for i in range(n):
	for j in range(k, n):
		table[i][j] = a
		a += 1
res = 0
for i in range(n):
	res += table[i][k]
print(res)
for i in range(n):
	for j in range(n):
		print(table[i][j], end=' ')
	print()


(n, k) = list(map(int, input().split()))
T = ['' for _ in range(n)]
t = 0
r = 0
for i in range(n):
	for j in range(k - 1):
		t += 1
		T[i] += str(t) + ' '
for i in range(n):
	r += t + 1
	for j in range(k - 1, n):
		t += 1
		T[i] += str(t) + ' '
print(r)
for i in range(n):
	print(T[i][:len(T[i]) - 1])


(n, k) = map(int, input().split())
a = [[0 for i in range(n)] for i in range(n)]
count = 1
for i in range(n):
	for j in range(k - 1):
		a[i][j] = count
		count += 1
for i in range(n):
	for j in range(k - 1, n):
		a[i][j] = count
		count += 1
sum1 = 0
for i in range(n):
	sum1 += a[i][k - 1]
print(sum1)
for i in range(n):
	for j in range(n):
		print(a[i][j], end=' ')
	print()


(n, k) = map(int, input().split(' '))
Sum = 0
(t, s) = ((), pow(n, 2))
for i in range(n):
	tmpt = ()
	for j in range(n - k + 1):
		tmpt += (s,)
		s -= 1
	t += (tmpt,)
	Sum += s
print(Sum + n)
s = 1
for i in range(n):
	tn = ()
	for j in range(k - 1):
		tn += (s,)
		s += 1
	print(str(tn + t[i][::-1]).replace(',', '').replace('(', '').replace(')', ''))


from sys import stdout
(n, k) = [int(t) for t in input().split()]
k -= 1
lst = [[0 for t in range(n)] for t in range(n)]
for col in range(n - 1, k - 1, -1):
	for row in range(n):
		lst[row][col] = n * n - (n - 1 - col) - row * (n - k)
for col in range(k - 1, -1, -1):
	for row in range(n):
		lst[row][col] = n * k - (k - 1 - col) - row * k
print(sum([lst[i][k] for i in range(n)]))
for i in range(n):
	line = ' '.join((str(v) for v in lst[i])) + '\n'
	stdout.write(line)


import sys

def main():
	(n, k) = map(int, input().split())
	mat = [[0] * n for i in range(n)]
	cur = 1
	k -= 1
	for i in range(k):
		for j in range(n):
			mat[j][i] = cur
			cur += 1
	s = 0
	for i in range(n):
		for j in range(k, n):
			mat[i][j] = cur
			cur += 1
		s += mat[i][k]
	print(s)
	for i in range(n):
		for j in range(n):
			print(mat[i][j], end=' ')
		print()
main()


(n, k) = map(int, input().split())
res = list(([0] * n for i in range(n)))
idx = 1
for i in range(n):
	for j in range(k - 1):
		res[i][j] = idx
		idx += 1
sum_res = 0
for i in range(n):
	for j in range(k - 1, n):
		res[i][j] = idx
		idx += 1
	sum_res += res[i][k - 1]
print(sum_res)
for i in res:
	print(*i)


import sys
sys.setrecursionlimit(300000)

def solve(n, k):
	ret = [[0] * n for _ in range(n)]
	kk = k - 1
	v = 1
	for r in range(n):
		for c in range(kk):
			ret[r][c] = v
			v += 1
	for r in range(n):
		for c in range(n - kk):
			ret[r][c + kk] = v
			v += 1
	s = 0
	for r in range(n):
		s += ret[r][k - 1]
	print(s)
	for r in ret:
		print(*r, sep=' ')
	return ret

def main():

	def iterate_tokens():
		for line in sys.stdin:
			for word in line.split():
				yield word
	tokens = iterate_tokens()
	(n, k) = map(int, input().split())
	solve(n, k)
main()


def main():
	(n, k) = map(int, input().split())
	left = k - 1
	right = n - k + 1
	lcntr = 1
	rcntr = left * n + 1
	s = 0
	ls = []
	for i in range(n):
		ll = [str(c) for c in range(lcntr, lcntr + left)]
		rl = [str(c) for c in range(rcntr, rcntr + right)]
		lcntr += left
		rcntr += right
		tl = ll + rl
		s += int(tl[left])
		ls.append(' '.join(tl))
	print(s)
	print('\n'.join(ls))
main()


def main():
	(n, k) = [int(t) for t in input().split()]
	grid = [[-1] * n for _ in range(n)]
	current = n * n
	for row in range(n):
		for column in range(n - 1, k - 2, -1):
			grid[row][column] = current
			current -= 1
	for row in range(n):
		for column in range(k - 2, -1, -1):
			grid[row][column] = current
			current -= 1
	s = sum((grid[row][k - 1] for row in range(n)))
	print(s)
	for row in range(n):
		print(' '.join((str(grid[row][column]) for column in range(n))))
main()


def mp():
	return map(int, input().split())

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

def pt(x):
	print(x)
(n, k) = mp()
pt((n ** 2 - n + k + (k - 1) * n + 1) * n // 2)
for i in range(n):
	L1 = [(k - 1) * i + r for r in range(1, k)]
	L2 = [(n - k + 1) * i + (k - 1) * n + r for r in range(1, n - k + 2)]
	print(' '.join([str(s) for s in L1] + [str(t) for t in L2]))


(n, k) = map(int, input().split())
board = [[0] * n for _ in range(n)]
for x in range(k - 1):
	for y in range(n):
		board[y][x] = x * n + y + 1
val = n * (k - 1)
for y in range(n):
	for x in range(k - 1, n):
		val += 1
		board[y][x] = val
print(sum((row[k - 1] for row in board)))
for row in board:
	print(' '.join(map(str, row)))


(n, k) = map(int, input().split())
print(int((n * (k - 1) + 1 + (n - 1) * (n - k + 1) / 2) * n))
for i in range(n):
	curr = []
	for j in range(n):
		if j + 1 == k:
			curr.append(n * (k - 1) + 1 + i * (n - k + 1))
		elif j + 1 > k:
			curr.append(n * (k - 1) + 1 + i * (n - k + 1) + (j + 1 - k))
		else:
			curr.append(i * (k - 1) + j + 1)
	print(' '.join(map(str, curr)))


(n, k) = map(int, input().split())
k -= 1
cnt = 1
ans = 0
p = [[0 for i in range(n)] for i in range(n)]
for i in range(n):
	for j in range(k):
		p[i][j] = str(cnt)
		cnt += 1
for i in range(n):
	ans += cnt
	for j in range(k, n):
		p[i][j] = str(cnt)
		cnt += 1
print(ans)
print('\n'.join([' '.join(x) for x in p]))


import re, sys, string, operator, functools, fractions, collections
sys.setrecursionlimit(10 ** 7)
dX = [-1, 1, 0, 0, -1, 1, -1, 1]
dY = [0, 0, -1, 1, 1, -1, -1, 1]
RI = lambda x=' ': list(map(int, input().split(x)))
RS = lambda x=' ': input().rstrip().split(x)
mod = int(1000000000.0 + 7)
eps = 1e-06
(n, k) = RI()
l = 1
r = n * (k - 1) + 1
print(n * (2 * r + (n - k + 1) * (n - 1)) // 2)
for i in range(n):
	for j in range(n):
		if j + 1 >= k:
			print(r, end=' ')
			r += 1
		else:
			print(l, end=' ')
			l += 1
	print()


(n, k) = list(map(int, input().split()))
x = n ** 2
a = [[0 for i in range(n)] for j in range(n)]
for i in range(n):
	for j in range(n - 1, k - 2, -1):
		a[i][j] = x
		x -= 1
for i in range(n):
	for j in range(k - 2, -1, -1):
		a[i][j] = x
		x -= 1
print(sum([a[i][k - 1] for i in range(n)]))
for i in range(n):
	print(' '.join(list(map(str, a[i]))))


(n, k) = map(int, input().split())
print(n * (n * n + (k - 2) * n + k + 1) // 2)
t1 = [[0] * n for i in range(n)]
for j in range(k - 1):
	for i in range(n):
		t1[i][j] = i + 1 + n * j
for i in range(n):
	for j in range(k - 1, n):
		t1[i][j] = j + i * (n - k + 1) + n * (k - 1) + 2 - k
for i in range(n):
	print(' '.join((str(j) for j in t1[i])))


import sys
(n, k) = map(int, input().split())
arr = [0] * n
for i in range(n):
	arr[i] = [0 for x in range(n)]
q = 1
for i in range(n):
	for j in range(k - 1):
		arr[i][j] = q
		q += 1
for i in range(n):
	for j in range(k - 1, n):
		arr[i][j] = q
		q += 1
ans = 0
for i in range(n):
	ans += arr[i][k - 1]
print(ans)
for i in arr:
	for j in i:
		sys.stdout.write(str(j) + ' ')
	sys.stdout.write('\n')


int_inp = lambda : int(input())
strng = lambda : input().strip()
strl = lambda : list(input().strip())
mul = lambda : map(int, input().split())
mulf = lambda : map(float, input().split())
seq = lambda : list(map(int, input().split()))
import math
from collections import Counter, defaultdict
(a, b) = map(int, input().split())
z = []
x = 1
s = 0
for i in ' ' * a:
	k = [1] * a
	z += [k]
for i in range(a):
	for j in range(b - 1):
		z[i][j] = x
		x += 1
for i in range(a):
	for j in range(b - 1, a):
		z[i][j] = x
		x += 1
print(sum((z[i][b - 1] for i in range(a))))
for i in z:
	print(*i)


(n, k) = map(int, input().split())
a = [[0] * n for i in range(n)]
k -= 1
m = n - k
for i in range(n):
	for j in range(k):
		a[i][j] = i * k + j + 1
for i in range(n):
	for j in range(m):
		a[i][j + k] = n * k + i * m + j + 1
print(sum((a[i][k] for i in range(n))))
[print(*i) for i in a]


(n, k) = map(int, input().split(' '))
(a, x, y, s) = ([], 1, n * (k - 1) + 1, 0)
for i in range(n):
	a.append([])
	for j in range(k - 1):
		a[i].append(x)
		x += 1
	for j in range(n - k + 1):
		a[i].append(y)
		y += 1
	s += a[i][k - 1]
print(s)
for i in range(n):
	for j in range(n):
		print(a[i][j], end=' ')
	print()


(n, k) = [int(x) for x in input().split()]
M1 = [[i + n * j for j in range(k - 1)] for i in range(1, n + 1)]
M2 = [[(k - 1) * n + i * (n - k + 1) + j for j in range(1, n - k + 2)] for i in range(0, n)]
M = [M1[i] + M2[i] for i in range(n)]
print((n - k + 1) * (n - 1) * n // 2 + ((k - 1) * n + 1) * n)
for i in range(n):
	for j in range(n):
		print(M[i][j], end=' ')
	print()


(n, k) = map(int, input().split())
ans = list()
left = list()
if k != 1:
	for idx in range(1, n * (k - 1) + 1, k - 1):
		left.append(list(range(idx, idx + k - 1)))
right = list()
sm = 0
for idx in range(n * (k - 1) + 1, n * n + 1, n - k + 1):
	sm += idx
	right.append(list(range(idx, idx + n - k + 1)))
ans = list()
for idx in range(n):
	if k == 1:
		ans.append(right[idx])
	else:
		ans.append(left[idx] + right[idx])
print(sm)
for v in ans:
	print(*v)


(n, k) = list(map(int, input().split()))
g = [[0 for _ in range(n)] for _ in range(n)]
x = 1
for i in range(n):
	for j in range(k - 1):
		g[i][j] = x
		x += 1
for i in range(n):
	for j in range(k - 1, n):
		g[i][j] = x
		x += 1
ret = 0
for i in range(n):
	ret += g[i][k - 1]
print(ret)
print('\n'.join([' '.join(map(str, x)) for x in g]))


(n, k) = (int(x) for x in input().split())
summ = 0
data = []
for i in range(1, n + 1):
	last = n ** 2 - (n - k) * i - i + 1
	data.append(last)
	summ += last
print(summ)
for i in range(1, n + 1):
	t = list(range(last - k + 1, last))
	value = [0] * n
	for j in range(1, n + 1):
		if j < k:
			value[j - 1] = str(t[j - 1])
		elif j == k:
			value[j - 1] = str(data[i - 1])
		else:
			value[j - 1] = str(data[i - 1] + j - k)
	last -= k - 1
	print(' '.join(value))


(n, k) = map(int, input().split(' '))
ans = [[0 for i in range(n)] for j in range(n)]
cur = n ** 2
for i in range(n):
	for j in range(k - 1, n):
		j = n + k - 2 - j
		ans[i][j] = cur
		cur -= 1
cur = 1
for i in range(n):
	for j in range(k - 1):
		ans[i][j] = cur
		cur += 1
print(sum((ans[i][k - 1] for i in range(n))))
for row in ans:
	print(' '.join(map(str, row)))


(n, k) = [int(t) for t in input().split()]
mat = []
for row in range(n):
	left = [str(t) for t in range(n * (k - 1) - row * (k - 1) - (k - 2), n * (k - 1) - row * (k - 1) + 1)]
	right = [str(t) for t in range(n ** 2 - row * (n - k + 1) - (n - k + 1) + 1, n ** 2 - row * (n - k + 1) + 1)]
	mat.append(' '.join(left) + ' ' + ' '.join(right))
print(sum([n ** 2 - i * (n - k + 1) - (n - k) for i in range(n)]))
print('\n'.join(mat))


import sys
(n, k) = map(int, input().split())
a = [[0] * n for i in range(n)]
a[0][k - 1] = n * n - n + k
for i in range(1, n):
	a[i][k - 1] = a[i - 1][k - 1] - (n - k + 1)
for i in range(n):
	x = a[i][k - 1] + 1
	for j in range(k, n):
		a[i][j] = x
		x += 1
x = 1
for i in range(n):
	for j in range(k - 1):
		a[i][j] = x
		x += 1
cnt = 0
for i in range(n):
	cnt += a[i][k - 1]
print(cnt)
for i in range(n):
	for j in range(n):
		sys.stdout.write(str(a[i][j]) + ' ')
	print()


R = lambda : map(int, input().split())
(n, k) = R()
l = 0
r = [[0 for _ in range(n)] for _ in range(n)]
for i in range(n):
	for j in range(k - 1):
		l += 1
		r[i][j] = l
for i in range(n):
	for j in range(k - 1, n):
		l += 1
		r[i][j] = l
s = 0
for i in range(n):
	s += r[i][k - 1]
print(s)
for i in range(n):
	print(*r[i])


def main():
	(n, k) = (int(x) for x in input().split())
	(total, table) = solver(n, k)
	print(total)
	for row in range(n):
		for col in range(n):
			print(table[row][col], end=' ')
		print()

def solver(n, k):
	col = k - 1
	smallest = col * n + 1
	table = [list(range(col * row + 1, col * (row + 1) + 1)) + list(range((n - col) * row + smallest, (n - col) * (row + 1) + smallest)) for row in range(n)]
	total = n * smallest + n * (n - 1) * (n - col) // 2
	return (total, table)
main()


import sys

def input():
	return sys.stdin.readline().rstrip('\n')
(n, k) = map(int, input().split())
array = [[0 for i in range(n)] for j in range(n)]
num = n ** 2
k_array = [[0 for i in range(n - k + 1)] for j in range(n - k + 1)]
start = n ** 2 - (n - k + 1) * n + 1
for i in range(n):
	for j in range(k - 1, n):
		array[i][j] = start
		start += 1
min_start = 1
for i in range(n):
	for j in range(k - 1):
		array[i][j] = min_start
		min_start += 1
kans = 0
for i in range(n):
	kans += array[i][k - 1]
print(kans)
for i in array:
	print(*i)


(n, k) = [int(x) for x in input().split()]
s = [x for x in range(1, n ** 2 + 1)]
tab = []
sm = 0
l = n - k + 1
for i in range(n):
	t = s[:k - 1] + s[-l:]
	s = s[k - 1:-l]
	sm = sm + t[k - 1]
	tab.append(t)
print(sm)
for i in range(n):
	for j in range(n):
		if j != n - 1:
			print(tab[i][j], end=' ')
		else:
			print(tab[i][j])


sLine = input()
sSplit = sLine.split()
n = int(sSplit[0])
k = int(sSplit[1])
v = []
for i in range(n):
	v.append([])
	for j in range(n):
		v[i].append(0)
x1 = n * n
x2 = 1
for i in range(n):
	for j in range(n - 1, k - 1 - 1, -1):
		v[i][j] = x1
		x1 -= 1
	for j in range(0, k - 1, 1):
		v[i][j] = x2
		x2 += 1
nSum = 0
for i in range(n):
	nSum += v[i][k - 1]
print(nSum)
for i in range(n):
	for j in range(n):
		print(v[i][j], end='')
		if j == n - 1:
			print('')
		else:
			print(' ', end='')


(n, k) = map(int, input().strip().split())
a = [[0 for x in range(n)] for x in range(n)]
cur = n * n
for i in range(0, n):
	for j in range(n - 1, k - 2, -1):
		a[i][j] = cur
		cur -= 1
for i in range(0, n):
	for j in range(k - 2, -1, -1):
		a[i][j] = cur
		cur -= 1
total = 0
for i in range(0, n):
	total += a[i][k - 1]
print(total)
for i in range(0, n):
	for j in range(0, n):
		print(a[i][j], end=' ')
	print()


(n, k) = map(int, input().split())
arr = [[0 for i in range(n)] for j in range(n)]
c = n * n
s = 0
for i in range(n):
	for j in range(n - 1, k - 2, -1):
		arr[i][j] = c
		c -= 1
	s += c + 1
for i in range(n):
	for j in range(k - 2, -1, -1):
		arr[i][j] = c
		c -= 1
print(s)
for i in arr:
	print(*i)


(n, k) = map(int, input().split())
t = [[0] * n for i in range(n)]
s = 0
v = 1
for y in range(n):
	for x in range(k - 1):
		t[y][x] = v
		v += 1
for y in range(n):
	for x in range(k - 1, n):
		t[y][x] = v
		v += 1
for y in range(n):
	s += t[y][k - 1]
print(s)
for y in range(n):
	print(*t[y])


(n, k) = map(int, input().split())
x = 0
y = 0
z = n * (k - 1)
a = []
for i in range((k - 1) * n + 1, n * n + 1, n - k + 1):
	x += i
print(x)
for i in range(0, n):
	for j in range(1, k):
		a.append(y + j)
	y += k - 1
	for j in range(1, n - k + 2):
		a.append(z + j)
	z += n - k + 1
	print(' '.join([str(i) for i in a]))
	a.clear()


(n, k) = map(int, input().split())
ans = [[0 for i in range(n)] for i in range(n)]
curr = 0
i = 1
for x in range(n):
	for j in range(k - 1):
		ans[x][j] = i
		i += 1
an = 0
for x in range(n):
	for j in range(k - 1, n):
		ans[x][j] = i
		i += 1
		if j == k - 1:
			an += ans[x][j]
print(an)
for i in range(n):
	print(*ans[i])


(n, k) = map(int, input().split())
data = [[None for i in range(n)] for j in range(n)]
data[0][k - 1] = n ** 2 - n + k
help = 0
answer = data[0][k - 1]
for i in range(1, n):
	data[i][k - 1] = data[i - 1][k - 1] - (n - k + 1)
	answer += data[i][k - 1]
for i in range(n - 1, -1, -1):
	for j in range(k, n):
		data[i][j] = data[i][j - 1] + 1
for i in range(n - 1, -1, -1):
	for j in range(k - 1):
		data[i][j] = help + 1
		help += 1
print(answer)
for i in range(n):
	print(*data[i])


import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
ALPHA = 'abcdefghijklmnopqrstuvwxyz'
M = 998244353
EPS = 1e-06

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
(n, k) = value()
c = 0
ans = [[0 for i in range(n)] for j in range(n)]
for i in range(n):
	for j in range(k - 1):
		c += 1
		ans[i][j] = c
tot = 0
for i in range(n):
	for j in range(k - 1, n):
		c += 1
		ans[i][j] = c
	tot += ans[i][k - 1]
print(tot)
for i in ans:
	print(*i)


s = list(map(int, input().split()))
n = s[0]
k = s[1]
answer = (n * (k - 1) + 1 + (n ** 2 - n + k)) * n // 2
print(answer)
for i in range(n):
	for j in range(1, k):
		print(i * (k - 1) + j, end=' ')
	for j in range(k, n + 1):
		if j != n:
			print(n * (k - 1) + i * (n - k + 1) + j - k + 1, end=' ')
		else:
			print(n * (k - 1) + i * (n - k + 1) + j - k + 1)


(a, b) = map(int, input().split())
z = []
x = 1
s = 0
for i in ' ' * a:
	k = [1] * a
	z += [k]
for i in range(a):
	for j in range(b - 1):
		z[i][j] = x
		x += 1
for i in range(a):
	for j in range(b - 1, a):
		z[i][j] = x
		x += 1
print(sum((z[i][b - 1] for i in range(a))))
for i in z:
	print(*i)


(n, k) = map(int, input().split())
a = []
for i in range(n):
	a.append([])
	for j in range(n):
		a[i].append(0)
c = 1
for i in range(n):
	for j in range(k - 1):
		a[i][j] = c
		c += 1
for i in range(n):
	for j in range(k - 1, n):
		a[i][j] = c
		c += 1
ans = 0
for i in range(n):
	ans += a[i][k - 1]
print(ans)
for i in range(n):
	print(' '.join(map(str, a[i])))


(n, k) = tuple(map(int, input().split()))
res = n * n * (k - 1) + n + (n - k + 1) * n * (n - 1) // 2
grid = []
for i in range(n):
	grid.append([])
for i in range((k - 1) * n):
	grid[i // (k - 1)].append(i + 1)
for i in range((k - 1) * n, n * n):
	grid[(i - n * (k - 1)) // (n - k + 1)].append(i + 1)
print(res)
for i in range(n):
	print(' '.join(map(str, grid[i])))


(n, k) = map(int, input().split(' '))
ans = [[0 for x in range(n)] for x in range(n)]
count = 1
for x in range(n):
	for y in range(k - 1):
		ans[x][y] = count
		count = count + 1
asn = 0
for x in range(n):
	for y in range(k - 1, n):
		ans[x][y] = count
		count = count + 1
for x in range(n):
	asn = asn + ans[x][k - 1]
print(asn)
for x in ans:
	print(*x)


def main():
	(n, k) = [int(t) for t in input().split()]
	k -= 1
	lst = [[0 for t in range(n)] for t in range(n)]
	for col in range(n - 1, k - 1, -1):
		for row in range(n):
			lst[row][col] = n * n - (n - 1 - col) - row * (n - k)
	for col in range(k - 1, -1, -1):
		for row in range(n):
			lst[row][col] = n * k - (k - 1 - col) - row * k
	print(sum([lst[i][k] for i in range(n)]))
	out = '\n'.join((' '.join((str(v) for v in lst[i])) for i in range(n)))
	print(out)
main()


(n, k) = map(int, input().split())
mat = [[0] * n for i in range(n)]
count = n * n
for i in range(n):
	for j in range(n - 1, k - 2, -1):
		mat[i][j] = count
		count -= 1
sum = 0
for i in range(n):
	sum += mat[i][k - 1]
for i in range(n):
	for j in range(k - 2, -1, -1):
		mat[i][j] = count
		count -= 1
print(sum)
for l in mat:
	print(*l)


(n, k) = map(int, input().split())
c = [[0 for i in range(n)] for i in range(n)]
p = n - k
z = pow(n, 2)
kth = [z - p]
for i in range(1, n):
	kth.append(kth[-1] - (p + 1))
kth.reverse()
u = sum(kth)
print(u)
for i in range(n):
	c[i][k - 1] = kth[i]
o = 1
for i in range(len(c)):
	for j in range(k, len(c[i])):
		c[i][j] = c[i][j - 1] + 1
	for j in range(k - 1):
		c[i][j] = o
		o += 1
for i in range(len(c)):
	print(*c[i])


import sys

def scan(TYPE_1, TYPE_2=0):
	if TYPE_1 == int:
		return map(int, sys.stdin.readline().strip().split())
	elif TYPE_1 == float:
		return map(float, sys.stdin.readline().strip().split())
	elif TYPE_1 == list and TYPE_2 == float:
		return list(map(float, sys.stdin.readline().strip().split()))
	elif TYPE_1 == list and TYPE_2 == int:
		return list(map(int, sys.stdin.readline().strip().split()))
	elif TYPE_1 == str:
		return sys.stdin.readline().strip()
	else:
		print('ERROR!!!!')

def main():
	(n, k) = scan(int)
	a = [[0 for i in range(n)] for j in range(n)]
	cnt1 = n * n
	cnt2 = cnt1 - (n - k + 1) * n
	ans = 0
	for i in range(n - 1, -1, -1):
		for j in range(n - 1, -1, -1):
			if j < k - 1:
				a[i][j] = cnt2
				cnt2 -= 1
			else:
				a[i][j] = cnt1
				cnt1 -= 1
			if j == k - 1:
				ans += a[i][j]
	print(ans)
	for i in range(n):
		for j in range(n):
			print(a[i][j], end=' ')
		print()
main()


def solve(n, k):
	sum = ((n - 1) * (n - 1) + k * (n + 1)) * n // 2
	res = [[]]
	tmp = []
	for j in range(k - 1):
		res[0].append(j + 1)
	for j in range(n - k + 1):
		res[0].append(j + 1 + n * (k - 1))
	for i in range(n - 1):
		for j in range(k - 1):
			tmp.append(res[i][j] + k - 1)
		for j in range(k - 1, n, 1):
			tmp.append(res[i][j] + n - k + 1)
		res.append(tmp)
		tmp = []
	print(sum)
	return res
s = [int(x) for x in input().split()]
res = solve(s[0], s[1])
for x in res:
	for y in x[:-1]:
		print(y, end=' ')
	print(x[-1])


(n, k) = list(map(int, input().split()))
tabl = [[0 for i in range(n)] for i in range(n)]
maxx = n ** 2
minn = 1
summ = 0
for i in range(n):
	for j in range(n):
		if j < k - 1:
			tabl[i][j] = minn
			minn += 1
		else:
			tabl[i][j] = maxx - (n - j) + 1
	maxx -= n - k + 1
	summ += tabl[i][k - 1]
print(summ)
for i in range(n):
	print(*tabl[i])


(n, k) = map(int, input().split())
k -= 1
answer = [[0] * n for i in range(n)]
cur = 1
for i in range(n):
	for j in range(k):
		answer[i][j] = cur
		cur += 1
for i in range(n):
	for j in range(k, n):
		answer[i][j] = cur
		cur += 1
summa = 0
for i in range(n):
	summa += answer[i][k]
print(summa)
for i in range(n):
	print(*answer[i])


(n, k) = map(int, input().split())
a = n * (k - 1) + 1
sm = n * (2 * a + (n - 1) * (n - k + 1)) // 2
t = [[0] * n for i in range(n)]
x = 1
for j in range(k - 1):
	for i in range(n):
		t[i][j] = x
		x += 1
for i in range(n):
	for j in range(k - 1, n):
		t[i][j] = x
		x += 1
print(sm)
for row in t:
	print(' '.join(map(str, row)))


(n, k) = input().split()
(n, k) = (int(n), int(k))
cisel_vlavo = n * (k - 1)
prve_v_k = cisel_vlavo + 1
prve_vlavo = 1
riadok_vpravo = n - k + 1
cislo = prve_v_k
su = 0
for _ in range(n):
	su += cislo
	cislo += riadok_vpravo
print(su)
for r in range(n):
	space = False
	for c in range(1, n + 1):
		if space:
			print(' ', end='')
		else:
			space = True
		if c < k:
			print(prve_vlavo, end='')
			prve_vlavo += 1
		else:
			print(prve_v_k, end='')
			prve_v_k += 1
	print()


(n, k) = [int(i) for i in input().split()]
total_sum = n * n - (n - k)
for row in range(1, n):
	row_right = n * n - row * (n - k + 1)
	val = row_right - (n - k)
	total_sum += val
print(total_sum)
next_l_num = 1
for row in range(1, n + 1):
	row_right = n * n - (row - 1) * (n - k + 1)
	next_r_num = row_right - (n - k)
	for col in range(1, n + 1):
		if col < k:
			print(next_l_num, end=' ')
			next_l_num += 1
		else:
			print(next_r_num, end=' ')
			next_r_num += 1
	print()


string = input()
numbers = string.split()
(a, b) = (int(numbers[0]), int(numbers[1]))
rows = [[0 for x in range(a)] for x in range(a)]
p = a ** 2 + 1
s = 1
n = 0
for x in range(a):
	p -= a - b + 1
	t = s
	for y in range(b - 1):
		rows[x][y] = t
		t += 1
	q = p
	for y in range(b - 1, a):
		rows[x][y] = q
		q += 1
	s += b - 1
	n += p
print(n)
for x in rows:
	print(' '.join(map(str, x)))


(n, m) = map(int, input().split())
(n1, n2) = (1, (m - 1) * n + 1)
ans = 0
print(n * (n - 1) // 2 * (n - m + 1) + n2 * n)
for i in range(n):
	j = 0
	while j < m - 1:
		print(n1, end=' ')
		n1 += 1
		j += 1
	while j < n:
		print(n2, end=' ')
		n2 += 1
		j += 1
	print()


(n, k) = map(int, input().split())
(v, g, c) = (0, [[None] * n for i in range(n)], n * n + 1)
for i in range(n):
	c -= n - k + 1
	v += c
	g[n - 1 - i][k - 1] = c
	for j in range(n - k):
		g[n - 1 - i][k + j] = c + j + 1
for i in range(c - 1):
	(x, y) = divmod(i, k - 1)
	g[x][y] = i + 1
print(v)
for gi in g:
	print(' '.join(map(str, gi)))


(n, k) = map(int, input().split())
mass = [[-1] * n for i in range(n)]
c = 1
for i in range(n):
	for j in range(k - 1):
		mass[i][j] = c
		c += 1
for i in range(n):
	for j in range(k - 1, n):
		mass[i][j] = c
		c += 1
summ = 0
for i in range(n):
	summ += mass[i][k - 1]
print(summ)
for i in range(n):
	print(' '.join(list(map(str, mass[i]))))


(n, k) = map(int, input().split())
table = [[None] * n for i in range(n)]
z = 1
for i in range(k - 1):
	for j in range(n):
		table[j][i] = z
		z += 1
for j in range(n):
	for i in range(k - 1, n):
		table[j][i] = z
		z += 1
s = 0
for line in table:
	s += line[k - 1]
print(s)
for line in table:
	print(*line)


def main():
	(n, k) = map(int, input().split())
	k -= 1
	res = [[0] * n for _ in range(n)]
	i = 1
	for x in range(k):
		for y in range(n):
			res[y][x] = i
			i += 1
	for y in range(n):
		for x in range(k, n):
			res[y][x] = i
			i += 1
	print(sum((res[y][k] for y in range(n))))
	for row in res:
		print(*row)
main()


(n, k) = map(int, input().split())
li = [[0 for _ in range(n + 1)] for _ in range(n + 1)]
start = n ** 2 - (n - k + 1) * n + 1
one = 1
a = n ** 2 + 1
sum = 0
for i in range(n):
	sum += a - (n - k + 1)
	a -= n - k + 1
print(sum)
for i in range(1, n + 1):
	for j in range(1, n + 1):
		if j >= k:
			li[i][j] = start
			start += 1
		else:
			li[i][j] = one
			one += 1
	print(' '.join(map(str, li[i][1:])))


(n, k) = map(int, input().split())
a = []
for i in range(n):
	a.append([])
	for j in range(n):
		a[i].append(0)
m = n ** 2
for j in range(n):
	for i in range(n - k + 1):
		a[j][n - i - 1] = m - i
	m -= n - k + 1
c = n * k - n
while c > 0:
	a[(c - 1) % n][(c - 1) // n] = m
	c -= 1
	m -= 1
s = 0
for i in range(n):
	s += a[i][k - 1]
print(s)
for i in range(n):
	for j in range(n - 1):
		print(a[i][j], end=' ')
	print(a[i][n - 1])


(n, k) = map(int, input().split())
table1 = []
p = 1
for i in range(n):
	table1.append([])
	for j in range(1, k):
		table1[i].append(p)
		p += 1
for i in range(n):
	for j in range(n - k + 1):
		table1[i].append(p)
		p += 1
s = 0
for i in range(n):
	s += table1[i][k - 1]
print(s)
for i in range(n):
	print(' '.join([str(j) for j in table1[i]]))


(n, k) = map(int, input().split(' '))
k -= 1
g = [[0 for x in range(n)] for y in range(n)]
cnt = k * n + 1
op = 1
for i in range(n):
	for j in range(k):
		g[i][j] = op
		op += 1
	for j in range(k, n):
		g[i][j] = cnt
		cnt += 1
sum = 0
for i in range(n):
	sum += g[i][k]
print(sum)
for i in range(n):
	if i != 0:
		print()
	for j in range(n):
		if j != 0:
			print(end=' ')
		print(g[i][j], end='')


(n, k) = map(int, input().split())
a = []
for i in range(n):
	a.append([0] * n)
st = 1
for i in range(k - 1):
	for j in range(n):
		a[j][i] = st
		st += 1
for i in range(n):
	for j in range(k - 1, n):
		a[i][j] = st
		st += 1
ans = 0
for i in range(n):
	ans += a[i][k - 1]
print(ans)
for i in range(n):
	for j in range(n):
		if j == n - 1:
			print(a[i][j], end='\n')
		else:
			print(a[i][j], end=' ')


(n, k) = map(int, input().split())
a = [0] * n
for i in range(n):
	a[i] = [0] * n
for i in range(n):
	for j in range(k):
		a[i][j] = i * (k - 1) + j + 1
for i in range(n):
	for j in range(k - 1, n):
		a[i][j] = n * (k - 1) + (j - k + 2) + i * (n - k + 1)
ans = 0
for i in range(n):
	ans += a[i][k - 1]
print(ans)
for elem in a:
	print(' '.join(map(str, elem)))


(n, k) = map(int, input().split())
c = 0
ans = [[0 for i in range(n)] for j in range(n)]
for i in range(n):
	for j in range(k - 1):
		c += 1
		ans[i][j] = c
final = 0
for i in range(n):
	for j in range(k - 1, n):
		c += 1
		ans[i][j] = c
	final += ans[i][k - 1]
print(final)
for i in ans:
	print(*i)


(n, k) = map(int, input().split())
a = [[0] * n for p in range(0, n)]
c = 1
for i in range(0, n):
	for j in range(0, k - 1):
		a[i][j] = c
		c += 1
sm = 0
for i in range(0, n):
	for j in range(k - 1, n):
		a[i][j] = c
		c += 1
		if j == k - 1:
			sm += a[i][j]
print(sm)
for i in range(0, n):
	for j in range(0, n):
		print(a[i][j], end=' ')
	print('')


(n, column) = map(int, input().split())
(small, large) = (1, n ** 2)
table = [[[] for i in range(n)] for j in range(n)]
for i in range(n):
	for j in range(n - 1, column - 2, -1):
		table[i][j] = large
		large -= 1
	for x in range(column - 1):
		table[i][x] = small
		small += 1
total = 0
for k in table:
	total += k[column - 1]
print(total)
for l in table:
	print(' '.join(list(map(str, l))))


n = 5
k = 3
(n, k) = map(int, input().split())
sm = 1
lg = n ** 2
sum = (n ** 2 - (n - k) + n ** 2 - (n - k) - (n - 1) * (n - k + 1)) * n // 2
print(sum)
for i in range(n):
	for j in range(1, k):
		print(sm, end=' ')
		sm += 1
	for j in range(k, n + 1):
		print(lg - n + j, end=' ')
	lg = lg - (n - k + 1)
	print()


(n, k) = [int(x) for x in input().split(' ')]
ans = 0
y = n * (k - 1) + 1
x = 1
barr = []
for i in range(n):
	arr = []
	for j in range(k - 1):
		arr.append(x)
		x += 1
	for j in range(k - 1, n):
		arr.append(y)
		y += 1
	ans += arr[k - 1]
	barr.append(' '.join(map(str, arr)))
print(ans)
print('\n'.join(barr))


def inp():
	return map(int, stdin.readline().split())
from sys import *
(n, m) = inp()
(c2, arr, c3, sum) = (n * (m - 1), '', 0, 0)
for i in range(n):
	arr += ' '.join([str(c3 + j) for j in range(1, m)]) + ' ' + ' '.join([str(c2 + j) for j in range(1, n - m + 2)]) + '\n'
	sum += c2 + 1
	c3 += m - 1
	c2 += n - (m - 1)
print(sum, arr, sep='\n')


import collections
import math

def is_prime(x):
	for i in range(2, math.ceil(math.sqrt(x))):
		if x % i == 0:
			return False
	return True
(n, k) = map(int, input().split())
T = [[0] * n for _ in range(n)]
ans = 0
s = n * n
for i in range(n):
	for j in range(n - k + 1):
		T[i][n - j - 1] = s
		s -= 1
a = 1
for i in range(n):
	for j in range(k - 1):
		T[i][j] = a
		a += 1
for i in range(n):
	ans += T[i][k - 1]
print(ans)
for i in range(n):
	print(' '.join(map(str, T[i])))


(n, k) = map(int, input().split())
ans = n * n - (n - k)
test = ans
op = [n * n - (n - k)]
op2 = []
count = []
for i in range(1, n):
	ans += test - i * (n - k + 1)
	op.append(test - i * (n - k + 1))
print(ans)
for i in range(n):
	count.append(k + i * n)
pos = 1
j = 1
op = sorted(op)
if n == 1 and k == 1:
	print('1')
elif k == 1:
	for i in range(n):
		print(op[i], end=' ')
		for q in range(n - 2):
			print(op[i] + q + 1, end=' ')
		print(op[i] + n - 1)
elif k == n:
	for i in range(n):
		for q in range(i * (n - 1) + 1, (i + 1) * (n - 1) + 1):
			print(q, end=' ')
		print(op[i])
else:
	while j <= (k - 1) * n:
		for i in range(k - 1):
			print(j, end=' ')
			pos += 1
			j += 1
		print(op[pos // n], end=' ')
		now = op[pos // n]
		pos += 1
		for i in range(k + 1, n):
			now += 1
			print(now, end=' ')
			pos += 1
		print(now + 1)
		pos += 1


(n, k) = map(int, input().split())
ans = [[0 for i in range(n)] for j in range(n)]
cur = 1
sum_k = 0
for i in range(n):
	for j in range(k - 1):
		ans[i][j] = cur
		cur += 1
for i in range(n):
	for j in range(k - 1, n):
		ans[i][j] = cur
		cur += 1
for i in range(n):
	sum_k += ans[i][k - 1]
print(sum_k)
for i in range(n):
	for j in range(n):
		print(ans[i][j], end=' ')
	print('')


for t in range(1):
	(n, k) = map(int, input().split())
	number = []
	for i in range(1, n ** 2 + 1):
		number.append(i)
	ans = []
	small_a = number[0:n * (k - 1)]
	for i in range(n):
		row = [0] * n
		for j in range(n - 1, -1, -1):
			if j + 1 >= k:
				a = number.pop(-1)
			else:
				a = small_a.pop(-1)
			row[j] = a
		ans.append(row)
	suma = 0
	for i in ans:
		ele = i[k - 1]
		suma += ele
	print(suma)
	for i in ans:
		print(*i)


(N, K) = map(int, input().split())
mat = [[0] * N for _ in range(N)]
for i in range(N):
	for j in range(K - 1):
		mat[i][j] = i * (K - 1) + j + 1
for i in range(N):
	for j in range(N - K + 1):
		mat[i][j + K - 1] = N * (K - 1) + i * (N - K + 1) + j + 1
print(sum((mat[i][K - 1] for i in range(N))))
[print(*row) for row in mat]


from collections import defaultdict, deque, Counter, OrderedDict
import threading, sys

def main():
	(n, k) = map(int, input().split())
	a = [[0 for i in range(n)] for j in range(n)]
	c = 1
	k -= 1
	for i in range(n):
		for j in range(k):
			a[i][j] = c
			c += 1
	ans = 0
	for i in range(n):
		for j in range(k, n):
			if j == k:
				ans += c
			a[i][j] = c
			c += 1
	print(ans)
	for i in a:
		print(' '.join(map(str, i)))
main()


(n, k) = map(int, input().split())
matrix = [[0 for i in range(n)] for j in range(n)]
cur_number = 1
for i in range(n):
	for j in range(k - 1):
		matrix[i][j] = cur_number
		cur_number += 1
for i in range(n):
	for j in range(k - 1, n):
		matrix[i][j] = cur_number
		cur_number += 1
s = 0
for i in range(n):
	s += matrix[i][k - 1]
print(s)
for i in range(n):
	print(' '.join(map(str, matrix[i])))


(n, k) = map(int, input().split())
a = []
for i in range(1, n * (k - 1) + 1):
	a.append(i)
a = a[::-1]
lst = []
for i in range(n * (k - 1) + 1, n * n + 1):
	lst.append(i)
lst = lst[::-1]
s = 0
ans = ''
for i in range(1, n + 1):
	for j in range(1, n + 1):
		if j < k:
			ans += str(a.pop()) + ' '
		else:
			if j == k:
				s += lst[-1]
			ans += str(lst.pop()) + ' '
	ans += '\n'
print(s)
print(ans)


(n, k) = map(int, input().split())
matr = [[0] * n for i in range(n)]
p = 1
for i in range(n):
	for j in range(k - 1):
		matr[i][j] = p
		p += 1
for i in range(n):
	for j in range(k - 1, n):
		matr[i][j] = p
		p += 1
st = 0
for i in range(n):
	st += matr[i][k - 1]
print(st)
for i in range(n):
	for j in range(n):
		print(matr[i][j], end=' ')
	print()


(n, k) = map(int, input().split())
small = 1
large = n ** 2
print((n ** 2 - (n - k) + n ** 2 - (n - k) - (n - 1) * (n - k + 1)) * n // 2)
for i in range(n):
	for j in range(1, k):
		print(small, end=' ')
		small += 1
	for j in range(k, n + 1):
		print(large - (n - j), end=' ')
	large -= n - k + 1
	print()


inp = input()
inp = inp.split()
n = int(inp[0])
k = int(inp[1]) - 1
ans = 0
count = 1
table = [[0] * n for i in range(n)]
for i in range(k):
	for j in range(n):
		table[j][i] = count
		count = count + 1
for i in range(n):
	for j in range(k, n):
		table[i][j] = count
		count = count + 1
for i in range(n):
	ans = ans + table[i][k]
print(ans)
for i in table:
	for j in i:
		print(j, end=' ')
	print()


s = input().split()
n = int(s[0])
k = int(s[1])
x = (k - 1) * n + 1
ans = x * n + (n - k + 1) * (n - 1) * n / 2
print(int(ans))
ind1 = 1
ind2 = x
for i in range(1, n + 1):
	for j in range(1, n + 1):
		if j < k:
			print(ind1, end=' ')
			ind1 += 1
		else:
			print(ind2, end=' ')
			ind2 += 1
	print()


(n, k) = map(int, input().split())
m = 1
q = n * (k - 1)
q += 1
su = n / 2 * (2 * q + (n - 1) * (n - k + 1))
print(int(su))
for i in range(n):
	for j in range(n):
		if j < k - 1:
			print(m, '', end='')
			m += 1
		elif j >= k - 1:
			print(q, '', end='')
			q += 1
	print('')


(n, k) = map(int, input().split())
t = n - k + 1
maxi = n ** 2 - t + 1
upper_limit = maxi - t * (n - 1)
k -= 1
a = 1
print(n * (2 * maxi - t * (n - 1)) // 2)
for i in range(n):
	flag = True
	c = maxi
	for j in range(n):
		if flag and j != k:
			print(a, end=' ')
			a += 1
		else:
			print(c, end=' ')
			c += 1
			flag = False
	print('')
	maxi -= t


(n, k) = map(int, input().strip().split(' '))
i1 = 1
i2 = n * (k - 1) + 1
l = i2 + (n - 1) * (n - k + 1)
print(n * (i2 + l) // 2)
for i in range(1, n + 1):
	for j in range(1, n + 1):
		if j < k:
			print(i1 + n * (j - 1), end=' ')
		else:
			print(i2, end=' ')
			i2 += 1
	i1 += 1
	print()


def kspecial(n, k):
	j = n - k + 1
	return n * n * n - n * (n + 1) // 2 * j + n

def print_table(n):
	i1 = iter(range(1, n * (k - 1) + 1))
	i2 = iter(range(n * (k - 1) + 1, n * n + 1))
	for r in range(n):
		l = []
		l.extend((next(i1) for _ in range(k - 1)))
		l.extend((next(i2) for _ in range(n - k + 1)))
		print(' '.join(map(str, l)))
(n, k) = map(int, input().split())
print(kspecial(n, k))
print_table(n)


(N, k) = map(int, input().split())
a = [[0] * (N + 1) for i in range(N + 1)]
x = N * (k - 1) + 1
S = 0
for i in range(1, N + 1):
	S += x
	a[i][k] = x
	x += 1
	for j in range(k + 1, N + 1):
		a[i][j] = x
		x += 1
x = 1
for i in range(1, N + 1):
	for j in range(1, k):
		a[i][j] = x
		x += 1
print(S)
for i in range(1, N + 1):
	print(*a[i][1:])


(n, k) = map(int, input().split())
ans = [[0] * n for _ in range(n)]
b = 1
for i in range(n):
	for j in range(k - 1):
		ans[i][j] = b
		b += 1
for i in range(n):
	for j in range(k - 1, n):
		ans[i][j] = b
		b += 1
rans = 0
for i in range(n):
	rans += ans[i][k - 1]
print(rans)
for i in range(n):
	print(*ans[i])