Code:

#include <stdio.h>
#include <stdlib.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define libSize 20
#define libPointSize 20
#define libTimeSize 30
int n, k;
int lib[libSize][libSize];
int markLib[libPointSize][libPointSize][libPointSize][libPointSize][libTimeSize];
void constructor()
{
FILE *fr;
int a, b, c, d, e;
fr = fopen("library.dat", "+r");
fscanf(fr, "%d %d", &n, &k);
printf("haha");
for (a = 0; a < libSize; a++)
for (b = 0; b < libSize; b++)
lib[a][b] = 0;
for (a = 0; a < libPointSize; a++)
for (b = 0; b < libPointSize; b++)
for (c = 0; c < libPointSize; c++)
for (d = 0; d < libPointSize; d++)
for (e = 0; e < libTimeSize; e++)
markLib[a][b][c][d][e] = 10000;
for (a = 0; a < n; a++)
for (b = 0; b < n; b++)
fscanf(fr, "%d", &lib[a][b]);
fclose(fr);
}
int areaValue(int p1X, int p1Y, int p2X, int p2Y)
{
int i, j, totalValue;
totalValue = 0;
for (i = p1X; i < p2X; i++)
for (j = p1Y; j < p2Y; j++)
totalValue += (int)lib[i][j];
return totalValue;
}
/* data for minCutting function */
int a, b, time;
/* spX = start point X; epY = end point Y */
int minValue(int spX, int spY, int epX, int epY, int freq)
{
if ((markLib[spX][spY][epX][epY][freq] > 0) ||
(markLib[spX][spY][epX][epY][freq] <= 200000000))
return markLib[spX][spY][epX][epY][freq];
else {
if (freq == 0)
markLib[spX][spY][epX][epY][freq] = areaValue(spX, spY, epX, epY);
else {
for (a = spX+1; a < epX; a++)
for (time = 0; time < freq; time++)
markLib[spX][spY][epX][epY][freq] =
min(markLib[spX][spY][epX][epY][freq],
max(minValue(a, spY, epX, epY, freq-time-1),
minValue(spX, spY, a, epY, time)));
for (a = spY+1; a < epY; a++)
for (time = 0; time < freq; time++)
markLib[spX][spY][epX][epY][freq] =
min(markLib[spX][spY][epX][epY][freq],
max(minValue(spX, spY, epX, a, freq-time-1),
minValue(spX, a, epX, epY, time)));
return markLib[spX][spY][epX][epY][freq];
}
}
}
int main()
{
FILE *fw;
int result;
constructor();
result = minValue(0, 0, n-1, n-1, k);
fw = fopen("library.out", "+w");
fprintf(fw, "%d\n", result);
fclose(fw);
return 0;
}

library.dat