title: Biggest square submatrix with 0s inside and 1s outside
body: <p><strong>Task:</strong> I need to make a method that will find biggest <em>square submatrix</em> with <em>1s in border</em> and <em>0s inside</em>, from matrix(2d array) that could be square but doesn't need to be. All elements of matrix are 1s and 0s.</p>
  
  <p>This is my code</p>
  
  <pre><code>static void sub(int[][] p){
      int sm=(p[0].length&lt;p.length)?p[0].length:p.length;
      int bg=(p[0].length&lt;p.length)?p.length:p[0].length;
      if(p.length==p[0].length){
          sm=p.length;bg=p.length;
      }
      int t=0;
      boolean cool=false;
  
      z:for(int z=sm;z&gt;2;z--){
          x:for(int x=0,l=0;x&lt;sm-z;x++,l++){
              y:for(int y=0,m=0;y&lt;bg-z;y++,m++){
                  for (int i=y;i&lt;=z+m;i++){
                      if(p[x][i]!=1){cool=false; continue x;}
                      else cool=true;
                      if(p[z][i]!=1){cool=false; continue x;}
                      else cool=true;
                  }
                  int n=0;
                  for(int j=0;j&lt;z-1;j++)
                  for(int i=y+1;i&lt;z+m;i++){
                      if(p[x+n][i]!=0){cool=false; continue x;}
                      else cool=true;
                      if(i==z+m-1)n++;
                  }
                  for (int i=x+1;i&lt;z+l;i++){
                      if(p[i][y]!=1){cool=false; continue x;}
                      else cool=true;
                  }
                  for(int i=x+1;i&lt;=z-1;i++){
                      if(p[i][z+t]!=1) continue x;
                  }
                  if(cool){
                      System.out.println(x+" "+y+" "+z);
                  }
                  t++;
              }
              t=0;
          }
      }
  }
  public static void main(String[] args) {
      int[][] p = {
              {1,1,1,1,1,1,1,1,1},
              {1,0,0,0,1,1,1,1,1},
              {1,0,0,0,1,1,1,1,1},
              {1,0,0,0,1,1,1,1,1},
              {1,0,0,0,1,1,1,1,1},
              {1,0,0,0,1,1,1,1,1},
              {1,1,1,1,1,1,1,1,1}
      };
      sub(p);
  }
  </code></pre>
  
  <p><strong>Variables:</strong> x and y - starting x and y coordinate (p[x][y])
                 z - length of square submatrix</p>
  
  <p>Where's mistake. Why I don't get those x, y and z for this example. I have tested all these for loops they take elements that they should. And if you have some advice, some better way I would like to know. Thanks!</p>