c++程序运行错误,进入dfs函数后直接退出,后面没有输出

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果

```c++

#include <iostream>
#include<string>
using namespace std;
int cnt=0,total=0,m,n;
string a[500];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
bool visit[500][500];

void dfs(int x,int y){
  cnt++;
  visit[x][y] = true;
  for(int d=0;d<4;d++){
    int nx = x + dx[d];
    int ny = y + dy[d];
    if(0<=x && nx<n &&ny>=0 &&ny<m && a[nx][ny]=='1' && !visit[nx][ny]){
      dfs(nx,ny);
    }
  }
}

int main()
{
  // 请在此输入您的代码
  cin>>n>>m;
  for(int i=0;i<n;i++){
    cin>>a[i];
    for(int j=0;j<m;j++){
      if(a[i][j] == '1') total++;
    }
  }

  for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
      if(a[i][j] == '1'){
        dfs(i,j);
        if(cnt == total) cout<<"YES";
          else cout<<"NO ";
          return 0;
      }
    }
  }
  return 0;
}

```

bool visit[500][500]={false};
初始化一下。

递归中的判断条件里面错把nx写成x了