First look at the hanging line method: "On the use of great ideas to solve the biggest problem of sub-matrices"
For this question, barrier point is the current color to the same point, the other did as a template to do just fine.
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace std; int n,m,bd[2010][2010],l[2010][2010],r[2010][2010],ht[2010][2010]; int main() { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) for( Int J = . 1 ; J <= m; J ++ ) { Scanf ( " % D " , & BD [I] [J]); L [I] [J] = J; R & lt [I] [J] = J ; HT [I] [J] = . 1 ; // to suspension wires ij initial value } for ( int I = . 1 ; I <= n-; I ++ ) for ( int J = 2 ; J <= m; J ++ ) IF (! BD [I] [J] BD = [I] [J- . 1 ]) L [I] [J] = L [I] [J- . 1 ]; // suspension wires may extend leftward ij for(int i=1;i<=n;i++) for(int j=m-1;j>=1;j--) if(bd[i][j]!=bd[i][j+1]) r[i][j]=r[i][j+1]; //悬线ij向右延伸 for(int i=2;i<=n;i++) for(int j=1;j<=m;j++) if(bd[i][j]!=bd[i-1][j]) { ht[i][j]HT = [I- . 1 ] [J] + . 1 ; // suspension wires extending upwardly ij L [I] [J] = max (L [I] [J], L [I- . 1 ] [J]); / / suspended line interval ij shrinkage left R & lt [I] [J] = min (R & lt [I] [J], R & lt [I- . 1 ] [J]); // shrink the right section } int ANS1 = 0 , ANS2 = 0 ; for ( int I = . 1 ; I <= n-; I ++ ) for ( int J = . 1 ; J <= m; J ++ ) { int A = R & lt [I] [J] -l [I] [J] + . 1 , B = HT [I] [J]; //Ij corresponding to the width of the rectangle of the suspension wires extend around the suspension wires ij distance, high-ij extends upwardly a distance of ANS1 = max (ANS1, min (A, B) * min (A, B)); // the corresponding area of the square ans2 = max (ANS2, A * B); // rectangular area } the printf ( " % D \ D n-% \ n- " , ANS1, ANS2); return 0 ; }