Problem:
Score : 700 points

Problem Statement

There are some coins in the xy-plane.
The positions of the coins are represented by a grid of characters with H rows and W columns.
If the character at the i-th row and j-th column, s_{ij}, is #, there is one coin at point (i,j); if that character is ., there is no coin at point (i,j). There are no other coins in the xy-plane.

There is no coin at point (x,y) where 1\leq i\leq H,1\leq j\leq W does not hold.
There is also no coin at point (x,y) where x or y (or both) is not an integer.
Additionally, two or more coins never exist at the same point.

Find the number of triples of different coins that satisfy the following condition:

Choosing any two of the three coins would result in the same Manhattan distance between the points where they exist.

Here, the Manhattan distance between points (x,y) and (x',y') is |x-x'|+|y-y'|.
Two triples are considered the same if the only difference between them is the order of the coins.

Constraints

1 \leq H,W \leq 300

s_{ij} is # or ..

Input

Input is given from Standard Input in the following format:

H W
s_{11}...s_{1W}
:
s_{H1}...s_{HW}

Output

Print the number of triples that satisfy the condition.

Sample Input 1

5 4
#.##
.##.
#...
..##
...#

Sample Output 1

3

((1,1),(1,3),(2,2)),((1,1),(2,2),(3,1)) and ((1,3),(3,1),(4,4)) satisfy the condition.

Sample Input 2

13 27
......#.........#.......#..
#############...#.....###..
..............#####...##...
...#######......#...#######
...#.....#.....###...#...#.
...#######....#.#.#.#.###.#
..............#.#.#...#.#..
#############.#.#.#...###..
#...........#...#...#######
#..#######..#...#...#.....#
#..#.....#..#...#...#.###.#
#..#######..#...#...#.#.#.#
#..........##...#...#.#####

Sample Output 2

870

Solution in C++14 (GCC 5.4.1) (judge verdict: Time Limit Exceeded)

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll INF = 1LL << 60;
const ll MOD = 998244353;
typedef pair<int, int> P;
int x[600][600];
int y[600][600];
int main() {
	int H, W;
	cin >> H >> W;
	vector<string> s(H);
	for (int i = 0; i < H; i++) cin >> s[i];
	set<P> coin;
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			if (s[i][j] == '#') {
				int nx = j - i + 300, ny = j + i;
				x[ny][nx]++;
				y[ny][nx]++;
				coin.insert(P(nx, ny));
			}
		}
	}
	for (int i = 0; i < 600; i++) {
		for (int j = 1; j < 600; j++) {
			x[i][j] += x[i][j - 1];
		}
	}
	for (int i = 0; i < 600; i++) {
		for (int j = 1; j < 600; j++) {
			y[j][i] += y[j - 1][i];
		}
	}
	ll ans = 0;
	ll t = 0;
	for (int d = 1; d < 600; d++) {
		for (P p : coin) {
			if (coin.find(P(p.first + d, p.second)) != coin.end()) {
				if (p.second + d < 600) {
					ans += x[p.second + d][p.first + d];
					if (p.first > 0) ans -= x[p.second + d][p.first - 1];
				}
				if (p.second - d >= 0) {
					ans += x[p.second - d][p.first + d];
					if (p.first > 0) ans -= x[p.second - d][p.first - 1];
				}
			}
			if (coin.find(P(p.first, p.second + d)) != coin.end()) {
				if (p.first + d < 600) {
					ans += y[p.second + d][p.first + d];
					if (p.second > 0) ans -= y[p.second - 1][p.first + d];
				}
				if (p.first - d >= 0) {
					ans += y[p.second + d][p.first - d];
					if (p.second > 0) ans -= y[p.second - 1][p.first - d];
				}
			}
			for (int i = -1; i <= 1; i += 2) {
				if (coin.find(P(p.first + d * i, p.second)) == coin.end()) continue;
				for (int j = -1; j <= 1; j += 2) {
					if (coin.find(P(p.first, p.second + d * j)) == coin.end()) continue;
					t++;
				}
			}
		}
	}
	ans -= t;
	cout << ans << endl;
 }