#include <bits/stdc++.h>
#define meow(args...) fprintf(stderr, args)
typedef unsigned u32;
typedef long long s64;
typedef unsigned long long u64;
template<class T1, class T2> inline bool cmin(T1 &a, const T2 &b) {return b<a?(a=b, true):false;}
template<class T1, class T2> inline bool cmax(T1 &a, const T2 &b) {return a<b?(a=b, true):false;}

const int N=1500;
int n, a[N][N], b[N][N], dl[N][N], dr[N][N], ul[N][N], ur[N][N], top[N], bot[N], meet[N][N], both_s[N], both_l[N][N], both_r[N][N], both_m[N][N], only[N], queue[N];
char s[N+1];
s64 ans;
int both(int i, int j, int k) {
	cmin(k, bot[i]);
	cmin(k, bot[j]);
	return k<meet[i][j]?0:both_s[k]-both_l[k][i]-both_r[k][j]+both_m[i][j];
}
void solve(int u, int h, int l, int w, const int (*a)[N], const int (*b)[N]) {
	if(h<w) {
		std::swap(u, l);
		std::swap(h, w);
		std::swap(a, b);
	}
	if(h==1) return;
	int hu=h/2, hd=h-hu;
	solve(u, hu, l, w, a, b);
	solve(u+hu, hd, l, w, a, b);
	for(int i=0; i<hd; ++i) {
		memset(dl[i], 63, w*sizeof(int));
		memset(dr[i], 192, w*sizeof(int));
	}
	for(int i=0; i<hd; ++i)
		for(int j=0; j<w; ++j)
			if(a[u+hu+i][l+j]) {
				dl[i][j]=i?dl[i-1][j]:j;
				dr[i][j]=i?dr[i-1][j]:j;
				if(j) {
					cmin(dl[i][j], dl[i][j-1]);
					cmax(dr[i][j], dr[i][j-1]);
				}
			}
	for(int i=0; i<=hu; ++i) {
		memset(ul[i], 63, w*sizeof(int));
		memset(ur[i], 192, w*sizeof(int));
	}
	for(int i=hu; i>=0; --i)
		for(int j=w; j--; )
			if(a[u+i][l+j]) {
				ul[i][j]=i<hu?ul[i+1][j]:j;
				ur[i][j]=i<hu?ur[i+1][j]:j;
				if(j+1<w) {
					cmin(ul[i][j], ul[i][j+1]);
					cmax(ur[i][j], ur[i][j+1]);
				}
			}
	memset(bot, 192, w*sizeof(int));
	for(int i=hd; i--; )
		for(int j=w; j--; )
			if(a[u+hu+i][l+j]) {
				cmax(bot[j], i);
				if(j+1<w) cmax(bot[j], bot[j+1]);
			} else bot[j]=-1e9;
	memset(top, 63, w*sizeof(int));
	for(int i=0; i<=hu; ++i)
		for(int j=0; j<w; ++j)
			if(a[u+i][l+j]) {
				cmin(top[j], i);
				if(j) cmin(top[j], top[j-1]);
			} else top[j]=1e9;
	for(int i=0; i<w; ++i) memset(meet[i]+i, 63, (w-i)*sizeof(int));
	for(int i=0; i<hd; ++i)
		for(int j=0; j<w; ++j)
			if(dl[i][j]<=dr[i][j])
				cmin(meet[dl[i][j]][dr[i][j]], i);
	for(int i=0; i<w; ++i)
		for(int j=w; j-->i; ) {
			if(i) cmin(meet[i][j], meet[i-1][j]);
			if(j+1<w) cmin(meet[i][j], meet[i][j+1]);
		}
	for(int i=0; i<w; ++i)
		for(int j=i; j<w; ++j)
			if(meet[i][j]>bot[i]||meet[i][j]>bot[j]) meet[i][j]=1e9;
	memset(both_s, 0, hd*sizeof(int));
	for(int i=0; i<hd; ++i) {
		memset(both_l[i], 0, w*sizeof(int));
		memset(both_r[i], 0, w*sizeof(int));
	}
	for(int i=0; i<w; ++i) memset(both_m[i]+i, 0, (w-i)*sizeof(int));
	for(int i=0; i<hd; ++i)
		for(int j=0; j<w; ++j)
			if(dl[i][j]<=dr[i][j]) {
				int A=a[u+hu+i][l+j], L=dl[i][j]-1, R=dr[i][j]+1;
				both_s[i]+=A;
				if(L>=0) both_l[i][L]+=A;
				if(R<w) both_r[i][R]+=A;
				if(L>=0&&R<w) both_m[L][R]+=A;
			}
	for(int i=1; i<hd; ++i) {
		both_s[i]+=both_s[i-1];
		for(int j=0; j<w; ++j) {
			both_l[i][j]+=both_l[i-1][j];
			both_r[i][j]+=both_r[i-1][j];
		}
	}
	for(int i=0; i<hd; ++i) {
		for(int j=w-1; j--; ) both_l[i][j]+=both_l[i][j+1];
		for(int j=1; j<w; ++j) both_r[i][j]+=both_r[i][j-1];
	}
	for(int i=0; i<w; ++i)
		for(int j=i+1; j<w; ++j) both_m[i][j]+=both_m[i][j-1];
	for(int i=w-1; i--; )
		for(int j=i+1; j<w; ++j) both_m[i][j]+=both_m[i+1][j];
	for(int i=0; i<hu; ++i) {
		int *qhead=queue, *qtail=queue, sum=0;
		memset(only, 0, w*sizeof(int));
		for(int j=0, k=0, left=0; j<w; ++j)
			if(ul[i][j]<=ur[i][j]) {
				while(left<ul[i][j]) sum-=only[left++];
				while(qhead!=qtail&&*qhead<ul[i][j]) ++qhead;
				for(; k<w&&k<=ur[i][j]; ++k)
					if(top[k]<=i) {
						int last=-1;
						sum+=only[k]=both(k, k, bot[k]);
						while(qhead!=qtail) {
							int u=qtail[-1], diff=both(u, k, last);
							last=std::min(bot[u], bot[k]);
							diff-=both(u, k, last);
							sum+=diff;
							only[u]+=diff;
							if(bot[u]>bot[k]) break;
							--qtail;
						}
						*qtail++=k;
					}
				ans+=a[u+i][l+j]*sum;
			}
	}
//	for(int i=0; i<hu; ++i)
//		for(int j=0; j<w; ++j) if(a[u+i][l+j]) {
//			int *qhead=queue, *qtail=queue, sum=0;
//			bool vis[13][13]={};
//			*qtail++=i*w+j;
//			vis[i][j]=true;
//			while(qhead!=qtail) {
//				int me=*qhead++, x=me/w, y=me%w;
//				if(x>=hu) sum+=a[u+x][l+y];
//				if(y+1<w&&!vis[x][y+1]&&a[u+x][l+y+1]) *qtail++=me+1, vis[x][y+1]=true;
//				if(x+1<h&&!vis[x+1][y]&&a[u+x+1][l+y]) *qtail++=me+w, vis[x+1][y]=true;
//			}
//		}
}
int main() {
	assert(scanf("%d", &n)==1);
	for(int i=0; i<n; ++i) {
		assert(scanf("%s", s)==1);
		for(int j=0; j<n; ++j) a[i][j]=(s[j]=='#')?0:s[j]-'0';
	}
	for(int i=0; i<n; ++i) for(int j=0; j<n; ++j) b[i][j]=a[j][i];
	solve(0, n, 0, n, a, b);
	printf("%lld\n", ans);
	return 0;
}


Measured memory_bytes: 73856