On a certain meeting of a ruling party "A" minister Pavel suggested to improve the sewer system and to create a new pipe in the city.

The city is an n × m rectangular squared field. Each square of the field is either empty (then the pipe can go in it), or occupied (the pipe cannot go in such square). Empty squares are denoted by character '.', occupied squares are denoted by character '#'.

The pipe must meet the following criteria:  the pipe is a polyline of width 1,  the pipe goes in empty squares,  the pipe starts from the edge of the field, but not from a corner square,  the pipe ends at the edge of the field but not in a corner square,  the pipe has at most 2 turns (90 degrees),  the border squares of the field must share exactly two squares with the pipe,  if the pipe looks like a single segment, then the end points of the pipe must lie on distinct edges of the field,  for each non-border square of the pipe there are exacly two side-adjacent squares that also belong to the pipe,  for each border square of the pipe there is exactly one side-adjacent cell that also belongs to the pipe. 

Here are some samples of allowed piping routes: 

           ....#            ....#            .*..#

           *****            ****.            .***.

           ..#..            ..#*.            ..#*.

           #...#            #..*#            #..*#

           .....            ...*.            ...*.



Here are some samples of forbidden piping routes: 

           .**.#            *...#            .*.*#

           .....            ****.            .*.*.

           ..#..            ..#*.            .*#*.

           #...#            #..*#            #*.*#

           .....            ...*.            .***.



In these samples the pipes are represented by characters ' * '.

You were asked to write a program that calculates the number of distinct ways to make exactly one pipe in the city. 

The two ways to make a pipe are considered distinct if they are distinct in at least one square.


-----Input-----

The first line of the input contains two integers n, m (2 ≤ n, m ≤ 2000) — the height and width of Berland map.

Each of the next n lines contains m characters — the map of the city. 

If the square of the map is marked by character '.', then the square is empty and the pipe can through it. 

If the square of the map is marked by character '#', then the square is full and the pipe can't through it.


-----Output-----

In the first line of the output print a single integer — the number of distinct ways to create a pipe.


-----Examples-----
Input
3 3
...
..#
...

Output
3
Input
4 2
..
..
..
..

Output
2

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

Output
4


-----Note-----

In the first sample there are 3 ways to make a pipe (the squares of the pipe are marked by characters ' * '): 

       .*.        .*.        ...

       .*#        **#        **#

       .*.        ...        .*.

Difficulty: HARD
Tags: Sorting

Combinatorics

Implementation

Complete search

Dynamic programming
Skill types: Dynamic programming

Sorting

Complete search

Starter code:

Solutions:
(n, m) = [int(x) for x in input().split()]
a = [[int(c == '.') for c in input()] for i in range(n)]

def rotate(a):
	n = len(a)
	m = len(a[0])
	b = [[0] * n for i in range(m)]
	for i in range(n):
		for j in range(m):
			b[j][n - 1 - i] = a[i][j]
	return b

def calc(a):
	n = len(a)
	m = len(a[0])
	alive = a[0][:]
	(alive[0], alive[m - 1]) = (0, 0)
	(ans_l, ans_r, ans_u) = (0, 0, 0)
	ans_bs = [0] * m
	for i in range(1, n - 1):
		s = 0
		for j in range(1, m - 1):
			if a[i][j]:
				if alive[j]:
					ans_u += s - alive[j - 1]
				ans_bs[j] += s
				s += alive[j]
			else:
				s = 0
				ans_bs[j] = 0
				alive[j] = 0
		if a[i][m - 1]:
			ans_r += s
		s = 0
		for j in range(m - 2, 0, -1):
			if a[i][j]:
				if alive[j]:
					ans_u += s - alive[j + 1]
				ans_bs[j] += s
				s += alive[j]
			else:
				s = 0
				ans_bs[j] = 0
				alive[j] = 0
		if a[i][0]:
			ans_l += s
	ans_u //= 2
	ans_b = sum((a[n - 1][i] * (ans_bs[i] + alive[i]) for i in range(1, m - 1)))
	return (ans_l, ans_r, ans_u, ans_b)
ans = 0
(ans_l, ans_r, ans_u, ans_b) = calc(a)
ans += ans_l + ans_r + ans_u + ans_b
a = rotate(a)
(ans_l, _, ans_u, ans_b) = calc(a)
ans += ans_l + ans_u + ans_b
a = rotate(a)
(ans_l, _, ans_u, _) = calc(a)
ans += ans_l + ans_u
a = rotate(a)
(_, _, ans_u, _) = calc(a)
ans += ans_u
print(ans)