#pragma GCC target("arch=skylake")
#pragma GCC optimize("O3,unroll-all-loops")
#define ll long long
#define max(a,b) ((a)>(b)?(a):(b))
typedef long long ssize_t;
typedef unsigned long long size_t;
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
void *memset(void *s,int c,size_t n){
for(int i=0;i<n;++i) ((char *)s)[i]=c;
return s;
}
static void eexit(int status){
asm volatile (
"syscall"
:
: "a"(60), "D"(status)
: "rcx","r11","memory"
);
}
static ssize_t wwrite(int fd,const void *buf,size_t count){
ssize_t r;
asm volatile (
"syscall"
: "=a"(r)
: "a"(1), "D"(fd), "S"(buf), "d"(count)
: "rcx","r11","memory"
);
return r;
}
static ssize_t rread(int fd,void *buf,size_t count){
ssize_t r;
asm volatile (
"syscall"
: "=a"(r)
: "a"(0), "D"(fd), "S"(buf), "d"(count)
: "rcx","r11","memory"
);
return r;
}
static int isdigit(int c){
return '0'<=c&&c<='9';
}
static int atoi(const char *b,int *r){
int a=0;
int i=0;
while(isdigit(b[i])){
a*=10;
a+=b[i]-'0';
++i;
}
*r=a;
return i;
}
static void prints(size_t a){
char buf[30]={0};
buf[29]='\n';
int i=29;
do {
--i;
buf[i]='0'+a%10;
a/=10;
} while(a);
wwrite(STDOUT_FILENO,buf+i,30-i);
}
#define N 500
char B[350600];
int K,n,C,t[N][N],z[N],M[N],O[N];
ssize_t BS;
ll R,r;
int main(){
BS=rread(STDIN_FILENO,B,sizeof B);
int I=0;
I+=atoi(B,&n)+1;
int l[26]={0};
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
if(I>=BS){
BS=rread(STDIN_FILENO,B,(n-i-1)*(n+1)+n-j+1);
I=0;
}
int d=B[I]-'A';
if(!l[d]){
l[d]=K+1;
++K;
}
int c=1<<(l[d]-1);
t[i][j]=c;
C|=c;
++I;
}
if(I>=BS){
BS=rread(STDIN_FILENO,B,(n-i-1)*(n+1)+1);
I=0;
}
++I;
}
int sitruuna=0;
for(int c=K;c<26;++c) sitruuna|=1<<c;
for(int l=0;l<n;++l){
for(int i=0;i<N;++i) z[i]=0;
for(int i=0;i<N;++i) M[i]=0;
for(int i=0;i<N;++i) O[i]=0;
for(int r=l;r<n;++r){
for(int i=0;i<n;++i) z[i]|=t[r][i]|sitruuna;
int h[13]={0};
for(int i=0;i<n;++i){
int k=z[i];
h[0 ]=(h[0 ]+1)*!(k&(1<<0 ));
h[1 ]=(h[1 ]+1)*!(k&(1<<1 ));
h[2 ]=(h[2 ]+1)*!(k&(1<<2 ));
h[3 ]=(h[3 ]+1)*!(k&(1<<3 ));
h[4 ]=(h[4 ]+1)*!(k&(1<<4 ));
h[5 ]=(h[5 ]+1)*!(k&(1<<5 ));
h[6 ]=(h[6 ]+1)*!(k&(1<<6 ));
h[7 ]=(h[7 ]+1)*!(k&(1<<7 ));
h[8 ]=(h[8 ]+1)*!(k&(1<<8 ));
h[9 ]=(h[9 ]+1)*!(k&(1<<9 ));
h[10]=(h[10]+1)*!(k&(1<<10));
h[11]=(h[11]+1)*!(k&(1<<11));
h[12]=(h[12]+1)*!(k&(1<<12));
int m=max(max(max(max(h[0],h[1]),max(h[2],h[3])),max(max(h[4],h[5]),max(h[6],h[7]))),
max(max(max(h[8],h[9]),max(h[10],h[11])),h[12]));
M[i]=m;
}
h[0]=h[1]=h[2]=h[3]=h[4]=h[5]=h[6]=h[7]=h[8]=h[9]=h[10]=h[11]=h[12]=0;
for(int i=0;i<n;++i){
int k=z[i];
h[0 ]=(h[0 ]+1)*!(k&(1<<13));
h[1 ]=(h[1 ]+1)*!(k&(1<<14));
h[2 ]=(h[2 ]+1)*!(k&(1<<15));
h[3 ]=(h[3 ]+1)*!(k&(1<<16));
h[4 ]=(h[4 ]+1)*!(k&(1<<17));
h[5 ]=(h[5 ]+1)*!(k&(1<<18));
h[6 ]=(h[6 ]+1)*!(k&(1<<19));
h[7 ]=(h[7 ]+1)*!(k&(1<<20));
h[8 ]=(h[8 ]+1)*!(k&(1<<21));
h[9 ]=(h[9 ]+1)*!(k&(1<<22));
h[10]=(h[10]+1)*!(k&(1<<23));
h[11]=(h[11]+1)*!(k&(1<<24));
h[12]=(h[12]+1)*!(k&(1<<25));
int m=max(max(max(max(h[0],h[1]),max(h[2],h[3])),max(max(h[4],h[5]),max(h[6],h[7]))),
max(max(max(h[8],h[9]),max(h[10],h[11])),h[12]));
O[i]=m;
}
for(int i=0;i<n;++i) R+=max(M[i],O[i]);
}
}
r=n*(n+1)/2;
r*=r;
r-=R;
prints(r);
eexit(0);
}