Fill the dp matrix vertically . For contiguous ' X ' increment the value of y , otherwise make it 0
for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( arr [ j ] [ i ] == ' X ' ) y += 1 ; else y = 0 ; dp [ j ] [ i ] . second = y ; } }