#include <bits/stdc++.h>
using namespace std;
clock_t __stt;
inline void TStart() { __stt = clock(); }
inline void TReport() {
  cerr << fixed << setprecision(3) << "\nCurrent Case Taken Time : "
       << (double)(clock() - __stt) / CLOCKS_PER_SEC << "sec\n";
}
template <typename T>
T MIN(T a, T b) {
  return a < b ? a : b;
}
template <typename T>
T MAX(T a, T b) {
  return a > b ? a : b;
}
template <typename T>
T ABS(T a) {
  return a > 0 ? a : (-a);
}
template <typename T>
void UMIN(T &a, T b) {
  if (b < a) a = b;
}
template <typename T>
void UMAX(T &a, T b) {
  if (b > a) a = b;
}
char readch() {
  char c = getchar();
  while (c == ' ' || c == '\n') c = getchar();
  return c;
}
int n, m, c[4005][4005], segs;
int w(int l, int r) {
  return (c[r][r] - (l ? (c[l - 1][r] + c[r][l - 1] - c[l - 1][l - 1]) : 0)) /
         2;
}
pair<long long, int> dp[4005];
int deq[4005], pl, pr, EXT;
pair<long long, int> eval(int s, int t) {
  return make_pair(dp[s].first + (long long)w(s, t - 1) + (long long)EXT,
                   dp[s].second - 1);
}
int overtake(int A, int B) {
  int R = n + 1, i;
  for (i = 12; i >= 0; --i) {
    int nR = R - (1 << i);
    if (nR > A && nR > B && eval(A, nR) < eval(B, nR)) R = nR;
  }
  return R;
}
long long check(int ext) {
  int i, j, k;
  EXT = ext;
  dp[0] = make_pair(0ll, 0);
  pl = 0;
  pr = 1;
  deq[0] = 0;
  for (i = 1; i <= n; ++i) {
    while (pr > pl + 1) {
      if (eval(deq[pl], i) >= eval(deq[pl + 1], i))
        ++pl;
      else
        break;
    }
    dp[i] = eval(deq[pl], i);
    while (pr > pl + 1) {
      if (overtake(i, deq[pr - 1]) <= overtake(deq[pr - 1], deq[pr - 2]))
        --pr;
      else
        break;
    }
    if (eval(i, n) < eval(deq[pr - 1], n)) deq[pr++] = i;
  }
  segs = -dp[n].second;
  return dp[n].first;
}
int main() {
  int i, j, k;
  scanf("%d%d", &n, &m);
  for (i = 0; i < n; ++i) {
    for (j = 0; j < n; ++j) {
      c[i][j] = readch() - '0';
    }
  }
  for (i = 0; i < n; ++i) {
    for (j = 1; j < n; ++j) {
      c[i][j] += c[i][j - 1];
    }
  }
  for (i = 1; i < n; ++i) {
    for (j = 0; j < n; ++j) {
      c[i][j] += c[i - 1][j];
    }
  }
  int low = 0, high = 2e8, mid;
  long long res = 0ll;
  while (low < high) {
    mid = (low + high + 1) / 2;
    long long R = check(mid);
    if (segs < m)
      high = mid - 1;
    else {
      low = mid;
      UMAX(res, R);
    }
  }
  printf("%lld\n", res - (long long)low * (long long)m);
  return 0;
}
