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: Accepted)

/// Thank you tanakh!!!
/// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let mut s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

use std::cmp;

fn main() {
    input!(h: usize, w: usize, s: [chars; h]);

    let s: Vec<Vec<usize>> = s
        .iter()
        .map(|t| t.iter().map(|&c| if c == '#' { 1 } else { 0 }).collect())
        .collect();

    let ans1 = solve(h, w, &s);
    let s: Vec<Vec<usize>> = s
        .iter()
        .map(|t| {
            let mut t = t.clone();
            t.reverse();
            t
        }).collect();
    let ans2 = solve(h, w, &s);

    let mut ans3 = count_duplicated(h, w, &s);
    println!("{}", ans1 + ans2 - ans3);
}

fn solve(h: usize, w: usize, s: &Vec<Vec<usize>>) -> usize {
    let x = cmp::max(h, w);
    let mut map = vec![vec![0; x * 3]; x * 3];
    for i in 0..h {
        for j in 0..w {
            map[i + x][j + x] = s[i][j];
        }
    }
    let z = x * 3;
    let mut sum = vec![vec![0; z + 1]; z + 1];
    for si in 0..z {
        for i in si..z {
            let j = i - si;
            sum[i + 1][j + 1] = sum[i][j] + map[i][j];
        }
    }

    for sj in 1..z {
        for j in sj..z {
            let i = j - sj;
            sum[i + 1][j + 1] = sum[i][j] + map[i][j];
        }
    }

    let mut ans = 0;
    for si in 0..h {
        for i1 in si..h {
            let j1 = i1 - si;
            if j1 >= w {
                continue;
            }
            for i2 in (i1 + 1)..h {
                let j2 = i2 - si;
                if j2 >= w {
                    continue;
                }
                if s[i1][j1] != 1 || s[i2][j2] != 1 {
                    continue;
                }
                let d = i2 - i1;
                ans += sum[x + i2 + d + 1][x + j1 + 1] - sum[x + i2][x + j1 - d];
                ans += sum[x + i1 + 1][x + j2 + d + 1] - sum[x + i1 - d][x + j2];
            }
        }
    }

    for sj in 1..w {
        for j1 in sj..w {
            let i1 = j1 - sj;
            if i1 >= h {
                continue;
            }
            for j2 in (j1 + 1)..w {
                let i2 = j2 - sj;
                if i2 >= h {
                    continue;
                }
                if s[i1][j1] != 1 || s[i2][j2] != 1 {
                    continue;
                }
                let d = i2 - i1;
                ans += sum[x + i2 + d + 1][x + j1 + 1] - sum[x + i2][x + j1 - d];
                ans += sum[x + i1 + 1][x + j2 + d + 1] - sum[x + i1 - d][x + j2];
            }
        }
    }
    ans
}

fn count_duplicated(h: usize, w: usize, s: &Vec<Vec<usize>>) -> usize {
    let x = cmp::max(h, w);

    let mut ans = 0;
    for si in 0..h {
        for i1 in si..h {
            let j1 = i1 - si;
            if j1 >= w {
                continue;
            }
            for i2 in (i1 + 1)..h {
                let j2 = i2 - si;
                if j2 >= w {
                    continue;
                }
                if s[i1][j1] != 1 || s[i2][j2] != 1 {
                    continue;
                }
                let d = i2 - i1;
                if i1 >= d {
                    ans += s[i1 - d][j2];
                }
                if j2 + d < w {
                    ans += s[i1][j2 + d];
                }
                if j1 >= d {
                    ans += s[i2][j1 - d];
                }
                if i2 + d < h {
                    ans += s[i2 + d][j1];
                }
            }
        }
    }

    for sj in 1..w {
        for j1 in sj..w {
            let i1 = j1 - sj;
            if i1 >= h {
                continue;
            }
            for j2 in (j1 + 1)..w {
                let i2 = j2 - sj;
                if i2 >= h {
                    continue;
                }
                if s[i1][j1] != 1 || s[i2][j2] != 1 {
                    continue;
                }
                let d = i2 - i1;
                if i1 >= d {
                    ans += s[i1 - d][j2];
                }
                if j2 + d < w {
                    ans += s[i1][j2 + d];
                }
                if j1 >= d {
                    ans += s[i2][j1 - d];
                }
                if i2 + d < h {
                    ans += s[i2 + d][j1];
                }
            }
        }
    }
    ans
}