Language: C++

/**
 *    author:  math2do
 *    created: 22.03.2020 23:45:37
**/

#include <bits/stdc++.h>

using namespace std;

template <typename A, typename B>
string to_string(pair<A, B> p);
 
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
 
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
 
string to_string(const string& s) {
  return '"' + s + '"';
}
 
string to_string(const char* s) {
  return to_string((string) s);
}
 
string to_string(bool b) {
  return (b ? "1" : "0");
}
string to_string(const char &ch) {
  return string(1, ch);
}
 
string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}
 
template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  reverse(res.begin(), res.end());
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
 
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
 
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}
 
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

template <typename T> 
string to_string(vector <vector<T>> &mat) {
  string res = "";
  char temp[32];
  int n = mat.size();
  if (n == 0) return res;
  res += "\n";
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < (int) mat[i].size(); j++) {
      sprintf(temp, "%-2s", to_string(mat[i][j]).c_str());
      res += string(temp);
    }
    res += "\n";
  }
  return res;
}


void debug_out() { cerr << endl; }
 
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
 
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#endif

template <typename T>
vector <vector <T>> subset(vector <T>& arr) {
  int n = arr.size();
  int total = ((1 << n) - 1);
  vector <vector <T>> ss(total);

  for (int mask = 1; mask <= total; mask++) {
    for (int sh = 0; sh < n; sh++) {
      if ((mask & (1 << sh)) != 0) {
        ss[mask - 1].push_back(arr[sh]);
      } 
    }
  }
  ss.push_back(vector <T>());
  return ss;
}

template <typename T>
class cum {
 public:
  int n, m;
  vector <vector <T>> mat;
  cum(vector <vector <T>> &_mat) {
    n = _mat.size();
    m = _mat[0].size();
    mat = vector <vector <T>>(n + 1, vector <T>(m + 1, 0));
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        mat[i + 1][j + 1] = _mat[i][j];
      } 
    } 
    for (int i = 1; i < n + 1; i++) {
      for (int j = 1; j < m + 1; j++) {
        mat[i][j] += mat[i - 1][j] + mat[i][j - 1] - mat[i - 1][j - 1];
      } 
    }
  }
  T get(int x, int y) {
    return get(0, 0, x, y);
  } 
  T get(int p, int q, int r, int s) {
    p += 1, q += 1, r += 1, s += 1;
    return mat[r][s] - mat[r][q - 1] - mat[p - 1][s] + mat[p - 1][q - 1];
  } 
};

template <typename T> 
inline bool smin(T &a, const T &b) {
  return a > b ? a = b, true : false;
}

const int pinf = std::numeric_limits<int>::max();

int solve(int yy, vector <int> &vec, vector <vector <int>> &mat, cum <int> &c, int k, int n, int m) {
  int i = 0;
  vector <int> pos;
  for (int line : vec) {
    int j = line;
    for (int y = yy; y < m; y++) {
      int sum = c.get(i, yy, j, y);
      if (sum > k) {
        if (y == yy) return pinf;
        pos.push_back(y - 1);
        break;
      } 
    } 
    i = line + 1; 
  }
  debug(yy);
  debug(pos);
  if (pos.empty()) return 0;
  int new_y = *min_element(pos.begin(), pos.end());
  int ret = solve(new_y + 1, vec, mat, c, k, n, m);
  return ret == pinf ? pinf : ret + 1;
} 

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m, k;
  cin >> n >> m >> k;
  vector <vector <int>> mat(n, vector <int>(m));
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < m; j++) {
      mat[i][j] = s[j] - '0';
    } 
  }
  cum <int> c(mat);
  vector <int> s;
  for (int i = 0; i < n - 1; i++) {
    s.push_back(i);
  }
  debug(k);
  debug(mat);
  int ans = pinf;
  vector <vector <int>> ss = subset(s);
  //vector <vector <int>> ss = {{0, 1}};

  debug(ss.size());
  for (auto &vec : ss) {
    sort(vec.begin(), vec.end());
    vec.push_back(n - 1);
    debug(vec);
    int ret = solve(0, vec, mat, c, k, n, m);
    if (ret == pinf) continue;
    debug(vec, ret);
    smin(ans, ret + (int) vec.size() - 1);
  }
  cout << ans << '\n'; 
  return 0;
}


Compiler output:
a.cc: In function 'int solve(int, std::vector<int>&, std::vector<std::vector<int> >&, cum<int>&, int, int, int)':
a.cc:186:3: error: 'debug' was not declared in this scope
  186 |   debug(yy);
      |   ^~~~~
a.cc: In function 'int main()':
a.cc:212:3: error: 'debug' was not declared in this scope
  212 |   debug(k);
      |   ^~~~~