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 Rust (1.15.1) (judge verdict: Time Limit Exceeded)

use std::cmp;
use std::collections::BTreeSet;

fn main() {
    let s = std::io::stdin();
    let mut sc = Scanner { stdin: s.lock() };
    let h: usize = sc.read();
    let w: usize = sc.read();
    let m = cmp::max(h, w);
    let map: Vec<Vec<char>> = (0..h).map(|_| sc.chars()).collect();

    let mut left = vec![vec![]; m * 3];
    let mut right = vec![vec![]; m * 3];

    for x in 0..h {
        for y in 0..w {
            if map[x][y] == '#' {
                let k = m + y - x;
                let l = m + y + x;
                left[k].push((x as i64, y as i64));
                right[l].push((x as i64, y as i64));
            }
        }
    }

    let insert = |ans: &mut BTreeSet<[(i64, i64); 3]>, left: &Vec<Vec<(i64, i64)>>| {
        for (key, value) in left.iter().enumerate() {
            let n = value.len();
            for i in 0..n {
                for j in (i + 1)..n {
                    let (x1, y1) = value[i];
                    let (x2, y2) = value[j];
                    let d = (x1 - x2).abs() + (y1 - y2).abs();
                    if key + (d as usize) < left.len() {
                        for &(x3, y3) in left[key + (d as usize)].iter() {
                            let d1 = (x1 - x3).abs() + (y1 - y3).abs();
                            let d2 = (x2 - x3).abs() + (y2 - y3).abs();
                            if d1 == d && d2 == d {
                                ans.insert(tuple((x1, y1), (x2, y2), (x3, y3)));
                            }
                        }
                    }
                    if key >= (d as usize) {
                        for &(x3, y3) in left[key - (d as usize)].iter() {
                            let d1 = (x1 - x3).abs() + (y1 - y3).abs();
                            let d2 = (x2 - x3).abs() + (y2 - y3).abs();
                            if d1 == d && d2 == d {
                                ans.insert(tuple((x1, y1), (x2, y2), (x3, y3)));
                            }
                        }
                    }
                }
            }
        }
    };

    let mut ans = BTreeSet::new();
    insert(&mut ans, &left);
    insert(&mut ans, &right);
    println!("{}", ans.len());
}

fn tuple(p1: (i64, i64), p2: (i64, i64), p3: (i64, i64)) -> [(i64, i64); 3] {
    let mut v = [p1, p2, p3];
    v.sort();
    [v[0], v[1], v[2]]
}

pub struct Scanner<R> {
    stdin: R,
}

impl<R: std::io::Read> Scanner<R> {
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .stdin
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n')
            .take_while(|&b| b != b' ' && b != b'\n')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}