#include using namespace std; const int vy[] = {1, 0, -1, 0}; const int vx[] = {0, 1, 0, -1}; int N; string S[3000]; bool v[3000][3000]; int ord[3000][3000], low[3000][3000], k; int sub[3000][3000]; int rec(int y, int x, int py = -1, int px = -1) { v[y][x] = true; ord[y][x] = k++; low[y][x] = ord[y][x]; sub[y][x] = S[y][x] == '.'; int ret = 0, add = 1; for(int i = 0; i < 4; i++) { int ny = y + vy[i], nx = x + vx[i]; if(ny < 0 || nx < 0 || ny >= N || nx >= N) continue; if(S[ny][nx] == '#') continue; if(ny == py && nx == px) continue; if(v[ny][nx]) { low[y][x] = min(low[y][x], ord[ny][nx]); } else { ret = max(ret, rec(ny, nx, y, x)); sub[y][x] += sub[ny][nx]; low[y][x] = min(low[y][x], low[ny][nx]); if(ord[y][x] <= low[ny][nx]) add += sub[ny][nx]; } } if(S[y][x] == '.') ret = max(ret, add); return ret; } int main() { cin >> N; int all = 0; for(int i = 0; i < N; i++) { cin >> S[i]; all += count(begin(S[i]), end(S[i]), '.'); } int res; for(int i = 0; i < N; i++) { for(int j = 0; j < N; j++) { if(S[i][j] == 'T') res = all - rec(i, j); } } for(int i = 0; i < N; i++) { for(int j = 0; j < N; j++) { if(!v[i][j]) res -= S[i][j] == '.'; } } cout << res << endl; }