#include <bits/stdc++.h>
using namespace std;
using REAL = long double;
using LONG = long long;
int n, m, c[4004][4004], a[4004][4004], d[4004][2], opt[4004][2];
inline void compute(int l, int r, int k, int L, int R) {
  if (l > r) {
    return;
  }
  int i = (l + r) >> 1;
  d[i][k & 1] = -1;
  for (int j = min(i, R); j >= max(k, L); --j) {
    if (d[i][k & 1] == -1 || d[i][k & 1] > d[j - 1][(k + 1) & 1] + a[j][i]) {
      d[i][k & 1] = d[j - 1][(k + 1) & 1] + a[j][i];
      opt[i][k & 1] = j;
    }
  }
  compute(l, i - 1, k, L, opt[i][k & 1]);
  compute(i + 1, r, k, opt[i][k & 1], R);
}
int main() {
  scanf("%d %d", &n, &m);
  getchar();
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      c[i][j] = c[i][j - 1] + getchar() - '0';
      getchar();
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = i; j <= n; ++j) {
      a[i][j] = a[i][j - 1] + c[j][j] - c[j][i - 1];
    }
  }
  for (int i = 1; i <= n; ++i) {
    d[i][1] = a[1][i];
    opt[i][1] = 1;
  }
  for (int k = 2; k <= m; ++k) {
    compute(k, n, k, k, n);
  }
  cout << d[n][m & 1] << "\n";
  return 0;
}
