#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 10;
const long long inf = 1e18 + 10;
long long a[4005][4005];
long long dp_cur[4005], dp_prev[4005];
void solve(long long l, long long r, long long opl, long long opr) {
  if (l > r) return;
  long long mid = (l + r) >> 1;
  pair<long long, long long> best = {inf, -1};
  for (long long k = opl; k <= min(mid, opr); ++k) {
    long long c = a[mid][mid] + a[k][k] - a[k][mid] - a[mid][k];
    best = min(best, {dp_prev[k] + c, k});
  }
  dp_cur[mid] = best.first;
  solve(l, mid - 1, opl, best.second);
  solve(mid + 1, r, best.second, opr);
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  long long n, k;
  cin >> n >> k;
  for (long long i = 1; i <= n; ++i) {
    for (long long j = 1; j <= n; ++j) {
      char x;
      cin >> x;
      a[i][j] = x - '0';
      a[i][j] += a[i][j - 1];
    }
  }
  for (long long i = 1; i <= n; ++i) {
    for (long long j = 1; j <= n; ++j) {
      a[j][i] += a[j - 1][i];
    }
  }
  for (long long i = 1; i <= n; ++i) {
    dp_cur[i] = dp_prev[i] = a[i][i];
  }
  for (long long i = 2; i <= k; ++i) {
    solve(1, n, 1, n);
    for (long long j = 1; j <= n; ++j) {
      dp_prev[j] = dp_cur[j];
    }
  }
  cout << dp_cur[n] / 2 << endl;
}
