#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>

using namespace std;
typedef long long ll;
int n;
int a[1505][1505];
char ss[1505];
int L[1505][1505],R[1505][1505];
ll f[1505][1505];
ll ans;
int main() {
	scanf("%d",&n);
	for(int i=1;i<=n;i++) {
		scanf("%s",&ss[1]);
		for(int j=1;j<=n;j++) {
			a[i][j] = (ss[j]=='#'?0:ss[j]-'0');
		}
	}
	for(int i=n;i>=1;i--) {
		for(int j=n;j>=1;j--) {
			if(a[i][j]) {
				bool mk1 = a[i+1][j] , mk2 = a[i][j+1];
				if( (!mk1) && (!mk2) ) {
					L[i][j] = R[i][j] = j;
					f[i][j] = 0;
					for(int k=i+1;k<=n;k++) L[k][j] = n+1,R[k][j] = 0;
				} else if(!mk1) {
					f[i][j] = f[i][j+1];
					L[i][j] = j; R[i][j] = R[i][j+1];
					for(int k=i+1;k<=n;k++) L[k][j] = L[k][j+1] , R[k][j] = R[k][j+1];
				} else if(!mk2) {
					f[i][j] = f[i+1][j];
					L[i][j] = R[i][j] = j;
				} else {
					int msr = 0;
					int yoo = f[i][j+1] + f[i+1][j];
					L[i][j] = j; R[i][j] = R[i][j+1];
					for(int k=i+1;k<=n;k++) {
						if( (L[k][j+1]>msr) && (L[k][j+1]<=R[k][j])  ) yoo -= f[k][L[k][j+1]];
						msr = max(msr,R[k][j]);
						L[k][j] = min(L[k][j],L[k][j+1]);
						R[k][j] = max(R[k][j],R[k][j+1]);
					}
					f[i][j] = yoo;
				}
				ans += f[i][j] * a[i][j];
				f[i][j] += a[i][j];
			} else L[i][j] = n+1,R[i][j] = 0;
		}
	}
	printf("%lld",ans);
}