#include<bits/stdc++.h>
using namespace std;

int N,n,m,mid,a[1510][1510],A[1510][1510],mi[1510][1510],ma[1510][1510],mp[1510][1510],bot[1510][1510],tp[1510][1510],l[1510][1510],r[1510][1510],ql,qr,sta[1510],stp,sbt;
long long ans,s0[1510],s1[1510][1510],s2[1510][1510],s3[1510][1510],nw,val[1510];
char s[1510];

long long ask(int a,int b,int l)
{
	l=min(l,min(bot[mid+1][a],bot[mid+1][b]));
	if (l<mp[a][b]) return 0;
	return s0[l]-s1[l][a+1]-s2[l][b-1]+s3[a+1][b-1];
}

void ins(int x)
{
	if (stp>=sbt) 
	{
		nw-=val[sta[stp]];
		val[sta[stp]]-=ask(sta[stp],x,min(bot[mid+1][sta[stp]],bot[mid+1][x]));
		nw+=val[sta[stp]];
	}
	while (stp>=sbt&&bot[mid+1][sta[stp]]<=bot[mid+1][x])
	{
		if (stp>sbt) 
		{
			nw-=val[sta[stp-1]];
			val[sta[stp-1]]-=ask(sta[stp-1],x,min(bot[mid+1][sta[stp-1]],bot[mid+1][x]));
			val[sta[stp-1]]+=ask(sta[stp-1],x,bot[mid+1][sta[stp]]);
			nw+=val[sta[stp-1]];
		}
		stp--;
	}
	sta[++stp]=x,nw+=(val[x]=ask(x,x,n));
}

void solve(int xl,int yl,int xr,int yr)
{
	if (xr-xl+1>=yr-yl+1)
	{
		n=xr-xl+1,m=yr-yl+1;
		for (int i=1; i<=n; i++)
			for (int j=1; j<=m; j++) a[i][j]=A[i+xl-1][j+yl-1];
	} else
	{
		n=yr-yl+1,m=xr-xl+1;
		for (int i=1; i<=n; i++)
			for (int j=1; j<=m; j++) a[i][j]=A[j+xl-1][i+yl-1];
	}
	if (n==1&&m==1) return;
	for (int i=1; i<=n; i++) tp[i][0]=n+1;
	for (int j=1; j<=m; j++) tp[0][j]=n+1;
	for (int i=1; i<=n; i++)
		for (int j=1; j<=m; j++) tp[i][j]=(a[i][j]?min(i,min(tp[i-1][j],tp[i][j-1])):n+1);
	for (int i=1; i<=n; i++) bot[i][m+1]=0;
	for (int j=1; j<=m; j++) bot[n+1][j]=0;
	for (int i=n; i; i--)
		for (int j=m; j; j--) bot[i][j]=(a[i][j]?max(i,max(bot[i+1][j],bot[i][j+1])):0);
	mid=(n+1>>1);
	for (int i=mid+1; i<=n; i++) a[i][0]=a[i][m+1]=0,l[i][0]=m+1,r[i][0]=0;
	for (int i=1; i<=m; i++)
		if (!a[mid+1][i]) l[mid+1][i]=m+1; else l[mid+1][i]=(a[mid+1][i-1]?l[mid+1][i-1]:i);
	for (int i=1; i<=m; i++)
		if (!a[mid+1][i]) r[mid+1][i]=0; else r[mid+1][i]=i;
	for (int i=mid+2; i<=n; i++)
		for (int j=1; j<=m; j++) 
			if (a[i][j]) l[i][j]=min(l[i-1][j],l[i][j-1]),r[i][j]=max(r[i-1][j],r[i][j-1]);
			else l[i][j]=m+1,r[i][j]=0;
	for (int i=0; i<=m+1; i++)
		for (int j=0; j<=m+1; j++) mp[i][j]=n+1;
	for (int i=mid+1; i<=n; i++)
		for (int j=1; j<=m; j++) 
			if (l[i][j]<=r[i][j]) mp[l[i][j]][r[i][j]]=min(mp[l[i][j]][r[i][j]],i);
	for (int i=1; i<=m; i++)
		for (int j=m; j; j--) mp[i][j]=min(mp[i][j],min(mp[i-1][j],mp[i][j+1]));
	for (int i=0; i<=n+1; i++)
	{
		s0[i]=0;
		for (int j=0; j<=m+1; j++) s1[i][j]=s2[i][j]=s3[i][j]=0;
	}
	for (int i=mid+1; i<=n; i++)
		for (int j=1; j<=m; j++) if (l[i][j]!=m+1)
			s0[i]+=a[i][j],s1[i][l[i][j]]+=a[i][j],s2[i][r[i][j]]+=a[i][j],s3[l[i][j]][r[i][j]]+=a[i][j];
	for (int i=mid+2; i<=n; i++) s0[i]+=s0[i-1];
	for (int i=mid+1; i<=n; i++)
		for (int j=m; j; j--) s1[i][j]+=s1[i-1][j]+s1[i][j+1]-s1[i-1][j+1];
	for (int i=mid+1; i<=n; i++)
		for (int j=1; j<=m; j++) s2[i][j]+=s2[i-1][j]+s2[i][j-1]-s2[i-1][j-1];
	for (int i=m; i; i--)
		for (int j=1; j<=m; j++) s3[i][j]+=s3[i+1][j]+s3[i][j-1]-s3[i+1][j-1];
	for (int i=m; i; i--) 
		if (a[mid+1][i]) mi[mid+1][i]=i,ma[mid+1][i]=(a[mid+1][i+1]?ma[mid+1][i+1]:i);
		else mi[mid+1][i]=m+1,ma[mid+1][i]=0;
	for (int i=1; i<=mid; i++) mi[i][m+1]=m+1,ma[i][m+1]=0;
	for (int i=mid; i; i--)
		for (int j=m; j; j--) 
			if (a[i][j]) mi[i][j]=min(mi[i+1][j],mi[i][j+1]),ma[i][j]=max(ma[i+1][j],ma[i][j+1]);
			else mi[i][j]=m+1,ma[i][j]=0;
	for (int i=1; i<=mid; i++)
	{
		ql=1,qr=0,stp=0,nw=0,sbt=1;
		for (int j=1; j<=m; j++) val[j]=0;
		for (int j=1; j<=m; j++) if (mi[i][j]!=m+1)
		{
			while (qr+1<=ma[i][j]) 
			{
				qr++;
				if (tp[mid+1][qr]<=i) ins(qr);
			}
			while (ql<mi[i][j]) nw-=val[ql++];
			while (sbt<=stp&&sta[sbt]<mi[i][j]) sbt++;
			ans+=nw*a[i][j];
		}
	}
	if (xr-xl+1>=yr-yl+1) solve(xl,yl,xl+xr>>1,yr),solve((xl+xr>>1)+1,yl,xr,yr);
	else solve(xl,yl,xr,yl+yr>>1),solve(xl,(yl+yr>>1)+1,xr,yr);
}

int main()
{
	scanf("%d",&N);
	for (int i=1; i<=N; i++)
	{
		scanf("%s",s+1);
		for (int j=1; j<=N; j++) A[i][j]=(s[j]=='#'?0:s[j]-'0');
	}
	solve(1,1,N,N);
	return printf("%lld\n",ans),0;
}

Measured memory_bytes: 129792