#include <bits/stdc++.h>
using namespace std;
const int N = 4001;
const int K = 801;
int dp[K][N], opt[K][N], sum[N][N];
int get(int l, int r) {
  return (sum[r][r] - sum[l - 1][r] - sum[r][l - 1] + sum[l - 1][l - 1]) / 2;
}
void compute_dp(int level, int i, int optl, int optr) {
  for (int j = optl; j <= min(i, optr); ++j) {
    if (dp[level][i] > dp[level - 1][j - 1] + get(j, i)) {
      dp[level][i] = dp[level - 1][j - 1] + get(j, i);
      opt[level][i] = j;
    }
  }
}
void compute(int level, int l, int r, int optl, int optr) {
  if (l > r) return;
  int m = l + r >> 1;
  compute_dp(level, m, optl, optr);
  compute(level, l, m - 1, optl, opt[level][m]);
  compute(level, m + 1, r, opt[level][m], optr);
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<vector<int>> c(n + 1, vector<int>(n + 1));
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      cin >> c[i][j];
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + c[i][j];
    }
  }
  const int inf = 2e9;
  for (int i = 0; i <= k; ++i)
    for (int j = 0; j <= n; ++j) dp[i][j] = inf, opt[i][j] = 1;
  dp[0][0] = 0;
  for (int level = 1; level <= k; ++level) compute(level, level, n, 1, n);
  cout << dp[k][n];
  return 0;
}
