public static ArrayList<Submatrix> findLargestSubmatrix(int[][] matrix) {
        if (matrix == null) return null;
        int maxSum = Integer.MIN_VALUE;
        ArrayList<Submatrix> result = new ArrayList<Submatrix>();
        for (int x1 = 0; x1 < matrix.length; ++x1) {
            for (int x2 = x1; x2 < matrix.length; ++x2) {
                for (int y1 = 0; y1 < matrix[0].length; ++y1) {
                    for (int y2 = y1; y2 < matrix[0].length; ++y2) {
                        int sum = sum(matrix, x1, y1, x2, y2);
                        Submatrix submatrix = new Submatrix(x1, y1, x2, y2, sum);
                        if (sum > maxSum) {
                            maxSum = sum;
                            result.clear();
                            result.add(submatrix);
                        } else if (sum == maxSum) {
                            result.add(submatrix);
                        }
                    }
                }
            }
        }
        return result;
    }