public static void main(String[] args) {
int[][] matrix = { { -1, -2, -3 }, { 4, 5, -6 }, { -7, -8, -9 } };
int n = 3;
int sum[][][] = new int[n][n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
sum[i][j][j] = matrix[i][j];
}
for (int j = 1; j < n; j++) {
for (int k = 0; k < n - j; k++) {
//dp
sum[i][k][k + j] = sum[i][k][k] + sum[i][k + 1][k + j];
}
}
}
Integer Max = null;
// find the biggest
int a1 = 0, a2 = 0, b1 = 0, b2 = 0;
for (int b = 0; b < n; b++) {
for (int c = b; c < n; c++) {
int s = 0;
Integer max = null;
int maxs = 0, maxe = 0;
int start = 0;
for (int a = 0; a < n; a++) {
s += sum[a][b][c];
if (max == null || s > max) {
max = s;
maxs = start;
maxe = a;
}
if (s <= 0) {
start = a + 1;
s = 0;
}
}
if (Max == null || max > Max ) {
Max = max;
a1 = maxs;
a2 = maxe;
b1 = b;
b2 = c;
}
}
}
System.out.println("row " + a1 + "-" + a2);
System.out.println("col " + b1 + "-" + b2);
System.out.println("sum " + Max);
}