#include <cstdio>
#include <queue>

using namespace std;

const int kMaxSize = 1001;
const int kInfinity = 1000000000;

char table[kMaxSize][kMaxSize];
int visited[kMaxSize][kMaxSize];
int blackValues[kMaxSize][kMaxSize], whiteValues[kMaxSize][kMaxSize];
int componentSize[kMaxSize * kMaxSize];
int n, blackCount, whiteCount = 1, sol, bfsCount;
int bestBlack, bestWhite = kInfinity; 
queue<pair<int, int> > bfsQueue;
int dr[] = {-1, 0, 1, 0};
int dc[] = {0, 1, 0, -1}; 

bool isConnected(int row, int column, int type)
{
    return row >= 0 && row < n && column >= 0 && column < n && table[row][column] == type;   
}

void bfs(int row, int column, char stone)
{
    pair<int, int> liberty(-1, -1);
    int liberties = 0, groupSize = 0;
    bfsCount++;
    bfsQueue.push(make_pair(row, column));
    visited[row][column] = bfsCount;
    while (!bfsQueue.empty())
    {
        pair<int, int> current = bfsQueue.front();
        bfsQueue.pop();       
        groupSize++;
        for (int i = 0; i < 4; i++)
        {
            if (visited[current.first + dr[i]][current.second + dc[i]] != bfsCount)
            {
                if (isConnected(current.first + dr[i], current.second + dc[i], stone))
                {
                    bfsQueue.push(make_pair(current.first + dr[i], current.second + dc[i])); 
                    visited[current.first + dr[i]][current.second + dc[i]] = bfsCount;  
                }
                if (isConnected(current.first + dr[i], current.second + dc[i], '.'))
                {
                    visited[current.first + dr[i]][current.second + dc[i]] = bfsCount;
                    liberty = make_pair(current.first + dr[i], current.second + dc[i]); 
                    liberties++;  
                }      
            }
        }         
    } 
    componentSize[bfsCount] = liberties;
    if (liberties == 1)
    {
        if (stone == 'C')
        {
            blackValues[liberty.first][liberty.second] += groupSize;   
        }       
        else
        {
            whiteValues[liberty.first][liberty.second] += groupSize;   
        }
    }
    
}

int main()
{
    freopen("go.in", "r", stdin);
    freopen("go.out", "w", stdout);
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    {
        scanf("%s", table[i]);   
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (table[i][j] != '.')
            {
                if (visited[i][j] == 0)
                {
                    bfs(i, j, table[i][j]);   
                }
                if (table[i][j] == 'C') 
                {
                    blackCount++;   
                }   
                else
                {
                    whiteCount++;   
                }
            }
        }   
    }
    
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (table[i][j] == '.')
            {
                if (blackValues[i][j] > bestBlack)
                {
                    bestBlack = blackValues[i][j];  
                }    
                if (whiteValues[i][j] == 0)
                {
                    bool free = false;
                    for (int k = 0; k < 4; k++)
                    {
                        if (isConnected(i + dr[k], j + dc[k], '.'))
                        {
                            free = true;
                        }  
                    }
                    if (free)
                    {
                        whiteValues[i][j] = -1;   
                    }
                }
                if (blackValues[i][j] == 0)
                {
                    bool connects = false;
                    for (int k = 0; k < 4; k++)
                    {
                        if (isConnected(i + dr[k], j + dc[k], 'B') &&
                            componentSize[visited[i + dr[k]][j + dc[k]]] > 1)
                        {
                            connects = true;   
                        }
                    }
                    if (connects)
                    {
                        whiteValues[i][j] = -1;  
                    }
                    if (whiteValues[i][j] < bestWhite)
                    {
                        bestWhite = whiteValues[i][j] + 1;   
                    }
                }
            }
        }
    }
    sol = bestBlack > 0 ? whiteCount - blackCount + bestBlack: 
            whiteCount - blackCount - bestWhite;
    printf("%d\n", sol);
    return 0;   
}
