#include <bits/stdc++.h>
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  int p1 = 1, p2 = n * n - (n - k + 1) * n + 1;
  int res = 0;
  for (int i = n; i > 0; i--) {
    res += n * n - i * (n - k + 1) + 1;
  }
  printf("%d\n", res);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      printf("%d%s", (j < k) ? p1++ : p2++, (j == n) ? "\n" : " ");
    }
  }
  return 0;
}
