#include <iostream>
#include <string>

using namespace std;

int dx[4] = {0, -1, 1, 0};
int dy[4] = {-1, 0, 0, 1};
int cnt = 1;
int arr[101][101];

void war (int i,int j,int n,int m) {
    if (arr[i][j] == 1) {
        arr[i][j] = 0;
        for (int b = 0;b < 4;b++) {
            if (0 <= i+dy[b] && m > i+dy[b] && 0 <= j+dx[b] && n > j+dx[b]) {
                if (arr[i+dy[b]][j+dx[b]] == 1) {
                    war(i+dy[b],j+dx[b],n,m);
                    cnt++;
                }
            }
        }
    }
    else {
        arr[i][j] = 0;
        for (int b = 0;b < 4;b++) {
            if (0 <= i+dy[b] && m > i+dy[b] && 0 <= j+dx[b] && n > j+dx[b]) {
                if (arr[i+dy[b]][j+dx[b]] == 2) {
                    war(i+dy[b],j+dx[b],n,m);
                    cnt++;
                }
            }
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,m;
    cin >> n >> m;
    int W = 0;
    int B = 0;
    for (int i = 0;i < m;i++) {
        string s;
        cin >> s;
        for (int j = 0;j < n;j++) {
            if (s[j] == 'W') {
                arr[i][j] = 1;
            }
            else{
                arr[i][j] = 2;
            }
        }
    }

    
                                
    for (int i = 0;i < m;i++) {
        for (int j = 0;j < n;j++) {
            cnt = 1;
            if (arr[i][j] == 1) {
                war(i,j,n,m);
                W += cnt*cnt;
            }
            else if(arr[i][j] == 2){
                war(i,j,n,m);
                B += cnt*cnt;
            }
        }
    }
    cout << W << " " << B;
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: