Matrix Search Array Problem

#include
using namespace std;
int main() {
int n,m;
cin >> n >> m;
int a[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin >> a[i][j];
}
}
int test;
cin >> test;
int i=0,j=0;
while(i<n && j<m){
if(a[i][j]==test){
cout << 1 <<endl;
break;
}
if(a[i][j]<test){
j++;
}
if(a[i][j]>test){
i++;
}
}
if(a[i][j]!=test){
cout << 0 << endl;
}
return 0;
}

Here is my code, two test case are passed, still third shows wrong answer, i can’t figure out what i did wrong. Please Help

Hey Yuvray, your code is wrong you will not be able to search the elements like this, for eg. check for this case
input:
4 4
10 20 30 40
15 25 35 45
27 28 37 48
32 33 39 50
37

your code’s output is 0 but the element is present in the matrix.