#include <bits/stdc++.h>
using namespace std;
const int N = 505, M = 300005;
int a[N][N];
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  int res = n * k - n + 1, rp = n - k + 1;
  int ans = 0;
  int k1 = 0;
  for (int i = (1); i <= (n); i++) {
    ans += res;
    for (int j = (1); j <= (k - 1); j++) a[i][j] = ++k1;
    for (int j = (k); j <= (n); j++) a[i][j] = res++;
  }
  printf("%d\n", ans);
  for (int i = (1); i <= (n); i++) {
    for (int j = (1); j <= (n); j++) printf("%d ", a[i][j]);
    puts("");
  }
  return 0;
}
