Store frequency of elements in matrix
for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( mp . ContainsKey ( arr [ i , j ] ) ) mp [ arr [ i , j ] ] ++ ; else mp [ arr [ i , j ] ] = 1 ; } }