[アルゴリズム/標準]#10026赤色薬


難易度:金メダル5
)
package algorithm_lab.day15.hw;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class BJ_10026 {
	
	static char[][] map;
	static int N;
	static boolean[][] visited;
	static int[] dx= {1,-1,0,0};
	static int[] dy= {0,0,1,-1};
	
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		N=Integer.parseInt(br.readLine());
		map=new char[N][N];
		visited = new boolean[N][N];
		for(int i=0;i<N;i++) {
			String s = br.readLine();
			for(int j=0;j<N;j++) {
				map[i][j]=s.charAt(j);
			}
		}
		
		int cnt1=0, cnt2=0;
		for(int i=0;i<N;i++) {
			for(int j=0;j<N;j++) {
				if(!visited[i][j]) {
					dfs(i,j);
					cnt1++;
				}
			}
		}
		
		for(int i=0;i<N;i++) {
			for(int j=0;j<N;j++) {
				if(map[i][j]=='G') map[i][j]='R';
			}
		}
		
		visited = new boolean[N][N];
		
		for(int i=0;i<N;i++) {
			for(int j=0;j<N;j++) {
				if(!visited[i][j]) {
					dfs(i,j);
					cnt2++;
				}
			}
		}
		
		System.out.println(cnt1+" "+cnt2);
		
	}
	
	public static void dfs(int x, int y) {
		visited[x][y] =true;
		char c=map[x][y];
		for(int i=0;i<4;i++) {
			
			int nx=x+dx[i];
			int ny=y+dy[i];
			
			if(nx<0||nx>=N||ny<0||ny>=N) continue;
			
			if(!visited[nx][ny]&&map[nx][ny]==c) {
				dfs(nx,ny);
			}
		}
	}
}