for (int i = 0; i < n; i++)
{
if (was[i] != -1)
continue;
q.push(i);
while (!q.empty())
{
int v = q.front();
q.pop();
if (was[v] != -1)
continue;
was[v] = cur;
for (int j = 0; j < n; j++)
if (mas[i][j] != 0 && was[j] == -1)
q.push(j);
}
cur++;
}