#include<iostream>
#include<cstdio>
#define Min(x,y) ((x)<(y)?(x):(y))
#define Max(x,y) ((x)>(y)?(x):(y))
#define gmi(x,y) x=Min(x,y)
#define gmx(x,y) x=Max(x,y)
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define ROF(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int N=1515,INF=1000010000;
int n,dep[N],top[N],q[N],su[N],g0[N];
long long ans;
int A[N][N],a[N][N],lx[N][N],rx[N][N],dx[N][N],tx[N][N];
int mp[N][N],g1[N][N],g2[N][N],g3[N][N];
char str[N];
void ini_lxrx(int md,int H,int W){
	FOR(i,1,H)FOR(j,1,W) lx[i][j]=INF,rx[i][j]=-INF;
	FOR(j,1,W)if(a[md][j]) lx[md][j]=rx[md][j]=j;
	//FOR(i,1,H)FOR(j,1,W) cout<<lx[i][j]<<'-'<<rx[i][j]<<" \n"[j==W];
	ROF(i,md-1,1)ROF(j,W,1)if(a[i][j]){
		if(i<md) gmi(lx[i][j],lx[i+1][j]),gmx(rx[i][j],rx[i+1][j]);
		if(j< W) gmi(lx[i][j],lx[i][j+1]),gmx(rx[i][j],rx[i][j+1]);
	}
	FOR(i,md+0,H)FOR(j,1,W)if(a[i][j]){
		if(i>md) gmi(lx[i][j],lx[i-1][j]),gmx(rx[i][j],rx[i-1][j]);
		if(j> 1) gmi(lx[i][j],lx[i][j-1]),gmx(rx[i][j],rx[i][j-1]);
	}
	ROF(i,H,md)ROF(j,W,1) dx[i][j]=a[i][j]?i:-INF;
	ROF(i,H,md)ROF(j,W,1)if(a[i][j]){
		if(i<H) gmx(dx[i][j],dx[i+1][j]);
		if(j<W) gmx(dx[i][j],dx[i][j+1]);
	}
	FOR(j,1,W) dep[j]=dx[md][j];
	FOR(i,1,md)FOR(j,1,W) tx[i][j]=a[i][j]?i:+INF;
	FOR(i,1,md)FOR(j,1,W)if(a[i][j]){
		if(i>1) gmi(tx[i][j],tx[i-1][j]);
		if(j>1) gmi(tx[i][j],tx[i][j-1]);
	}
	FOR(j,1,W) top[j]=tx[md][j];
	FOR(i,1,H)FOR(j,1,W) a[i][j]*=lx[i][j]<=rx[i][j];
}
void ini_g123(int md,int H,int W){
	FOR(i,0,W+1)FOR(j,0,W+1) mp[i][j]=INF;
	FOR(i,md,H)FOR(j,1,W)if(a[i][j])
		gmi(mp[lx[i][j]][rx[i][j]],i);
	FOR(i,1,W)ROF(j,W,i){
		if(i>1) gmi(mp[i][j],mp[i-1][j]);
		if(i<W) gmi(mp[i][j],mp[i][j+1]);
	}
	FOR(i,1,W)ROF(j,W,i)
		if(Min(dep[i],dep[j])<mp[i][j]) mp[i][j]=INF;
	FOR(i,0,H+1) g0[i]=0;
	FOR(i,0,H+1)FOR(j,0,W+1) g1[i][j]=g2[i][j]=0;
	FOR(i,0,W+1)FOR(j,0,W+1) g3[i][j]=0;
	FOR(i,md,H)FOR(j,1,W)if(a[i][j]){
		g0[i]+=a[i][j];
		g1[i][lx[i][j]]+=a[i][j];
		g2[i][rx[i][j]]+=a[i][j];
		g3[lx[i][j]][rx[i][j]]+=a[i][j];
	}
	FOR(i,md,H) g0[i]+=g0[i-1];
	FOR(i,md,H)ROF(j,W,1) g1[i][j]+=g1[i][j+1]+g1[i-1][j]-g1[i-1][j+1];
	FOR(i,md,H)FOR(j,1,W) g2[i][j]+=g2[i][j-1]+g2[i-1][j]-g2[i-1][j-1];
	ROF(i,W,1)FOR(j,1,W)  g3[i][j]+=g3[i+1][j]+g3[i][j-1]-g3[i+1][j-1];
}
int cal(int x,int y,int h){
	if(h<mp[x][y]) return 0;
	gmi(h,Min(dep[x],dep[y]));
	return g0[h]+g3[x+1][y-1]-g1[h][x+1]-g2[h][y-1];
}
void work(int i,int md,int H,int W){
	int ql=1,qr=1,nl=1,nr=0,sum=0,las;
	FOR(j,1,W) su[j]=0;
	FOR(j,1,W)if(a[i][j]){
		while(nr<rx[i][j]){
			nr++;las=0;
			if(top[nr]>i) continue;
			while(ql<qr && las<=dep[nr]){
				int x=q[qr-1],y=nr;
				int inc=cal(x,y,H)-cal(x,y,las);
				sum-=inc;su[x]-=inc;las=dep[x];
				if(las<=dep[nr]) qr--;
			}
			su[nr]=cal(nr,nr,H);
			sum+=su[nr];q[qr++]=nr;
		}
		while(nl<lx[i][j]) sum-=su[nl++];
		while(q[ql]<nl) ql++;
		ans+=1ll*a[i][j]*sum;
	}
}
void sol(int ub,int db,int lb,int rb){
	int W=rb-lb+1,H=db-ub+1,rv=W>H;
	if(W==1 && H==1) return;
	if(rv) swap(W,H);
	//cout<<W<<' '<<H<<'\n';
	FOR(i,ub,db)FOR(j,lb,rb) 
		rv?a[j-lb+1][i-ub+1]=A[i][j]:a[i-ub+1][j-lb+1]=A[i][j];
	//FOR(i,1,H)FOR(j,1,W) cout<<a[i][j]<<" \n"[j==W];
	int md=(H>>1)+1;
	ini_lxrx(md,H,W);
	//FOR(i,1,H)FOR(j,1,W) cout<<lx[i][j]<<'-'<<rx[i][j]<<" \n"[j==W];
	ini_g123(md,H,W);
	//FOR(i,1,W)FOR(j,1,W) cout<<cal(i,j,0)<<" \n"[j==W];
	FOR(i,1,md-1) work(i,md,H,W);
	//cout<<"ans="<<ans<<'\n';
	if(!rv) sol(ub,ub+md-2,lb,rb),sol(ub+md-1,db,lb,rb);
	else    sol(ub,db,lb,lb+md-2),sol(ub,db,lb+md-1,rb);
}
int main(){
	//freopen("1.in","r",stdin);
	scanf("%d",&n);
	FOR(i,1,n){
		scanf("%s",str+1);
		FOR(j,1,n) A[i][j]=str[j]=='#'?0:str[j]-'0';
	}
	sol(1,n,1,n);
	cout<<ans<<'\n';
}

Measured memory_bytes: 87808