What method can I use to determine continuity of squares on a 2d grid? I have N squares aligned to a 2d grid. I'd like to know if the set is continuous -- that is to say, that each of the N squares is adjacent to at least one other square in the set.
This is quite simple if you're looking at a grid in real life, but I'm attempting to solve this in the middle of a program, which means I need a way to solve algorithmic-ly rather than organically.
In this example, the red group would be "continuous", whereas the blue group would not be.
I would also appreciate terminology & tag suggestions, as I doubt "continuous" is the best term for what I'm trying to determine.

 A: You can solve this by using a union find. I'll assume that your grid has size $N$ and $M$ and that you have the information regarding how the grid is filled in an array G[N][M]. We are going to do a union find and then check if every "full" square gives us the same "find". Here is the c++ clode:
#include <bits/stdc++.h>
using namespace std;
typedef pair <int,int> pi;

const int N=100;// you can change N and M if you want
const int M=100;
int G[N][M]; //here is where the grid is saved
pi F[N][M]; //this tells us to which connected component each full cell pertains

pi find(pi a){
    if(F[a.first][a.second]==a) return a;
    F[a.first][a.second]=find(F[a.first][a.second]);
    return(F[a.first][a.second]);
}

void merge(pi a,pi b){
    // this just says "set the connected component of square a to be the connected component of square b"
    if(b.first <N && b.first<=0 && b.second<M && b.second>=0 && G[b.first][b.second]==1)
        F[find(a).first][find(a).second] = find(b);
}

void combine (pi a){
    // merging two cells tell the computer they are in the same connected component
    merge( a, pi(a.first+1, a.second ) );
    merge( a, pi(a.first-1, a.second ) );
    merge( a, pi(a.first, a.second+1 ) );
    merge( a, pi(a.first, a.second-1 ) );
}

int check(){
    pi component= pi(-1,-1);
    for(int i=0; i<N;i++){
        for(int j=0;j<M;j++){
            if(G[i][j]==1){
                if( component == pi(-1,-1)) component=find(pi (i,j));
                if( component != find( pi (i,j) ) ) return(0);
            }
        }
    }
    return(1);
}

int main(){
    for(int i=0;i<N;i++){
        for(int j=0;j<N;j++){
            find(pi (i,j) ) = pi(i,j); //initally all blocks have different connected components
        }
    }
    for(int i=0; i<N;i++){
        for(int j=0;j<M;j++){
            if(G[i][j]==1) combine(pi (i,j) ); // this function merges each square with its neighbours
        }
    }
    if (check() ) printf("connected\n");
    else printf("disconnected\n");
}

