https://www.acmicpc.net/problem/10026

#include<iostream>
using namespace std;
int n;
string s;
const int MAX = 100;
char arr[MAX][MAX];
char arr2[MAX][MAX];
int dx[] ={0, 0, -1, 1};
int dy[] ={1, -1, 0, 0};
void dfs(int y, int x, bool type, char(*arr)[MAX]){
    char c = arr[y][x];
        arr[y][x] = 'C';
    for(int i=0;i<4;i++){
        int mx = x + dx[i];
        int my = y + dy[i];
        if(mx >= n || mx<0 || my >= n || my<0)
            continue;
        if(type == true && (c == 'R' || c == 'G')){
            if(arr[my][mx] == 'R' || arr[my][mx] == 'G')
                dfs(my, mx, type, arr);
        }else{
            if(arr[my][mx] == c)
                dfs(my, mx, type, arr);
        }
    }
}
void solution(){
    int answer =0;
    int answer2 =0;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(arr[i][j] != 'C'){
                dfs(i, j, false, arr);
                answer++;
            }
            if(arr2[i][j] != 'C'){
                dfs(i, j, true, arr2);
                answer2++;
            }
        }
    }
    printf("%d %d\n", answer, answer2);
}
#include<cstdio>
#include<cstring>
const int dy[] = { 0,0,-1,1 };
const int dx[] = { 1,-1,0,0 };
int n;
char map[101][101], nap[101][101];
void dfs(int y, int x, char c, bool sw) {
	sw ? nap[y][x] = 0 : map[y][x] = 0;
	for (int w = 0; w < 4; ++w) {
		int ny = y + dy[w], nx = x + dx[w];
		if (ny < 0 || nx < 0 || ny == n || nx == n) continue;
		if (!sw) {
			if (map[ny][nx] == c) dfs(ny, nx, c, sw);
		}
		else {
			if( nap[ny][nx] == c || (c == 'R' && nap[ny][nx] == 'G')
				|| (c == 'G' && nap[ny][nx] == 'R')) dfs(ny, nx, c, sw);
		}
	}
}
void component() {
	int np = 0, mp = 0;
	for (int i = 0; i < n; ++i)
		strcpy(nap[i], map[i]);
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			if (map[i][j]) mp++, dfs(i, j, map[i][j], 0);
			if (nap[i][j]) np++, dfs(i, j, nap[i][j], 1);
		}
	}
	printf("%d %d", mp, np);
}
int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; ++i)
		scanf(" %s", map[i]);
	component();
}