How can i improve time complexity?

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

bool vis[maxval][maxval];

int countone(bool **pond, int i, int j,int n, int m){

if(i>=n || j>=m || i<0 || j<0){
	return 0;
}

if(vis[i][j]==1){
	return 0;
}

if(pond[i][j]==0){
	return 0;
}

vis[i][j] = 1;
int x = countone(pond,i,j+1,n,m);
int y = countone(pond,i,j-1,n,m);
int z = countone(pond,i+1,j,n,m);
int w = countone(pond,i-1,j,n,m);

return 1+x+y+z+w;

}

int solve(bool ** pond, int n, int m){

int ma = INT_MIN;

for(int i=0;i<n;++i){
	for(int j=0;j<m;++j){
		if(pond[i][j] == 1){
			continue;
		}

		memset(vis,false,sizeof(vis));
		pond[i][j] = 1;
		int count = countone(pond,i,j,n,m);
		pond[i][j] = 0;

		ma = max(ma,count);

	}
}

return ma;

}

int main(){

int n,m;
cin>>n>>m;

bool **pond = new bool*[n];
for(int i=0;i<n;++i){
	pond[i] = new bool [m];
	for(int j=0;j<m;++j){
		cin>>pond[i][j];
	}
}

cout<<solve(pond,n,m);

return 0;

}

@ime_mya
keep a count of visited array and keep marking all the nodes once visited, so that you dont visit them again, secodly mark all the connected componenets with an index , finaly just take five distinct values of every index , up,down,left,right,and that index it self.