Function to find required prefix - count for each row from right to left
function findPrefixCount ( p_arr , arr ) { for ( var i = 0 ; i < n ; i ++ ) for ( var j = n - 1 ; j >= 0 ; j -- ) { if ( arr [ i ] [ j ] ) continue ; if ( j != n - 1 ) p_arr [ i ] [ j ] += p_arr [ i ] [ j + 1 ] ; p_arr [ i ] [ j ] += ( ! arr [ i ] [ j ] ) ; } }