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

using ll=int64_t;
//#define int ll

#define FOR(i,a,b) for(int i=int(a);i<int(b);i++)
#define REP(i,b) FOR(i,0,b)
#define MP make_pair
#define PB push_back
#define ALL(x) x.begin(),x.end()
#ifdef LOCAL
#define cerr (cerr<<"-- line "<<__LINE__<<" -- ")
#else
class CerrDummy{}cerrDummy;
template<class T>
CerrDummy& operator<<(CerrDummy&cd,const T&){
	return cd;
}
using charTDummy=char;
using traitsDummy=char_traits<charTDummy>;
CerrDummy& operator<<(CerrDummy&cd,basic_ostream<charTDummy,traitsDummy>&(basic_ostream<charTDummy,traitsDummy>&)){
	return cd;
}
#define cerr cerrDummy
#endif
#define REACH cerr<<"reached"<<endl
#define DMP(x) cerr<<#x<<":"<<x<<endl
#define ZERO(x) memset(x,0,sizeof(x))
#define ONE(x) memset(x,-1,sizeof(x))

using pi=pair<int,int>;
using vi=vector<int>;
using ld=long double;

template<class T,class U>
ostream& operator<<(ostream& os,const pair<T,U>& p){
	os<<"("<<p.first<<","<<p.second<<")";
	return os;
}

template<class T>
ostream& operator <<(ostream& os,const vector<T>& v){
	os<<"{";
	REP(i,(int)v.size()){
		if(i)os<<",";
		os<<v[i];
	}
	os<<"}";
	return os;
}

ll read(){
	ll i;
	scanf("%"  SCNd64,&i);
	return i;
}

void printSpace(){
	printf(" ");
}

void printEoln(){
	printf("\n");
}

void print(ll x,int suc=1){
	printf("%" PRId64,x);
	if(suc==1)
		printEoln();
	if(suc==2)
		printSpace();
}

string readString(){
	static char buf[3341000];
	scanf("%s",buf);
	return string(buf);
}

char* readCharArray(){
	static char buf[3341000];
	static int bufUsed=0;
	char* ret=buf+bufUsed;
	scanf("%s",ret);
	bufUsed+=strlen(ret)+1;
	return ret;
}

template<class T,class U>
void chmax(T& a,U b){
	if(a<b)
		a=b;
}

template<class T,class U>
void chmin(T& a,U b){
	if(b<a)
		a=b;
}

template<class T>
T Sq(const T& t){
	return t*t;
}

#define CAPITAL
void Yes(bool ex=true){
	#ifdef CAPITAL
	cout<<"YES"<<endl;
	#else
	cout<<"Yes"<<endl;
	#endif
	if(ex)exit(0);
}
void No(bool ex=true){
	#ifdef CAPITAL
	cout<<"NO"<<endl;
	#else
	cout<<"No"<<endl;
	#endif
	if(ex)exit(0);
}

const ll infLL=LLONG_MAX/3;

#ifdef int
const int inf=infLL;
#else
const int inf=INT_MAX/2-100;
#endif

ll ans;

const int Nmax=1510;
int h,w;
int lf[Nmax][Nmax],rg[Nmax][Nmax];
int p[Nmax],q[Nmax][Nmax],r[Nmax][Nmax],s[Nmax][Nmax],meet[Nmax][Nmax],tp[Nmax],bt[Nmax],tpBuf[Nmax][Nmax],btBuf[Nmax][Nmax];
int use[Nmax],pr[Nmax],high[Nmax],has[Nmax];

int BothReachable(int a,int b,int l){
	assert(l<=bt[a]);
	assert(l<=bt[b]);
	if(meet[a][b]>l)
		return 0;
	int res=p[l];
	res+=s[a+1][b-1];
	res-=q[l][a+1];
	res-=r[l][b-1];
	return res;
}

int Reachable(int a){
	int l=bt[a];
	int res=p[l];
	if(a+1<w)
		res-=q[l][a+1];
	if(a-1>=0)
		res-=r[l][a-1];
	return res;
}

