static Scanner sc = new Scanner(System.in);
public static void main(String[] args){
int rows = sc.nextInt();
int cols = sc.nextInt();
int[][] arr = takeInput(rows,cols);
int item = sc.nextInt();
System.out.println(matrix_search(arr,rows,cols,item));
}
private static int matrix_search(int[][] arr,int rows, int cols,int item){
if(rows == 1){
return binarySearch(arr, 0, 0, cols-1, item);
}
int present = 0;
int top = 0;
int bottom = rows-1;
int mid = (cols/2);
while((top+1) < bottom){
int m = (top + bottom)/2;
if(item == arr[m][mid]){
return 1;
}else if(item < arr[m][mid]){
bottom = m;
}else{
top = m;
}
}
if(arr[top][mid] == item) return 1;
if(arr[top+1][mid] == item) return 1;
else if(item < arr[top][mid]){
present = binarySearch(arr, top,0,mid-1,item);
}else if(item > arr[top][mid]){
present = binarySearch(arr, top,mid+1,cols-1,item);
}else if(item < arr[top+1][mid]){
present = binarySearch(arr, top+1,0,mid-1,item);
}else{
present = binarySearch(arr, top+1,mid+1,cols-1,item);
}
return present;
}
static int binarySearch(int[][] arr,int row, int col_start, int col_end, int item){
while(col_start <= col_end){
int mid = (col_start + col_end)/2;
if(arr[row][mid] < item) col_start = mid + 1;
else if(arr[row][mid] > item) col_end = mid - 1;
else return 1;
}
return 0;
}
private static int[][] takeInput(int rows, int cols){
int[][] arr = new int[rows][cols];
for(int row=0; row < rows; row++){
for(int col = 0; col < cols; col++){
arr[row][col] = sc.nextInt();
}
}
return arr;
}