#include <bits/stdc++.h>
using namespace std;
long long powmod(long long a, long long b, long long MOD) {
  long long res = 1;
  a %= MOD;
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % MOD;
    a = a * a % MOD;
  }
  return res;
}
const int maxn = 4005;
const int INF = INT_MAX;
int dp[maxn][maxn], s[maxn][maxn], a[maxn][maxn];
char buf[maxn][maxn * 2];
int n, k;
inline int cost(int l, int r) {
  return a[r][r] - a[r][l - 1] - a[l - 1][r] + a[l - 1][l - 1];
}
int main() {
  scanf("%d%d\n", &n, &k);
  for (int i = 1; i < n + 1; ++i) gets(buf[i]);
  for (int i = 1; i < n + 1; ++i)
    for (int j = 1; j < n + 1; ++j) {
      a[i][j] = buf[i][(j - 1) << 1] - '0';
      a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1];
    }
  for (int i = 1; i < n + 1; ++i) {
    dp[0][i] = cost(1, i);
    s[0][i] = 0;
  }
  for (int i = 1; i < k; ++i)
    for (int j = n + 1 - 1; j >= 1; --j) {
      s[i][n + 1] = n;
      dp[i][j] = INF;
      for (int t = s[i - 1][j]; t < s[i][j + 1] + 1; ++t) {
        if (dp[i][j] > dp[i - 1][t] + cost(t + 1, j)) {
          dp[i][j] = dp[i - 1][t] + cost(t + 1, j);
          s[i][j] = t;
        }
      }
    }
  printf("%d\n", dp[k - 1][n] >> 1);
  return 0;
}
