Problem: Problem F and F2 are the same problem, but with different constraints and time limits.

We have a board divided into N horizontal rows and N vertical columns of square cells. The cell at the i-th row from the top and the j-th column from the left is called Cell (i,j). Each cell is either empty or occupied by an obstacle. Also, each empty cell has a digit written on it. If A_{i,j}= `1`, `2`, ..., or `9`, Cell (i,j) is empty and the digit A_{i,j} is written on it. If A_{i,j}= `#`, Cell (i,j) is occupied by an obstacle.

Cell Y is reachable from cell X when the following conditions are all met:

* Cells X and Y are different.
* Cells X and Y are both empty.
* One can reach from Cell X to Cell Y by repeatedly moving right or down to an adjacent empty cell.



Consider all pairs of cells (X,Y) such that cell Y is reachable from cell X. Find the sum of the products of the digits written on cell X and cell Y for all of those pairs.

Constraints

* 1 \leq N \leq 500
* A_{i,j} is one of the following characters: `1`, `2`, ... `9` and `#`.

Input

Input is given from Standard Input in the following format:


N
A_{1,1}A_{1,2}...A_{1,N}
A_{2,1}A_{2,2}...A_{2,N}
:
A_{N,1}A_{N,2}...A_{N,N}


Output

Print the sum of the products of the digits written on cell X and cell Y for all pairs (X,Y) such that cell Y is reachable from cell X.

Examples

Input

2
11
11


Output

5


Input

4
1111
11#1
1#11
1111


Output

47


Input

10
76##63##3#
8445669721
75#9542133
3#285##445
749632##89
2458##9515
5952578#77
1#3#44196#
4355#99#1#
298#63587


Output

36065


Input

10
4177143673
7#########
5#1716155#
6#4#####5#
2#3#597#6#
6#9#8#3#5#
5#2#899#9#
1#6#####6#
6#5359657#
5#########


Output

6525

Solution (in CPP):

Solution:
#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;
}