How to optimise it further as i am getting TLE

#include
#include<bits/stdc++.h>
using namespace std;

int MatrixSearch(int **a,int n,int m,int x)
{
int c;
if(x<a[0][0] || x>a[n-1][m-1])
{
//cout<<“0”;
c=0;
}

	int i=0,j=m-1;
	while(i<n && j>=0)
	{
		
		if(a[i][j]==x)
		{
			//cout<<"1";
			c=1;
		}
		else if(a[i][j]>x)
		{
			j--;
		}
		//if(a[i][j]<x)
		else
		{
			i++;
		}
	}
	if(i==n || j==-1)
	{
	  //cout<<"0";
	 c=0;
	}

return c;

}

int main() {
int n,m;
cin>>n>>m;
//int a[30][30];
int *a=new int[n];
for(int i=0;i<n;i++)
{
a[i]=new int[m];
}

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

cout<<MatrixSearch(a,n,m,x);

return 0;

}

You are not putting the breaking conditions to end the loop when you get the element.