Storing the cell value and cell indices in a vector .
vector < pair < long int , pair < int , int > > > x ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) x . push_back ( make_pair ( adj [ i ] [ j ] , make_pair ( i , j ) ) ) ;