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<vector>
#include<cmath>
#include<map>
#include<cstdlib>
#include<iostream>
#include<sstream>
#include<fstream>
#include<string>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<set>
#include<stack>
#include<bitset>
#include<functional>
#include<ctime>
#include<queue>
#include<deque>
#include<complex>
#include<cassert>
using namespace std;
#define pb push_back
#define pf push_front
typedef long long lint;
typedef complex<double> P;
#define mp make_pair
#define fi first
#define se second
typedef pair<int,int> pint;
#define All(s) s.begin(),s.end()
#define rAll(s) s.rbegin(),s.rend()
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
//問題文および制約はちゃんと確認しよう！
//サイズは10^5じゃなくて2×10^5とかかもしれないし、重要な制約・条件を見落としているかも
//とりあえずサンプルを読んでから解法を考えよう？
#define N 514
string ma[N];
int dp[N][N];//どこまで戻れるか
int mx[N][N],mn[N][N];//ある層への最高・最低着地点（毎回計算する）
lint num[N],sum[N];
int main()
{
	lint out=0;
	int n;
	cin>>n;
	rep(i,n) cin>>ma[i];
	rep(i,n) rep(j,n){
		dp[i][j]=N*2;
		if(ma[i][j]=='#') continue;
		dp[i][j]=i+j;
		if(i>0) dp[i][j]=min(dp[i][j],dp[i-1][j]);
		if(j>0) dp[i][j]=min(dp[i][j],dp[i][j-1]);
	}
	for(int i=n*2-2;i>=0;i--){
		//cout<<i<<endl;
		rep(j,n+5) rep(k,min(n,i-j)+5) mx[j][k]=-N,mn[j][k]=N;
		for(int j=n-1;j>=0;j--) for(int k=n-1;k>=0;k--){
			if(j+k==i) mx[j][k]=mn[j][k]=j;
			if(j+k<i){
				if(ma[j][k]!='#'){
					if(j<n-1) mx[j][k]=max(mx[j][k],mx[j+1][k]),mn[j][k]=min(mn[j][k],mn[j+1][k]);
					if(k<n-1) mx[j][k]=max(mx[j][k],mx[j][k+1]),mn[j][k]=min(mn[j][k],mn[j][k+1]);
				}
			}
			//cout<<i<<j<<k<<mx[j][k]<<mn[j][k]<<endl;
		}
		for(int j=i-1;j>=0;j--){
			memset(num,0,sizeof(num));
			memset(sum,0,sizeof(sum));
			rep(k,n){
				int l=i-k;
				if(l>=0 && l<n && dp[k][l]<=j && ma[k][l]!='#') num[k]=(ma[k][l]-'0');
				sum[k+1]=sum[k]+num[k];
			}
			rep(k,n){
				int l=j-k;
				if(l>=0 && l<n && mx[k][l]>=mn[k][l] && ma[k][l]!='#'){
					//cout<<i<<' '<<k<<' '<<l<<' '<<mx[k][l]<<' '<<mn[k][l]<<' '<<(sum[mx[k][l]+1]-sum[mn[k][l]])<<endl;
					out+=(ma[k][l]-'0')*(sum[mx[k][l]+1]-sum[mn[k][l]]);
				}
			}
		}
	}
	cout<<out<<endl;
}