The output completely matches conditions.
The only thing i can think about is, every time i find a rectangle i set it's 1s to 0s.
But it doesn't say in conditions you can't change arrays.
Thanks.

package com.codegym.task.task20.task2026;
import java.util.Arrays;
/*
Rectangle algorithms
*/
public class Solution {
public static void main(String[] args) {
byte[][] a1 = new byte[][]{
{1, 1, 0, 0},
{1, 1, 0, 0},
{1, 1, 0, 0},
{1, 1, 0, 1}
};
byte[][] a2 = new byte[][]{
{1, 0, 0, 1},
{0, 0, 0, 0},
{0, 0, 0, 0},
{1, 0, 0, 1}
};
int count1 = getRectangleCount(a1);
System.out.println("count = " + count1 + ". Must be 2");
int count2 = getRectangleCount(a2);
System.out.println("count = " + count2 + ". Must be 4");
}
public static int getRectangleCount(byte[][] a) {
int rect = 0;
for(int i=0; i<a.length; i++ ){
for(int j=0; j<a[0].length; j++){
if(a[i][j]==1) {
int m = findX(j, i, a);
int n = findY(j, i, a);
for(int c=i; c<=n; c++){
for(int r=j; r<=m; r++){
a[c][r] = 0;
}
}
rect++;
}
}
}
return rect;
}
public static int findX(int x, int y, byte[][] a) {
int count = -1;
for (int i = x; i < a[0].length; i++) {
if (a[y][i] == 1) count++;
else break;
}
return count;
}
public static int findY(int x, int y, byte[][] a) {
int count = -1;
for (int i = y; i < a.length; i++) {
if (a[i][x] == 1) count++;
else break;
}
return count;
}
}

c<=n; c++){ ln 38: for(int r=j;r<=m; r++){ Both of the for loops will continue as long as variable c is less than or equal to n and variable r is less thanor equal to m. Variable c will be the current y index, while variable 'r' will be the current x index. Variables 'n' and 'm' signify the boundaries of the rectangle as determined from your methods findX and findY. Now with my array linked above, this works flawlessly on the very first rectangle starting at [0][0], but does not work for any of the other ones. This is because 'c' and 'r' are both set higher than 'n' and 'm' past the very first rectangle. You will need to compensate for the current position to properly scrub the rectangle and get the correct count. Otherwise this solution is very close and that is the only bug that I see.