#include <bits/stdc++.h>
using namespace std;
template <class T>
bool ckmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
namespace debug {
void __print(int x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto z : x) cerr << (f++ ? "," : ""), __print(z);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
}  // namespace debug
using namespace debug;
const char nl = '\n';
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
int a[4005][4005];
int dp[4005][1005];
void compute(int i, int l, int r, int lk, int rk) {
  if (l > r) return;
  int mid = (l + r) / 2;
  pair<int, int> naj{1e9, 1e9};
  for (int k = lk; k <= min(mid, rk); ++k) {
    int val = dp[k - 1][i - 1] +
              (a[mid][mid] - a[k - 1][mid] - a[mid][k - 1] + a[k - 1][k - 1]);
    ckmin(naj, {val, k});
  }
  dp[mid][i] = naj.first;
  compute(i, l, mid - 1, lk, naj.second);
  compute(i, mid + 1, r, naj.second, rk);
}
void solve() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      cin >> a[i][j];
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      a[i][j] = a[i][j] + a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
    }
  }
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= k; ++j) {
      dp[i][j] = 1e9;
    }
  }
  dp[0][0] = 0;
  for (int i = 1; i <= k; ++i) {
    compute(i, 1, n, 1, n);
  }
  cout << dp[n][k] / 2 << nl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int testCases = 1;
  while (testCases--) solve();
}
