Package com.microsoft; public class Matrixutil {public static long Returnsumofmaxsubmatrix (int[][] matrix) throws Exception {int [][]index=n
EW int[4][2];
Index[0]=new int[]{0,0};
Index[1]=new int[]{0,matrix[0].length-1};
Index[2]=new int[]{matrix.length-1,0};
Index[3]=new int[]{matrix.length-1,matrix[0].length-1};
Return Recuisivemaxsubmatrix (Index,matrix); private static Long Recuisivemaxsubmatrix (int[][] index,int[][] Matrix {if (Index[0][0]==index[3][0]&&index [0]
[1]==index[3][1]) {return matrix[index[0][0]][index[0][1]];
int [][]topleft=new int[4][2];
TOPLEFT[0]=INDEX[0];
Topleft[1]=new int[]{index[0][0], (index[0][1]+index[1][1])/2};
Topleft[2]=new int[]{(index[0][0]+index[2][0])/2,index[0][1]};
Topleft[3]=new int[]{(index[0][0]+index[2][0])/2, (Index[0][1]+index[1][1])/2};
int [][]topright=new int[4][2];
Topright[0]=new int[]{index[0][0], (index[0][1]+index[1][1])/2+1};
Topright[1]=new Int[]{index[1][0],index[1][1]}; ToprIght[2]=new int[]{(index[0][0]+index[2][0])/2, (Index[0][1]+index[1][1])/2+1};
Topright[3]=new int[]{(index[0][0]+index[2][0])/2,index[1][1]};
int [][]bottomleft=new int[4][2];
Bottomleft[0]=new int[]{(index[0][0]+index[2][0])/2+1,index[0][1]};
Bottomleft[1]=new int[]{(index[0][0]+index[2][0])/2+1, (index[0][1]+index[1][1])/2};
BOTTOMLEFT[2]=INDEX[2];
Bottomleft[3]=new int[]{index[2][0], (index[2][1]+index[3][1])/2};
int [][]bottomright=new int[4][2];
Bottomright[0]=new int[]{(index[0][0]+index[2][0])/2+1, (index[0][1]+index[1][1])/2+1};
Bottomright[1]=new int[]{(index[0][0]+index[2][0])/2+1,index[1][1]};
Bottomright[2]=new int[]{index[2][0], (index[2][1]+index[3][1])/2+1};
BOTTOMRIGHT[3]=INDEX[3];
Long Max1=recuisivemaxsubmatrix (Topleft,matrix);
Long Max2=recuisivemaxsubmatrix (Topright,matrix);
Long Max3=recuisivemaxsubmatrix (Bottomleft,matrix);
Long Max4=recuisivemaxsubmatrix (Bottomright,matrix);
Long Maxtop=integer.min_value;
Long max=0; for (int x1=topleft[0][0];x1<=topleft[2][0];x1++) {for (int x2=x1;x2<=topleft[2][0];x2++) {for (int y1=topleft[0][1];y1<
=topleft[1][1];y1++) {for (int y2=topright[0][1];y2<=topright[1][1];y2++) {max=0;
for (int i=x1;i<=x2;i++) {for (int j=y1;j<=y2;j++) {max+=matrix[i][j];
} maxtop=math.max (Maxtop, Max);
}}} long Maxleft=long.min_value;
max=0; for (int x1=topleft[0][0];x1<=topleft[2][0];x1++) {for (int x2=bottomleft[0][0];x2<=bottomleft[2][0];x2++) {fo
R (int y1=topleft[0][1];y1<=topleft[1][1];y1++) {for (int y2=y1;y2<=topleft[1][1];y2++) {max=0;
for (int i=x1;i<=x2;i++) {for (int j=y1;j<=y2;j++) {max+=matrix[i][j];
} maxleft=math.max (Maxleft, Max);
}}} long Maxbottom=long.min_value;
max=0; for (int x1=bottomleft[0][0];x1<=bottomleft[2][0];x1++) {for (int x2=x1;x2<=bottomleft[2][0];x2++) {for (inty1=bottomleft[0][1];y1<=bottomleft[1][1];y1++) {for (int y2=bottomright[0][1];y2<=bottomright[1][1];y2++) {
max=0;
for (int i=x1;i<=x2;i++) {for (int j=y1;j<=y2;j++) {max+=matrix[i][j];
} maxbottom=math.max (Maxbottom, Max);
}}} long Maxright=long.min_value;
max=0; for (int x1=topright[0][0];x1<=topright[2][0];x1++) {for (int x2=bottomright[0][0];x2<=bottomright[2][0];x2++
{for (int y1=bottomright[0][1];y1<=bottomright[1][1];y1++) {for (int y2=y1;y2<=bottomright[1][1];y2++) {
max=0;
for (int i=x1;i<=x2;i++) {for (int j=y1;j<=y2;j++) {max+=matrix[i][j];
} maxright=math.max (Maxright, Max);
}}} long Maxfull=long.min_value;
max=0; for (int x1=topleft[0][0];x1<=topleft[2][0];x1++) {for (int x2=bottomleft[0][0];x2<=bottomleft[2][0];x2++) {fo
R (int y1=bottomleft[0][1];y1<=bottomleft[1][1];y1++) { for (int y2=bottomright[0][1];y2<=bottomright[1][1];y2++) {max=0;
for (int i=x1;i<=x2;i++) {for (int j=y1;j<=y2;j++) {max+=matrix[i][j];
} maxfull=math.max (Maxfull, Max);
}}} long Result=math.max (Max1, MAX2);
Result=math.max (result, max3);
Result=math.max (result, max4);
Result=math.max (result, maxtop);
Result=math.max (result, maxleft);
Result=math.max (result, maxbottom);
Result=math.max (result, maxright);
Result=math.max (result, maxfull);
return result; public static void Main (string[] args) throws Exception {int[][] matrix = {{9,-3, 7,-5}, {-3,-4, 2, 0}, {7
, 2, 2,-1}, {-5, 0,-1, 6}};
Long max = Returnsumofmaxsubmatrix (matrix);
System.out.println ("max =" + max); }
}