number of islands leetcode code

C++
void numIslands_dfs(int r, int c, int n, int m, vector<vector<char>> &grid)
    {        
        if(grid[r][c] != '1')
            return;
        
        grid[r][c] = '0';
        
        if(r+1 < n)
            numIslands_dfs(r+1,c,n,m,grid);
        
        if(c+1 < m)
            numIslands_dfs(r,c+1,n,m,grid);
        
        if(r-1 >= 0)
            numIslands_dfs(r-1,c,n,m,grid);
        
        if(c-1 >= 0)
            numIslands_dfs(r,c-1,n,m,grid);
        
    }
    
    int numIslands(vector<vector<char>>& grid) 
    {
        if(grid.size() == 0)
            return 0;
        
        int n = grid.size();
        int m = grid[0].size();
        
        int count = 0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(grid[i][j] == '1')
                {
                    count++;
                    numIslands_dfs(i,j,n,m,grid);
                }
            }
        }
        return count;    
    }
Source

Also in C++: