请大佬帮忙看看
#include <iostream>
using namespace std;
const int N = 1001;
const short dx[4] = {1, 0, -1, 0};
const short dy[4] = {0, 1, 0, -1};
bool map[N][N], vis[N][N];
short n;
int m;
void dfs(short x, short y){
if(x <= 0 || x > n || y <= 0 || y > n || vis[x][y]){
return ;
}
bool flag = map[x][y];
vis[x][y] = true;
for(int i=0; i<4; i++)
if(map[x+dx[i]][y+dy[i]] == (!flag))
dfs(x + dx[i], y + dy[i]);
}
int found(){
int ans=0;
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
if(vis[i][j])
ans++;
return ans;
}
void cl(){
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
vis[i][j] = false;
}
int main(){
cin >> n >> m;
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
cin >> map[i][j];
for(int i=0; i<m; i++){
short x, y;
cin >> x >> y;
dfs(x, y);
int ans = found();
cout << ans << endl;
cl();
}
return 0;
}