|  | @@ -0,0 +1,49 @@
 | 
	
		
			
				|  |  | +import java.util.Scanner;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +public class Painter {
 | 
	
		
			
				|  |  | +    private static int N = 0, M = 0;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    public static void main(String[] args) {
 | 
	
		
			
				|  |  | +        Scanner scanner = new Scanner(System.in);
 | 
	
		
			
				|  |  | +        N = scanner.nextInt();
 | 
	
		
			
				|  |  | +        M = scanner.nextInt();
 | 
	
		
			
				|  |  | +        scanner.nextLine(); // !!important
 | 
	
		
			
				|  |  | +        char[][] board = new char[N][M];
 | 
	
		
			
				|  |  | +        for (int i = 0; i < N; i++) {
 | 
	
		
			
				|  |  | +            board[i] = scanner.nextLine().toCharArray();
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        int cnt = 0;
 | 
	
		
			
				|  |  | +        for (int i = 0; i < N; i++) {
 | 
	
		
			
				|  |  | +            for (int j = 0; j < M; j++) {
 | 
	
		
			
				|  |  | +                switch (board[i][j]) {
 | 
	
		
			
				|  |  | +                    case 'G':
 | 
	
		
			
				|  |  | +                        cnt++;
 | 
	
		
			
				|  |  | +                        dfs(board, j, i, 1, 1, 'Y', 'B');
 | 
	
		
			
				|  |  | +                    case 'B':
 | 
	
		
			
				|  |  | +                        cnt++;
 | 
	
		
			
				|  |  | +                        dfs(board, j, i, 1, -1, 'B', 'Y');
 | 
	
		
			
				|  |  | +                        break;
 | 
	
		
			
				|  |  | +                    case 'Y':
 | 
	
		
			
				|  |  | +                        cnt++;
 | 
	
		
			
				|  |  | +                        dfs(board, j, i, 1, 1, 'Y', 'B');
 | 
	
		
			
				|  |  | +                        break;
 | 
	
		
			
				|  |  | +                    default:
 | 
	
		
			
				|  |  | +                        break;
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        System.out.println(cnt);
 | 
	
		
			
				|  |  | +        scanner.close();
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private static void dfs(char[][] board, int x, int y, int dx, int dy, char curr, char next) {
 | 
	
		
			
				|  |  | +        if (x < 0 || M <= x || y < 0 || N <= y || board[y][x] == 'X' || board[y][x] == next) return;
 | 
	
		
			
				|  |  | +        if (board[y][x] == curr) {
 | 
	
		
			
				|  |  | +            board[y][x] = 'X';
 | 
	
		
			
				|  |  | +        } else if (board[y][x] == 'G') {
 | 
	
		
			
				|  |  | +            board[y][x] = next;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        dfs(board, x+dx, y+dy, dx, dy, curr, next);
 | 
	
		
			
				|  |  | +        dfs(board, x-dx, y-dy, dx, dy, curr, next);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +}
 |