#include<bits/stdc++.h>
#define fo(i,a,b)for(int i=a,_e=b;i<=_e;++i)
#define fd(i,a,b)for(int i=b,_e=a;i>=_e;--i)
#define ll long long
#define cmin(a,b)(a=min(a,b))
#define cmax(a,b)(a=max(a,b))
#define V2(x) vector<x>
#define V(x) vector<V2(x)>
#define rs resize
using namespace std;
const int N=1505;
int n;
ll ans;
V(char) a;
int top[N],bot[N],su[N];
int su0[N],su1[N][N],su2[N][N],su3[N][N];
int l[N][N],r[N][N],mp[N][N];
int v[N],v2[N];
//inline int sum(int a,int b,int l){
//	int L=l;
//	return mp[a][b]<=L?L=min(L,min(bot[a],bot[b])),su0[L]-su1[L][a]-su2[L][b]+su3[a][b]:0;
//}
void work(V(char) &a){
	V(char) a2;
	if(a.size()<a[0].size()){
		a2.rs(a[0].size());
		fo(i,0,a[0].size()-1)a2[i].rs(a.size());
		fo(i,0,a.size()-1)
			fo(j,0,a[0].size()-1)
				a2[j][i]=a[i][j];
		a=a2;a2.clear();
	}
	if(a.size()==1)return;
	V(char) u,d;
	int h=a.size()-1>>1,n=a.size()-h-1,m=a[0].size();
	u.rs(h+1);
	d.rs(n);
	fo(i,0,h)u[i]=a[i];
	fo(i,h+1,a.size()-1)d[i-(h+1)]=a[i];
	
	fo(i,0,m-1)fo(j,0,m-1)mp[i][j]=N;
	
	fo(i,0,m-1)bot[i]=-N;
	fd(i,0,n-1)fd(j,0,m-1){
		if(!d[i][j]){bot[j]=-N;continue;}
		cmax(bot[j],i);
		if(j<m-1)cmax(bot[j],bot[j+1]);
	}
	fo(i,0,n-1)fo(j,0,m-1){
		l[i][j]=N;r[i][j]=-N;
		if(!d[i][j])continue;
		if(!i)l[i][j]=r[i][j]=j;
		if(i)cmin(l[i][j],l[i-1][j]),cmax(r[i][j],r[i-1][j]);
		if(j)cmin(l[i][j],l[i][j-1]),cmax(r[i][j],r[i][j-1]);
		cmin(mp[l[i][j]][r[i][j]],i);
	}
	fo(i,0,m-1)
		fd(j,i,m-1){
			if(i)cmin(mp[i][j],mp[i-1][j]);
			if(j<m-1)cmin(mp[i][j],mp[i][j+1]);
		}
	fo(i,0,m-1){
		mp[i][i]=0;
		fo(j,i+1,m-1)
			if(mp[i][j]>min(bot[i],bot[j]))
				mp[i][j]=N;
	}
	fo(i,0,m-1)memset(su3[i],0,4*m);
	fo(i,0,n-1){
		su0[i]=0;
		memset(su1[i],0,4*m);
		memset(su2[i],0,4*m);
		fo(j,0,m-1)if(l[i][j]<=r[i][j]){
			su0[i]+=d[i][j];
			if(l[i][j])su1[i][l[i][j]-1]+=d[i][j];
			if(r[i][j]<m-1)su2[i][r[i][j]+1]+=d[i][j];
			if(l[i][j]&&r[i][j]<m-1)su3[l[i][j]-1][r[i][j]+1]+=d[i][j];
		}
		fd(j,0,m-2)su1[i][j]+=su1[i][j+1];
		fo(j,1,m-1)su2[i][j]+=su2[i][j-1];
		if(i){
			su0[i]+=su0[i-1];
			fo(j,0,m-1)su1[i][j]+=su1[i-1][j],su2[i][j]+=su2[i-1][j];
		}
	}
	fd(i,0,m-1)fo(j,i,m-1){
		su3[i][j]+=su3[i][j-1];
		if(i<m-1)su3[i][j]+=su3[i+1][j]-su3[i+1][j-1];
	}
	int L;
	#define sum(a,b,l)(L=l,mp[a][b]<=L?L=min(L,min(bot[a],bot[b])),su0[L]-su1[L][a]-su2[L][b]+su3[a][b]:0)
	fo(i,0,m-1)d[0][i]?su[i]=sum(i,i,n-1):0,top[i]=N;
	fo(i,0,h+1)fo(j,0,m-1){
		if(!a[i][j]){top[j]=N;continue;}
		cmin(top[j],i);
		if(j)cmin(top[j],top[j-1]);
	}
	fd(i,0,h+1)fd(j,0,m-1){
		l[i][j]=N;r[i][j]=-N;
		if(!a[i][j])continue;
		if(i==h+1)l[i][j]=r[i][j]=j;
		if(i<h+1)cmin(l[i][j],l[i+1][j]),cmax(r[i][j],r[i+1][j]);
		if(j<m-1)cmin(l[i][j],l[i][j+1]),cmax(r[i][j],r[i][j+1]);
	}
	fo(i,0,h){
		int le=1,ri=0,tot=0,l2=0,r2=-1,las,dt;
		fo(j,0,m-1)v2[j]=0;
		fo(j,0,m-1)if(l[i][j]<=r[i][j]){
			fo(y,r2+1,r[i][j])if(top[y]<=i){
				las=N;
				for(;le<=ri&&bot[v[ri]]<=bot[y];tot+=dt,v2[v[ri]]+=dt,las=v[ri],--ri)
					dt=-sum(v[ri],y,bot[v[ri]])+(las<N?sum(v[ri],y,bot[las]):0);
				if(le<=ri){
					dt=-sum(v[ri],y,bot[y])+(las<N?sum(v[ri],y,bot[las]):0);
					tot+=dt;v2[v[ri]]+=dt;
				}
				v[++ri]=y;v2[y]=su[y];
				tot+=su[y];
			}
			fo(y,l2,l[i][j]-1)
				if(tot-=v2[y],le<=ri&&v[le]==y)++le;
			l2=l[i][j];r2=r[i][j];
			ans+=tot*a[i][j];
		}
	}
	a.clear();
	work(u);work(d);
}
int main(){
	scanf("%d",&n);
	a.rs(n);
	fo(i,0,n-1){
		scanf("\n");
		a[i].rs(n);
		fo(j,0,n-1)a[i][j]=getchar(),(a[i][j]=='#'?a[i][j]=0:a[i][j]-='0');
	}
	work(a);
	cout<<ans;
}

Measured memory_bytes: 47872