#include <bits/stdc++.h>
using namespace std;
int N, K;
int sum[4005][4005];
int dp[4005][4005];
int C(int l, int r) {
  return (sum[r][r] - sum[r][l - 1] - sum[l - 1][r] + sum[l - 1][l - 1]) / 2;
}
int solveDp(int i, int j, int optl, int optr) {
  int kopt = -1;
  dp[i][j] = 1e9;
  for (int k = optl; k <= min(optr, j - 1); k++) {
    int v = dp[i - 1][k] + C(k + 1, j);
    if (dp[i][j] > v) {
      dp[i][j] = v;
      kopt = k;
    }
  }
  return kopt;
}
void solve(int i, int l, int r, int optl, int optr) {
  if (l > r) return;
  if (l == r) {
    solveDp(i, l, optl, optr);
    return;
  }
  int mid = (l + r) / 2;
  int kopt = solveDp(i, mid, optl, optr);
  solve(i, l, mid - 1, optl, kopt);
  solve(i, mid + 1, r, kopt, optr);
}
int main() {
  scanf("%d%d", &N, &K);
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= N; j++) {
      char c = '#';
      while (!isdigit(c)) {
        c = getchar();
      }
      sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + c - '0';
    }
  for (int j = 1; j <= N; j++) dp[0][j] = 1e9;
  for (int i = 1; i <= K; i++) solve(i, 1, N, 0, N);
  printf("%d", dp[K][N]);
  return 0;
}
