static int LeftMostColumnWithOne(BinaryMatrix binaryMatrix) { var row = binaryMatrix.Dimensions()[0]; var col = binaryMatrix.Dimensions()[1]; int c = col - 1; int r = 0; int mostLeft = col; while (c >= 0 && r < row) { if (binaryMatrix.Get(r, c) == 1) { c--; } else { r++; } mostLeft = Math.Min(c + 1, mostLeft); } return(mostLeft == col ? -1 : mostLeft); }