Language: C++

typedef pair<int,int> P;

int h,w,s,all,t[32][32];
int rec[32][32][32][32]; //長方形の合計[ly][lx][hy][hx]
P dp[32][32][32][32]; //メモ化

//全ての(lx,ly)〜(hx,hy)の長方形合計を計算
void mkRec(){
  memset(rec,0,sizeof(rec));

  rep(i,h){
    rep(j,w){
      rec[i][j][i][j] = t[i][j];

      for(int y=i-1;y>=0;y--)
        rec[y][j][i][j] = rec[y+1][j][i][j] + t[y][j];

      for(int y=i;y>=0;y--){
        int sum = 0;
        for(int x=j;x>=0;x--){
          sum += rec[y][x][i][x];
          rec[y][x][i][j] = sum;
        }
      }
    }
  }
}

//戻り型 : P. first : 最大グループ数. second : 最大予備力
P dfs(int lx,int ly,int hx,int hy){
  if(dp[ly][lx][hy][hx].first != 0) return dp[ly][lx][hy][hx];

  P res = P(1,s-(all-rec[ly][lx][hy][hx]));

  //水平分割
  for(int i=ly;i<hy;i++){
    if(all - rec[ly][lx][i][hx] > s ||
       all - rec[i+1][lx][hy][hx] > s) continue;
    P a = dfs(lx,ly,hx,i);
    P b = dfs(lx,i+1,hx,hy);
    P cal = P(a.first+b.first,min(a.second,b.second));
    res = max(res,cal);
  }

  //垂直分割
  for(int j=lx;j<hx;j++){
    if(all - rec[ly][lx][hy][j] > s ||
       all - rec[ly][j+1][hy][hx] > s) continue;
    P a = dfs(lx,ly,j,hy);
    P b = dfs(j+1,ly,hx,hy);
    P cal = P(a.first+b.first,min(a.second,b.second));
    res = max(res,cal);
  }

  return dp[ly][lx][hy][hx] = res;
}

int main(void){
  while(scanf("%d%d%d",&h,&w,&s),h||w||s){
    all = 0;
    rep(i,h){
      rep(j,w){
        scanf("%d",&t[i][j]);
        all += t[i][j];
      }
    }

    mkRec();

    memset(dp,0,sizeof(dp));
    P p = dfs(0,0,w-1,h-1);
    printf("%d %d\n",p.first,p.second);
  }
}

Compiler output:
a.cc:1:9: error: 'pair' does not name a type
    1 | typedef pair<int,int> P;
      |         ^~~~
a.cc:5:1: error: 'P' does not name a type
    5 | P dp[32][32][32][32]; //メモ化
      | ^
a.cc: In function 'void mkRec()':
a.cc:9:3: error: 'memset' was not declared in this scope
    9 |   memset(rec,0,sizeof(rec));
      |   ^~~~~~
a.cc:1:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
  +++ |+#include <cstring>
    1 | typedef pair<int,int> P;
a.cc:11:7: error: 'i' was not declared in this scope
   11 |   rep(i,h){
      |       ^
a.cc:11:3: error: 'rep' was not declared in this scope; did you mean 'rec'?
   11 |   rep(i,h){
      |   ^~~
      |   rec
a.cc: At global scope:
a.cc:30:1: error: 'P' does not name a type
   30 | P dfs(int lx,int ly,int hx,int hy){
      | ^
a.cc: In function 'int main()':
a.cc:59:9: error: 'scanf' was not declared in this scope
   59 |   while(scanf("%d%d%d",&h,&w,&s),h||w||s){
      |         ^~~~~
a.cc:61:9: error: 'i' was not declared in this scope
   61 |     rep(i,h){
      |         ^
a.cc:61:5: error: 'rep' was not declared in this scope; did you mean 'rec'?
   61 |     rep(i,h){
      |     ^~~
      |     rec
a.cc:70:12: error: 'dp' was not declared in this scope
   70 |     memset(dp,0,sizeof(dp));
      |            ^~
a.cc:70:5: error: 'memset' was not declared in this scope
   70 |     memset(dp,0,sizeof(dp));
      |     ^~~~~~
a.cc:70:5: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
a.cc:71:5: error: 'P' was not declared in this scope
   71 |     P p = dfs(0,0,w-1,h-1);
      |     ^
a.cc:72:22: error: 'p' was not declared in this scope
   72 |     printf("%d %d\n",p.first,p.second);
      |                      ^
a.cc:72:5: error: 'printf' was not declared in this scope
   72 |     printf("%d %d\n",p.first,p.second);
      |     ^~~~~~
a.cc:1:1: note: 'printf' is defined in header '<cstdio>'; this is probably fixable by adding '#include <cstdio>'
  +++ |+#include <cstdio>
    1 | typedef pair<int,int> P;