Can someone pls tell why is it showing Wrong Answer


#1

public class Solution {
public int searchMatrix(int[][] A, int B) {

if(A.length == 0 || A[0].length == 0) {
        return 0;
    }

    int n=A[0].length;
    int l=0,h=A.length+n-1;
    
    while(l<=h)
    {
        int m=l+(h-l)/2;
        
        int r=m/n;
        int c=m%n;
       
        int cval=A[r][c];
        
        if(B==cval)
            return 1;
        
        if(B<cval)
            h=m-1;
        
        else
            l=m+1;
    }
    return 0;
}

}


#2

h should be A.length*n-1;