void rec(vector<string> origG){
	h=origG.size();
	w=origG[0].size();
	if(h<w){
		swap(h,w);
		vector<string> tmp(h,string(w,'.'));
		REP(i,h)REP(j,w)
			tmp[i][j]=origG[j][i];
		origG.swap(tmp);
	}
	if(h==1)
		return;
	
	int h1=h/2,h2=h-h1;
	vector<string> g1(h1,string(w,'.')),g2(h2,string(w,'.'));
	REP(i,h1)REP(j,w)
		g1[i][j]=origG[i][j];
	REP(i,h2)REP(j,w)
		g2[i][j]=origG[i+h1][j];
	
	REP(i,h2)
		p[i]=0;
	REP(i,h2)REP(j,w){
		q[i][j]=0;
		r[i][j]=0;
	}
	REP(i,w)REP(j,w){
		s[i][j]=0;
		meet[i][j]=inf;
	}
	
	REP(i,h2)REP(j,w){
		lf[i][j]=inf;
		rg[i][j]=-inf;
		if(g2[i][j]!='#'){
			if(i==0){
				lf[i][j]=j;
				rg[i][j]=j;
			}else{
				chmin(lf[i][j],lf[i-1][j]);
				chmax(rg[i][j],rg[i-1][j]);
			}
			if(j>0){
				chmin(lf[i][j],lf[i][j-1]);
				chmax(rg[i][j],rg[i][j-1]);
			}
		}
		if(lf[i][j]<=rg[i][j]){
			int z=g2[i][j]-'0';
			p[i]+=z;
			q[i][lf[i][j]]+=z;
			r[i][rg[i][j]]+=z;
			s[lf[i][j]][rg[i][j]]+=z;
			chmin(meet[lf[i][j]][rg[i][j]],i);
		}
	}
	
	FOR(i,1,h2)
		p[i]+=p[i-1];
	
	REP(i,h2)for(int j=w-2;j>=0;j--)
		q[i][j]+=q[i][j+1];
	FOR(i,1,h2)for(int j=w-1;j>=0;j--)
		q[i][j]+=q[i-1][j];
	
	REP(i,h2)FOR(j,1,w)
		r[i][j]+=r[i][j-1];
	FOR(i,1,h2)REP(j,w)
		r[i][j]+=r[i-1][j];
	
	for(int i=w-1;i>=0;i--)FOR(j,1,w)
		s[i][j]+=s[i][j-1];
	for(int i=w-2;i>=0;i--)REP(j,w)
		s[i][j]+=s[i+1][j];
	
	REP(i,w)for(int j=w-2;j>=0;j--)
		chmin(meet[i][j],meet[i][j+1]);
	FOR(i,1,w)for(int j=w-1;j>=0;j--)
		chmin(meet[i][j],meet[i-1][j]);
	
	REP(i,h1)REP(j,w){
		tpBuf[i][j]=inf;
		if(g1[i][j]!='#'){
			tpBuf[i][j]=i;
			if(i>0)
				chmin(tpBuf[i][j],tpBuf[i-1][j]);
			if(j>0)
				chmin(tpBuf[i][j],tpBuf[i][j-1]);
		}
	}
	REP(i,w)
		tp[i]=tpBuf[h1-1][i];
	
	for(int i=h2-1;i>=0;i--)for(int j=w-1;j>=0;j--){
		btBuf[i][j]=-inf;
		if(g2[i][j]!='#'){
			btBuf[i][j]=i;
			if(i+1<h2)
				chmax(btBuf[i][j],btBuf[i+1][j]);
			if(j+1<w)
				chmax(btBuf[i][j],btBuf[i][j+1]);
		}
	}
	REP(i,w)
		bt[i]=btBuf[0][i];
	
	for(int i=h1-1;i>=0;i--)for(int j=w-1;j>=0;j--){
		lf[i][j]=inf;
		rg[i][j]=-inf;
		if(g1[i][j]!='#'){
			if(i==h1-1){
				lf[i][j]=j;
				rg[i][j]=j;
			}else{
				chmin(lf[i][j],lf[i+1][j]);
				chmax(rg[i][j],rg[i+1][j]);
			}
			if(j+1<w){
				chmin(lf[i][j],lf[i][j+1]);
				chmax(rg[i][j],rg[i][j+1]);
			}
		}
	}
	
	REP(i,h1){
		int m=0;
		REP(j,w){
			if(g1[h1-1][j]!='#'&&g2[0][j]!='#'){
				if(tp[j]<=i){
					use[m++]=j;
				}
			}
		}
		int cnt=0;
		int b=0,e=0;
		int highB=0,highE=0;
		REP(j,w)if(lf[i][j]<=rg[i][j]){
			while(e<m&&use[e]<=rg[i][j]){
				has[e]=Reachable(use[e]);
				cnt+=has[e];
				int last=-1;
				while(highB<highE){
					int pos=high[highE-1];
					int z=min(bt[use[pos]],bt[use[e]]);
					int v=BothReachable(use[pos],use[e],z)-BothReachable(use[pos],use[e],last);
					has[pos]-=v;
					cnt-=v;
					if(z==bt[use[e]])
						break;
					last=z;
					highE--;
				}
				high[highE++]=e++;
			}
			while(b<m&&use[b]<lf[i][j]){
				if(b==high[highB])
					highB++;
				cnt-=has[b++];
			}
			ans+=cnt*(g1[i][j]-'0');
		}
	}
	
	rec(g1);
	rec(g2);
}

signed main(){
	int n=read();
	vector<string> g(n);
	REP(i,n)
		g[i]=readString();
	rec(g);
	print(ans);
}


Measured memory_bytes: 71272