The Walk to School

Time limit6sMemory limit128 MB

Summary
Count ordered pairs of grass cells where a fixed three-segment path (east, south, east) stays entirely on grass cells.
Level

Medium6 of 10

Topics
Matrix, Simulation, Implementation, Prefix sum
Solved
No attempts yet

Problem

Luka walks to school every day. The route is always the same and has three parts.

  • First he walks exactly 14\frac{1}{4} of the whole distance straight east.
  • Then he walks 12\frac{1}{2} of the whole distance straight south.
  • Finally he walks the remaining 14\frac{1}{4} straight east again.

Luka's village is an N×NN \times N grid of equally sized square cells. Some cells are packed with thorny bushes and cannot be entered at all, while the rest are grass and Luka can walk over them freely. While walking, Luka never walks along the border between two cells.

Luka's house is at the center of one grass cell, and the school is at the center of a different grass cell. Neither position is known. Every cell Luka passes through must be grass.

Write a program that counts the pairs of positions where Luka's house and the school can be.

In the picture below, grey cells cannot be entered and white cells can. The picture shows the answer to the first example, three pairs.

Input

The first line contains the grid size NN (1≤N≤20001 \le N \le 2000).

Each of the next NN lines contains NN characters. Every character is either '.' or the lowercase letter 'x'. A '.' is a grass cell and an 'x' is a cell that cannot be entered.

The grid is aligned with the compass. The cells in the first line are the northernmost, and the cells in the first column are the westernmost.

Output

Print the number of pairs of positions where Luka's house and the school can be.

Examples3

  1. Example 1

    Input
    5
    .....
    .x.x.
    .x...
    .....
    .....
    
    Expected output
    3
    
  2. Example 2

    Input
    6
    ......
    ......
    ......
    ......
    ......
    ......
    
    Expected output
    20
    
  3. Example 3

    Input
    7
    .......
    .xx.xx.
    x...xx.
    .......
    .xx.xx.
    .xx.xx.
    .......
    
    Expected output
    2