Code to find an element in an matrix, I am getting TLE in 2 test cases

#include
using namespace std;
int main() {
int arr[31][31];
int m;
int n;
int p;
int q;
cin >> m >> n;
p = 0;
q = n - 1;

for (int i = 0; i < m; i++) {
	for (int j = 0; j < n; j++) {
		cin >> arr[i][j];
	}
}

int x;
cin >> x;


while (p < m && q >= 0) {

	if (arr[p][q] == x)
		cout << "1";

	else if (arr[p][q] > x)
		q--;


	else
		p++;
}

cout << “0”;

return 0;
}