As already noted in the comments, you have incorrectly set upper limits for the cycles.
int a[N][M]; for(int i=0;i<M;i++){ ^^^^ for(int j=0;j<N;j++){ ^^^^ a[i][j]=rand()%2; cout<<a[i][j]<<" "; } cout<<endl; }
Must be
int a[N][M]; for(int i=0;i<N;i++){ ^^^ for(int j=0;j<M;j++){ ^^^ a[i][j]=rand()%2; cout<<a[i][j]<<" "; } cout<<endl; }
To avoid this kind of error, you can initialize in C ++ and output to the console an array like this, using a range-based loop.
for ( autp &row : a ) { for ( auto &x : row ) { x = rand() % 2; cout << x << " "; } cout << endl; }
Keep in mind that the rand function is defined in the <cstdlib> header that should be included in the program.
#include <cstdlib>
MandNin cycles. - post_zeew