How to break a square matrix into square submatrices?
<p>I am trying to create a program that returns the maximum square submatrix of 1's from a square matrix of 0's and 1's.  Right now I have figured out how to break the square up into a square submatrix starting at each number that equals 1.  The problem is, as the program starts to get farther from the starting point of the matrix, it suddenly goes out of bounds, which I am suspecting has to do with how it calculates what part of the matrix to start from for each submatrix.</p>

<p>Here is my code:</p>

<pre><code>    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the number of rows and columns in the matrix (only one input, this is a square matrix): ");
    int dimensions = input.nextInt();
    int[][] matrix = new int[dimensions][dimensions];
    for (int i = 0; i &lt; matrix.length; i++) {
        for (int j = 0; j &lt; matrix[i].length; j++) {
            int n = input.nextInt();
            if (n == 0 || n == 1)
                matrix[i][j] = n;
            else
                System.out.print("Input only 0 or 1");
        }
    }
    int[] largestBlock = findLargestBlock(matrix);
}
public static int[] findLargestBlock(int[][] m) {
    int[] solution = new int[3];
    //find rows with most consecutive 1's, then find columns with the same # of consecutive 1's
    for (int i = 0; i &lt; m.length; i++) {
        for (int j = 0; j &lt; m[i].length; j++) {
            //"origin" for each iteration is (i, j)
            if (m[i][j] == 1)
                if (isSquare(m, i, j) == true) {
                    solution[0] = i; solution[1] = j; solution[2] = getSize(m, i, j);
                }
        }
    }
    return solution;
}
public static boolean isSquare(int[][] m, int i, int j) {
    int k = m.length - i;
    if (m[0].length - j &lt; k)
        k = m.length - j;
    if (k &lt; 2)
        return false;
    int[][] testSquare = new int[k][k];
    for (int y = i; y &lt; m.length - i; y++) {
        for (int x = j; x &lt; m[i].length - j; x++) {

            testSquare[y - i][x - j] = m[y][x];
        }
    }
    for (int y = 0; y &lt; testSquare.length; y++) {
        for (int x = 1; x &lt; testSquare[y].length; x++) {
            if (testSquare[y][x] != testSquare[y][x - 1])
                return false;
        }
    }
    for (int x = 0; x &lt; testSquare[0].length; x++) {
        for (int y = 1; y &lt; testSquare.length; y++) {
            if (testSquare[y][x] != testSquare[y - 1][x])
                return false;
        }
    }
    return true;
}

public static int getSize(int[][] m, int i, int j) {
    int k = m.length - i;
    if (m[0].length - j &lt; k)
        k = m.length - j;
    return k;
}
</code></pre>

<p>I determined that this part of the program was causing the issue, apparently there is some flaw in it that sends the array x- or y- value out of bounds:</p>

<pre><code>    public static boolean isSquare(int[][] m, int i, int j) {
    int k = m.length - i;
    if (m[0].length - j &lt; k)
        k = m.length - j;
    if (k &lt; 2)
        return false;
    int[][] testSquare = new int[k][k];
    for (int y = i; y &lt; m.length - i; y++) {
        for (int x = j; x &lt; m[i].length - j; x++) {

            **testSquare[y - i][x - j] = m[y][x];**
        }
    }
</code></pre>

<p>I'm very confused regarding the line in stars/in bold font, as I think this is the line causing the issue.  However, I'm not sure how its causing the issue.</p>