Devise an algorithm of complexity O(N) finding No of 1's from a 2 dimensional matrix[N][N] containing 1's and 0's
I think your main loop should be something more along the lines of

row = n-1;      // start at bottom row 
for (col=0; col&lt;n; col++) {   // read columns from left to right
    while ((row &gt;= 0) &amp;&amp; (map[row,col] == 0)) {   // while not out of rows, and on a 0
        sum += col;  //add count of 1s to total
        row--;       //move to next row up
    }
    // do nothing if we're on a 1, just move to next column.
}
if (row &gt;= 0) sum += (row+1)*col; // add in any leftover rows of all 1s
printf("sum is %d\n",sum);