#include <bits/stdc++.h>
using namespace std;
const int N = 510;
int n, k;
int mat[N][N];
int res;
int main() {
  cin >> n >> k;
  int d = (n - k);
  res = mat[n][k] = n * n - d;
  for (int i = n - 1; i; i--) {
    mat[i][k] = mat[i + 1][k] - d - 1;
    res += mat[i][k];
  }
  for (int i = 1; i <= n; i++)
    for (int j = k + 1; j <= n; j++) mat[i][j] = mat[i][j - 1] + 1;
  int u = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j < k; j++) mat[i][j] = u++;
  cout << res << endl;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) cout << mat[i][j] << ' ';
    cout << '\n';
  }
}
