Language: C++

#include<algorithm>
#include<vector>
#include<cassert>
#include<queue>
#include<string.h>
#define F first
#define S second
using namespace std;
typedef pair<int,int> P;
char cmap[13][13];
bool used[13][13];
int imap[13][13];
static const int dx[4] = {0,1,0,-1};
static const int dy[4] = {1,0,-1,0};
int Cnt = 0;

void dfs(int x,int y,char c,int cnt)
{
  used[y][x] = true; 
  int fcnt = 0;
  for(int i=0;i<4;i++){
    int nx = x+dx[i],ny = y+dy[i];
    if(0<=nx&&nx<6&&0<=ny&&ny<12&&cmap[ny][nx] == c && !used[ny][nx]){
      fcnt++; Cnt++;
      dfs(nx,ny,c,cnt+1);
    }
  }
  if(fcnt == 0)imap[y][x] = Cnt;
}

void erase(int x,int y,char c)
{
  used[y][x] = true;
  cmap[y][x] = '.';

  queue<P> que;
  que.push(P(x,y));

  while(!que.empty())
    {
      P p = que.front(); que.pop();
      for(int i=0;i<4;i++)
	{
	  int nx = p.F+dx[i],ny = p.S+dy[i];

	  if(0<=nx&&nx<6&&0<=ny&&ny<12&& ((!used[ny][nx] && cmap[ny][nx] == c) || cmap[ny][nx] == 'O'))
	    {
	      if(cmap[ny][nx] != 'O')que.push(P(nx,ny));
	      cmap[ny][nx] = '.';
	      used[ny][nx] = true;
	      
	    }
	}
    }
}

void display()
{

  for(int i=0;i<12;i++)
    {
      for(int j=0;j<6;j++)
	{
	  cout << cmap[i][j] << " ";
	}
      cout << endl;
    }
  /*
for(int i=0;i<12;i++)
    {
      for(int j=0;j<6;j++)
	{
	  cout << imap[i][j] << " ";
	}
      cout << endl;
    }
  */
  cout << endl;
}


void drop(int x,int y)
{
  //cout << "x = " << x << ", y = " << y << endl;
  int st,ed;
  st = y;
  ed = -1;
  for(int i=y;i>=0;i--)
    {
      if(ed == -1 && cmap[i][x] != '.')
	{
	  ed = i+1;
	  //cout << "cmap["<<st-ed+(i+1)<<"][x] =" << cmap[st+ed-(i+1)][x] << ", cmpa["<<i<<"][x] = "<<  cmap[i][x] << endl;
	  cmap[st-ed+(i+1)][x] = cmap[i][x];
	  cmap[i][x] = '.';
	}
      else if(ed != -1 && cmap[i][x] != '.')
	{
	  //cout << "cmap["<<st-ed+(i+1)<<"][x] = " << cmap[st+ed-(i+1)][x] << ", cmap["<<i<<"][x] = " <<  cmap[i][x] << endl;
	  cmap[st-ed+(i+1)][x] = cmap[i][x];
	  cmap[i][x] = '.';
	}
      else if(ed != -1 && cmap[i][x] == '.')
	{
	  st = i;
	  ed = -1;
	}      

    }
}


int main()
{
  int N;
  cin >> N;
  while(N-- > 0)
    {
      for(int i=0;i<12;i++)
	{
	  string s;
	  cin >> s;
	  for(int j=0;j<s.length();j++)
	    {
	      cmap[i][j] = s[j];
	      used[i][j] = false;
	      imap[i][j] = -1;
	    }
	}

      int cnt = 0;
      while(true)
	{
	  //display();
	  int increase = 0;
	  for(int i=0;i<12;i++)for(int j=0;j<6;j++)
				 {
				   imap[i][j] = -1;
				   used[i][j] = false;
				 }
	  for(int i=0;i<12;i++)
	    {
	      for(int j=0;j<6;j++)
		{
		  if(cmap[i][j] != '.' && cmap[i][j] != 'O' && !used[i][j])
		    {
		      Cnt=1;
		      dfs(j,i,cmap[i][j],1); //最大何マスつながっているか
		    }
		}
	    }
	
	  memset(used,0,sizeof(used));
	  for(int i=0;i<12;i++)for(int j=0;j<6;j++)used[i][j] = false;
	  for(int i=0;i<12;i++)
	    {
	      for(int j=0;j<6;j++)
		{
		  if(!used[i][j] && imap[i][j] >= 4){
		    increase++;
		    erase(j,i,cmap[i][j]);
		  }
		}
	    }
	  //display();
	  if(increase == 0)
	    {
	      break;
	    }
	  cnt++;


	  for(int i=0;i<6;i++)
	    {
	      for(int j=11;j>=0;j--)
		{
		  if(cmap[j][i] == '.')
		    {
		      drop(i,j);
		      break;
		    }
		}

	    }





	}

      cout<< cnt << endl;

    }
  return 0;
}

Compiler output:
a.cc: In function 'void display()':
a.cc:64:11: error: 'cout' was not declared in this scope
   64 |           cout << cmap[i][j] << " ";
      |           ^~~~
a.cc:6:1: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
    5 | #include<string.h>
  +++ |+#include <iostream>
    6 | #define F first
a.cc:66:7: error: 'cout' was not declared in this scope
   66 |       cout << endl;
      |       ^~~~
a.cc:66:7: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:66:15: error: 'endl' was not declared in this scope
   66 |       cout << endl;
      |               ^~~~
a.cc:6:1: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
    5 | #include<string.h>
  +++ |+#include <ostream>
    6 | #define F first
a.cc:78:3: error: 'cout' was not declared in this scope
   78 |   cout << endl;
      |   ^~~~
a.cc:78:3: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:78:11: error: 'endl' was not declared in this scope
   78 |   cout << endl;
      |           ^~~~
a.cc:78:11: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
a.cc: In function 'int main()':
a.cc:116:3: error: 'cin' was not declared in this scope
  116 |   cin >> N;
      |   ^~~
a.cc:116:3: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:121:11: error: 'string' was not declared in this scope
  121 |           string s;
      |           ^~~~~~
a.cc:6:1: note: 'std::string' is defined in header '<string>'; this is probably fixable by adding '#include <string>'
    5 | #include<string.h>
  +++ |+#include <string>
    6 | #define F first
a.cc:122:18: error: 's' was not declared in this scope
  122 |           cin >> s;
      |                  ^
a.cc:192:7: error: 'cout' was not declared in this scope
  192 |       cout<< cnt << endl;
      |       ^~~~
a.cc:192:7: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:192:21: error: 'endl' was not declared in this scope
  192 |       cout<< cnt << endl;
      |                     ^~~~
a.cc:192:21: note: 'std::endl' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'