<p>There are <code>n</code> cities. Some of them are connected, while some are not. If city <code>a</code> is connected directly with city <code>b</code>, and city <code>b</code> is connected directly with city <code>c</code>, then city <code>a</code> is connected indirectly with city <code>c</code>.</p>

<p>A <strong>province</strong> is a group of directly or indirectly connected cities and no other cities outside of the group.</p>

<p>You are given an <code>n x n</code> matrix <code>isConnected</code> where <code>isConnected[i][j] = 1</code> if the <code>i<sup>th</sup></code> city and the <code>j<sup>th</sup></code> city are directly connected, and <code>isConnected[i][j] = 0</code> otherwise.</p>

<p>Return <em>the total number of <strong>provinces</strong></em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/24/graph1.jpg" style="width: 222px; height: 142px;" />
<pre>
<strong>Input:</strong> isConnected = [[1,1,0],[1,1,0],[0,0,1]]
<strong>Output:</strong> 2
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/12/24/graph2.jpg" style="width: 222px; height: 142px;" />
<pre>
<strong>Input:</strong> isConnected = [[1,0,0],[0,1,0],[0,0,1]]
<strong>Output:</strong> 3
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 200</code></li>
	<li><code>n == isConnected.length</code></li>
	<li><code>n == isConnected[i].length</code></li>
	<li><code>isConnected[i][j]</code> is <code>1</code> or <code>0</code>.</li>
	<li><code>isConnected[i][i] == 1</code></li>
	<li><code>isConnected[i][j] == isConnected[j][i]</code></li>
</ul>

class Solution {
public:
//    void dfs(vector<int> adj[],int u,vector<bool>&visited)
//    {
//     visited[u]=true;

//     for(int &v:adj[u])
//     {
//         if(!visited[v])
//         {
//             visited[v]=true;
//             dfs(adj,v,visited);
//         }
//     }
//    }

void bfs(vector<int> adj[],vector<bool> &visited,int u)
{
    visited[u]=true;
    queue<int> q;
    q.push(u);

    while(!q.empty())
    {
        int node=q.front();
        q.pop();

        for(auto &v:adj[node])
        {
            if(!visited[v])
            {
                visited[v]=true;
                q.push(v);
                bfs(adj,visited,v);
            }
        }
    }
}

    int findCircleNum(vector<vector<int>>& isConnected) {

        int m=isConnected.size();
        int n=isConnected[0].size();
        vector<int> adj[m];

        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++){
                if(isConnected[i][j])
                {
                    adj[i].push_back(j);
                    adj[j].push_back(i);
                }
            }
        }
       int V=m;

        vector<bool> visited(V,0);
        int count=0;
       
       for(int i=0;i<V;i++)
       {
        if(!visited[i])
        {
            count++;
           // dfs(adj,i,visited);
           bfs(adj,visited,i);
        }
       }
        return count;
    }
};
23200