#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<tuple>
using namespace std;
#define N 2010
int n;
char dir[N];
const int dx[]={0, 0, -1, 1}, dy[]={-1, 1, 0, 0};
int tugi[N*N], hno[N*N], s[N*N], op[N*N];
bool vis[N*N];
inline bool ingrid(int x, int y){
return 0<=x && x<=n-1 && 0<=y && y<=n-1;
}
inline int sc(int x, int y){
return x*n+y;
}
void dfs(int i){
if(s[i]) return;
s[i] = 1;
int x=i/n, y=i%n;
for(int j=0; j<=3; j++) if(ingrid(x+dx[j], y+dy[j])){
int ch = sc(x+dx[j], y+dy[j]);
if(tugi[ch] != i) continue;
dfs(ch);
if(s[i] < s[ch]+1){
op[i] = ch;
s[i] = s[ch]+1;
}
}
}
int main(){
scanf("%d", &n);
for(int i=0; i<=n-1; i++){
scanf("%s", dir);
for(int j=0; j<=n-1; j++){
int k = -1;
switch(dir[j]){
case 'l': k=0; break;
case 'r': k=1; break;
case 'u': k=2; break;
case 'd': k=3; break;
}
if(ingrid(i+dx[k], j+dy[k])){
tugi[sc(i, j)] = sc(i+dx[k], j+dy[k]);
}else{
tugi[sc(i, j)] = sc(i, j);
}
}
}
vector<int> toubu;
for(int i=0; i<=n*n-1; i++) if(!vis[i]){
int usagi=i, kame=i;
bool loli = true;
do{
usagi = tugi[tugi[usagi]];
kame = tugi[kame];
if(vis[kame]){
loli=false; break;
}
}while(usagi!=kame);
if(loli){
do{
usagi = tugi[usagi];
s[kame]++;
}while(usagi!=kame);
toubu.push_back(kame);
}
for(kame=i; !vis[kame]; kame=tugi[kame]){
vis[kame] = true;
}
}
memset(hno, -1, n*n*sizeof(int));
for(int i=0; i<(int)toubu.size(); i++){
for(int j=toubu[i]; hno[j]==-1; j=tugi[j]){
hno[j] = i;
}
}
memset(op, -1, n*n*sizeof(int));
for(int i=0; i<=n*n-1; i++) if(hno[i]==-1){
dfs(i);
}
for(int i=0; i<=n*n-1; i++) if(hno[i]==-1 && ~hno[tugi[i]]){
int h = toubu[hno[tugi[i]]];
if(op[h]==-1 || s[i]>s[op[h]]){
s[h] += s[i]-s[op[h]];
op[h] = i;
}
}
priority_queue<pair<int, int>> pq;
for(int i=0; i<(int)toubu.size(); i++){
pq.push(make_pair(s[toubu[i]], toubu[i]));
}
int ans = 0;
for(int c=1; !pq.empty(); c^=1){
int d, i;
tie(d, i) = pq.top(); pq.pop();
if(c) ans += d;
if(~hno[i]){
int j = i;
do{
int x=j/n, y=j%n;
for(int k=0; k<=3; k++) if(ingrid(x+dx[k], y+dy[k])){
int ch = sc(x+dx[k], y+dy[k]);
if(~hno[ch] || tugi[ch]!=j || ch==op[i]) continue;
pq.push(make_pair(s[ch], ch));
}
j = tugi[j];
}while(j!=i);
if(op[i] == -1) continue;
i = op[i];
}
for(; ~op[i]; i=op[i]){
int x=i/n, y=i%n;
for(int j=0; j<=3; j++) if(ingrid(x+dx[j], y+dy[j])){
int ch = sc(x+dx[j], y+dy[j]);
if(tugi[ch]!=i || ch==op[i]) continue;
pq.push(make_pair(s[ch], ch));
}
}
}
printf("%d\n", ans);
return 0;
}
I2luY2x1ZGU8Y3N0ZGlvPgojaW5jbHVkZTxjc3RyaW5nPgojaW5jbHVkZTx2ZWN0b3I+CiNpbmNsdWRlPHF1ZXVlPgojaW5jbHVkZTx0dXBsZT4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCiNkZWZpbmUgTiAyMDEwCmludCBuOwpjaGFyIGRpcltOXTsKY29uc3QgaW50IGR4W109ezAsIDAsIC0xLCAxfSwgZHlbXT17LTEsIDEsIDAsIDB9OwppbnQgdHVnaVtOKk5dLCBobm9bTipOXSwgc1tOKk5dLCBvcFtOKk5dOwpib29sIHZpc1tOKk5dOwppbmxpbmUgYm9vbCBpbmdyaWQoaW50IHgsIGludCB5KXsKICAgIHJldHVybiAwPD14ICYmIHg8PW4tMSAmJiAwPD15ICYmIHk8PW4tMTsKfQppbmxpbmUgaW50IHNjKGludCB4LCBpbnQgeSl7CiAgICByZXR1cm4geCpuK3k7Cn0Kdm9pZCBkZnMoaW50IGkpewogICAgaWYoc1tpXSkgcmV0dXJuOwogICAgc1tpXSA9IDE7CiAgICBpbnQgeD1pL24sIHk9aSVuOwogICAgZm9yKGludCBqPTA7IGo8PTM7IGorKykgaWYoaW5ncmlkKHgrZHhbal0sIHkrZHlbal0pKXsKICAgICAgICBpbnQgY2ggPSBzYyh4K2R4W2pdLCB5K2R5W2pdKTsKICAgICAgICBpZih0dWdpW2NoXSAhPSBpKSBjb250aW51ZTsKICAgICAgICBkZnMoY2gpOwogICAgICAgIGlmKHNbaV0gPCBzW2NoXSsxKXsKICAgICAgICAgICAgb3BbaV0gPSBjaDsKICAgICAgICAgICAgc1tpXSA9IHNbY2hdKzE7CiAgICAgICAgfQogICAgfQp9CgppbnQgbWFpbigpewogICAgc2NhbmYoIiVkIiwgJm4pOwogICAgZm9yKGludCBpPTA7IGk8PW4tMTsgaSsrKXsKICAgICAgICBzY2FuZigiJXMiLCBkaXIpOwogICAgICAgIGZvcihpbnQgaj0wOyBqPD1uLTE7IGorKyl7CiAgICAgICAgICAgIGludCBrID0gLTE7CiAgICAgICAgICAgIHN3aXRjaChkaXJbal0pewogICAgICAgICAgICAgICAgY2FzZSAnbCc6IGs9MDsgYnJlYWs7CiAgICAgICAgICAgICAgICBjYXNlICdyJzogaz0xOyBicmVhazsKICAgICAgICAgICAgICAgIGNhc2UgJ3UnOiBrPTI7IGJyZWFrOwogICAgICAgICAgICAgICAgY2FzZSAnZCc6IGs9MzsgYnJlYWs7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYoaW5ncmlkKGkrZHhba10sIGorZHlba10pKXsKICAgICAgICAgICAgICAgIHR1Z2lbc2MoaSwgaildID0gc2MoaStkeFtrXSwgaitkeVtrXSk7CiAgICAgICAgICAgIH1lbHNlewogICAgICAgICAgICAgICAgdHVnaVtzYyhpLCBqKV0gPSBzYyhpLCBqKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIHZlY3RvcjxpbnQ+IHRvdWJ1OwogICAgZm9yKGludCBpPTA7IGk8PW4qbi0xOyBpKyspIGlmKCF2aXNbaV0pewogICAgICAgIGludCB1c2FnaT1pLCBrYW1lPWk7CiAgICAgICAgYm9vbCBsb2xpID0gdHJ1ZTsKICAgICAgICBkb3sKICAgICAgICAgICAgdXNhZ2kgPSB0dWdpW3R1Z2lbdXNhZ2ldXTsKICAgICAgICAgICAga2FtZSA9IHR1Z2lba2FtZV07CiAgICAgICAgICAgIGlmKHZpc1trYW1lXSl7CiAgICAgICAgICAgICAgICBsb2xpPWZhbHNlOyBicmVhazsKICAgICAgICAgICAgfQogICAgICAgIH13aGlsZSh1c2FnaSE9a2FtZSk7CiAgICAgICAgaWYobG9saSl7CiAgICAgICAgICAgIGRvewogICAgICAgICAgICAgICAgdXNhZ2kgPSB0dWdpW3VzYWdpXTsKICAgICAgICAgICAgICAgIHNba2FtZV0rKzsKICAgICAgICAgICAgfXdoaWxlKHVzYWdpIT1rYW1lKTsKICAgICAgICAgICAgdG91YnUucHVzaF9iYWNrKGthbWUpOwogICAgICAgIH0KICAgICAgICBmb3Ioa2FtZT1pOyAhdmlzW2thbWVdOyBrYW1lPXR1Z2lba2FtZV0pewogICAgICAgICAgICB2aXNba2FtZV0gPSB0cnVlOwogICAgICAgIH0KICAgIH0KICAgIG1lbXNldChobm8sIC0xLCBuKm4qc2l6ZW9mKGludCkpOwogICAgZm9yKGludCBpPTA7IGk8KGludCl0b3VidS5zaXplKCk7IGkrKyl7CiAgICAgICAgZm9yKGludCBqPXRvdWJ1W2ldOyBobm9bal09PS0xOyBqPXR1Z2lbal0pewogICAgICAgICAgICBobm9bal0gPSBpOwogICAgICAgIH0KICAgIH0KICAgIG1lbXNldChvcCwgLTEsIG4qbipzaXplb2YoaW50KSk7CiAgICBmb3IoaW50IGk9MDsgaTw9bipuLTE7IGkrKykgaWYoaG5vW2ldPT0tMSl7CiAgICAgICAgZGZzKGkpOwogICAgfQogICAgZm9yKGludCBpPTA7IGk8PW4qbi0xOyBpKyspIGlmKGhub1tpXT09LTEgJiYgfmhub1t0dWdpW2ldXSl7CiAgICAgICAgaW50IGggPSB0b3VidVtobm9bdHVnaVtpXV1dOwogICAgICAgIGlmKG9wW2hdPT0tMSB8fCBzW2ldPnNbb3BbaF1dKXsKICAgICAgICAgICAgc1toXSArPSBzW2ldLXNbb3BbaF1dOwogICAgICAgICAgICBvcFtoXSA9IGk7CiAgICAgICAgfQogICAgfQogICAgcHJpb3JpdHlfcXVldWU8cGFpcjxpbnQsIGludD4+IHBxOwogICAgZm9yKGludCBpPTA7IGk8KGludCl0b3VidS5zaXplKCk7IGkrKyl7CiAgICAgICAgcHEucHVzaChtYWtlX3BhaXIoc1t0b3VidVtpXV0sIHRvdWJ1W2ldKSk7CiAgICB9CiAgICBpbnQgYW5zID0gMDsKICAgIGZvcihpbnQgYz0xOyAhcHEuZW1wdHkoKTsgY149MSl7CiAgICAgICAgaW50IGQsIGk7CiAgICAgICAgdGllKGQsIGkpID0gcHEudG9wKCk7IHBxLnBvcCgpOwogICAgICAgIGlmKGMpIGFucyArPSBkOwogICAgICAgIGlmKH5obm9baV0pewogICAgICAgICAgICBpbnQgaiA9IGk7CiAgICAgICAgICAgIGRvewogICAgICAgICAgICAgICAgaW50IHg9ai9uLCB5PWolbjsKICAgICAgICAgICAgICAgIGZvcihpbnQgaz0wOyBrPD0zOyBrKyspIGlmKGluZ3JpZCh4K2R4W2tdLCB5K2R5W2tdKSl7CiAgICAgICAgICAgICAgICAgICAgaW50IGNoID0gc2MoeCtkeFtrXSwgeStkeVtrXSk7CiAgICAgICAgICAgICAgICAgICAgaWYofmhub1tjaF0gfHwgdHVnaVtjaF0hPWogfHwgY2g9PW9wW2ldKSBjb250aW51ZTsKICAgICAgICAgICAgICAgICAgICBwcS5wdXNoKG1ha2VfcGFpcihzW2NoXSwgY2gpKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIGogPSB0dWdpW2pdOwogICAgICAgICAgICB9d2hpbGUoaiE9aSk7CiAgICAgICAgICAgIGlmKG9wW2ldID09IC0xKSBjb250aW51ZTsKICAgICAgICAgICAgaSA9IG9wW2ldOwogICAgICAgIH0KICAgICAgICBmb3IoOyB+b3BbaV07IGk9b3BbaV0pewogICAgICAgICAgICBpbnQgeD1pL24sIHk9aSVuOwogICAgICAgICAgICBmb3IoaW50IGo9MDsgajw9MzsgaisrKSBpZihpbmdyaWQoeCtkeFtqXSwgeStkeVtqXSkpewogICAgICAgICAgICAgICAgaW50IGNoID0gc2MoeCtkeFtqXSwgeStkeVtqXSk7CiAgICAgICAgICAgICAgICBpZih0dWdpW2NoXSE9aSB8fCBjaD09b3BbaV0pIGNvbnRpbnVlOwogICAgICAgICAgICAgICAgcHEucHVzaChtYWtlX3BhaXIoc1tjaF0sIGNoKSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICBwcmludGYoIiVkXG4iLCBhbnMpOwogICAgcmV0dXJuIDA7Cn0K