public class Solution {
    public boolean Find(int target, int [][] array) {
        if (array == null || array.length == 0 || array[0].length == 0) {
            return false;
        }
        if (target < array[0][0]) {
            return false;
        }

        if (target > array[array.length - 1][array[0].length - 1]) {
            return false;
        }

        int row = array.length - 1;
        int col = 0;

        while (col < array[0].length) {
            if (array[0][col] == target || array[row][col] == target) {
                return true;
            }
            if (array[0][col] < target && target < array[row][col]) {
                break;
            }
            col++;
        }

        for (int i = col; i < array[0].length; i++) {
            for (int j = 0; j <= row; j++) {
                if (array[j][i] == target) {
                    return true;
                }
            }
        }
        return false;
    }

}