public static int[][] es(int[][] a, int y1, int y2, int x1, int x2) {
if (y1 < 0 || x1 < 0)
return new int[0][0];
if (y2 > a.length)
y2 = a.length;
if (x2 > a.length)
x2 = a.length;
int[][] b = new int[y2 - y1][x2 - x1];
for (int y = y1; y < y2; y++) {
for (int x = x1; x < x2 && x < a[y].length; x++) {
b[y - y1][x - x1] = a[y][x];
}
}
return b;
}
public static void main(String[] args) {
System.out.println(Arrays.deepToString(es(new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }, 0, 2, 0, 2)));
System.out.println(Arrays.deepToString(es(new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }, 2, 4, 2, 4)));
System.out.println(Arrays.deepToString(es(new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8 } }, 2, 4, 2, 4)));
}