Language: C++

// ConsoleApplication1.cpp : コンソール アプリケーションのエントリ ポイントを定義します。
//

//#include <bits/stdc++.h>
#include "stdafx.h"
#include <stack>
//実装が重そうな問題はある程度考えてから書く
//初期化を忘れずに（特に二分探索とか）
//コーナーケースを考えて（特に場合分けとか）
//不可解すぎるバグは配列外参照（配列の長さ）を検討

#define YES "YES"
#define NO "NO"
#define YESNO OUT(three(solve(),YES,NO))
#define ECHO OUT(solve())
#define three(A,B,C) ((A)?(B):(C))
#define FOR(i,a,b)  for(LL i=(a);i< (LL)(b);i++)
#define EFOR(i,a,b) for(LL i=(a);i<=(LL)(b);i++)
#define RFOR(i,a,b) for(LL i=(a);i>=(LL)(b);i--)
#define REP(i,b) FOR(i,zero,b)
#define EREP(i,b) EFOR(i,zero,b)
#define RREP(i,b) RFOR(i,b,zero)
#define ALL(c) c.begin(),c.end()
#define UNIQUE(c) sort(ALL(c));c.erase(unique(ALL(c)),c.end())
#define MAX(c) (*max_element(ALL(c)))
#define MIN(c) (*min_element(ALL(c)))
#define MP make_pair
#define FI first
#define SE second
#define SI(x) (LL(x.size()))
#define PB push_back
#define DEBUG(a) OUT(a)
#define DEBUG2(a,b) OUT2(a,b)
#define cat cout << __LINE__ << endl
#define OUT(a) cout << (a) << endl
#define OUT2(a,b) cout << (a) <<" "<<(b) << endl
#define zero 0LL

using namespace std;
template<typename T> inline void maximize(T &a, T b) { a = max(a, b); }
template<typename T> inline void minimize(T &a, T b) { a = min(a, b); }
template<typename T> inline bool middle(T a, T b, T c) { return b <= a && a <= c; }
template<class T> inline bool MX(T &l, const T &r) { return l<r ? l = r, 1 : 0; }
template<class T> inline bool MN(T &l, const T &r) { return l>r ? l = r, 1 : 0; }
typedef int LL;
typedef double ld;
typedef LL ut;
typedef vector<ut> VI;
typedef vector<VI> VII;
typedef pair<ut, ut> pr;
typedef pair<ut, pr> ppr;
typedef vector<pr> Vpr;
typedef vector<ppr> Vppr;
typedef priority_queue<pr, Vpr, greater<pr> > PQ;
inline void outputVI(VI x) { REP(i, SI(x)) { cout << three(i, " ", "") << x[i]; }OUT(""); }
const int SIZE1 = 5 * 1e5 + 1000;
const int SIZE2 = 5010;
const int SIZE3 = 501;
const int SIZE = SIZE3;
const LL p = 7 + 1e9;
const LL INF = 1LL << 60;
const long double EPS = 1e-7;
string s;
ut N, M, K, X, L, Y;
// ut A,B,C,D,E,F,G,H,I,J,O,P,Q,R,T,U;
VI edges[SIZE];
LL vals[SIZE], nums[SIZE], maps[SIZE2][SIZE2], answer = zero;

////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////
void init() {

}
long long ans = 0;
long long sum = 0;
bool reached[SIZE][SIZE];
int A[SIZE][SIZE];
int val[SIZE][SIZE];
LL times = 0,times2=0;
inline LL dfs(int y, int x) {
//	times++;
	if (y > N or x > N or A[y][x] == 0 or reached[y][x]) return 0;
	reached[y][x] = true;
	return dfs(y, x + 1) + dfs(y + 1, x) + A[y][x];
}
LL dfs252(int yy, int xx) {
	stack<pr> st;
	st.emplace(yy, xx);
	int ret = 0;
	while (!st.empty()) {
		int y=st.top().first, x=st.top().second;
		if (val[y][x] == 0) {
			val[y][x] = -1;
			reached[y][x] = 0;
			if ((x + 1 <= N and A[y][x + 1] and reached[y][x + 1] and reached[y - 1][x + 1] == 0)) st.emplace(y, x+1);
			if ((y + 1 <= N and A[y + 1][x] and reached[y + 1][x])) st.emplace(y + 1, x);
		}
		else {
			val[y][x] = val[y][x + 1] + val[y + 1][x];
//			if ((x + 1 <= N and A[y][x + 1] and reached[y][x + 1] and reached[y - 1][x + 1] == 0)) ret+=val[y][x + 1];
	//		if ((y + 1 <= N and A[y + 1][x] and reached[y + 1][x])) ret+=val[y][x+1];
			st.pop();
		}
	}
//	reached[y][x] = 0;
//	return  (( ? dfs2(y, x + 1) : 0) + ( : 0) + A[y][x]);
	return val[yy][xx];

}

inline LL dfs2(int y, int x) {
	//times++;
	reached[y][x] = 0;
	return  (((x + 1 <= N and A[y][x + 1] and reached[y][x + 1] and reached[y - 1][x + 1] == 0) ? dfs2(y, x + 1) : 0 )+ ((y + 1 <= N and A[y + 1][x] and reached[y + 1][x]) ? dfs2(y + 1, x) : 0) + A[y][x]);
}
long long solve2(int x) {
	REP(i, SIZE) REP(j, SIZE) reached[i][j] = 0;
	sum = 0;
	EFOR(i, 1, N) {
		//cout << "A";
		sum += dfs(x, i);
		//cout << "B";
		//EFOR(j,x,N)
		//	EFOR()
		sum -= A[x][i];
		ans += (long long)sum* (long long)A[x][i];
		//cout << ans << endl;
		reached[x][i] = false;
		if (A[x + 1][i] == 0) continue;
		if (reached[x + 1][i] == 0) continue;
		if (reached[x][i]) continue;

		sum -= dfs2(x + 1, i);
	//	cout << "C" << endl;

	}
//	cout << times << " " << times2 << endl;
	return ans;
}
long long solve() {
	cin >> N;
	EFOR(i, 1, N) {
		cin >> s;
		EFOR(j, 1, N) {
			if (s[j - 1] == '#')A[i][j] = 0;
			else A[i][j] = s[j - 1] - '0';
		}
	}
//	N = 300;
//	EFOR(i, 1, N) EFOR(j, 1, N) A[i][j] = 9;
	EFOR(i, 1, N) solve2(i);
	return ans;
}
signed main() {

	cout << solve() << endl;
	cin >> N;
	return 0;
}


Compiler output:
a.cc:5:10: fatal error: stdafx.h: No such file or directory
    5 | #include "stdafx.h"
      |          ^~~~~~~~~~
compilation terminated.