We need to count the number of right triangles with legs parallel to axes (i.e., axis-aligned right triangles) that have vertices at the centers of star cells. Essentially, given a set of points (i,j) on a grid (1-indexed perhaps), we want to count all triples of distinct points (A, B, C) such that they form a right triangle with a right angle at one of the vertices, and the legs are axis-aligned. In other words, the triangle's sides are parallel to the axes, meaning that the right angle occurs when one point is the intersection of a horizontal line and a vertical line through the other two points.
More precisely: For three distinct points (x1,y1), (x2,y2), (x3,y3), they form an axis-aligned right triangle if one point shares its x-coordinate with one of the other points and its y-coordinate with the third point. That is, there exists a point P such that the other two points Q and R satisfy: Q.x = P.x, Q.y ≠ P.y; R.x ≠ P.x, R.y = P.y. Then triangle PQR is right-angled at P.
Alternatively, we can think: For any two points that are aligned horizontally (same y) and any point that shares the same x with one of them and same y? Actually, typical counting: For each point that can serve as right angle vertex, count the number of stars in same row (to left and right) and same column (above and below). Then the number of triangles with right angle at that point = (# stars in same row left/right) * (# stars in same column up/down). Because any combination of a star on the same row (different column) and a star on the same column (different row) yields a triangle with right angle at the chosen point. But careful: If the other two stars are the same? Actually they are distinct because one is on the row, one on column. They could coincide? Could a point be both on same row and same column? No, that would require same row and same column i.e., same point. Since they are distinct stars, they are not the same as the right-angle vertex. So they are different and distinct from each other as long as they are not the same point. But they could be the same point? No because they are on different axes: one has same row, one same column. The only way they are the same point is if that point lies on both row and column, which would require it to be at the intersection of that row and column, i.e., the right-angle vertex itself. So distinct. So indeed triangles are counted correctly.
Thus total number of triangles = sum over all star cells of (row_count[y] - 1) * (col_count[x] - 1), where row_count[y] is number of stars in that row, col_count[x] is number of stars in that column. Wait careful: For a star at (i,j), the number of other stars in the same row = row_count[i] - 1 (including all stars in that row, both left and right). Similarly column count = col_count[j] - 1. Then number of triangles with right angle at that star = (row_count[i] - 1)*(col_count[j] - 1). Because we can pick any star on the same row (any other star) and any star on the same column (any other star) to form a right triangle.
Is that all? Check geometry: If we have three points A (right angle), B (same row, different column), C (same column, different row), then A, B, C are distinct. Indeed they form a right triangle with legs AB (horizontal) and AC (vertical), and hypotenuse BC. Are we sure that B and C are always stars? Yes. So that counts all triangles where the right angle vertex is at a star. But are there triangles where the right angle vertex is not a star? Could a right triangle be oriented such that legs are parallel to axes but the right angle vertex is not a star? Actually for the vertices of the triangle to be at star centers, all three points must be stars. So the right angle must be at one of the stars. So any such triangle is uniquely identified by its right angle vertex and the two other stars on its row and column. However, could there be a degenerate case where the triangle is degenerate (collinear) if B and C are also aligned with A? Already we require B and C distinct and not collinear? Actually if B is same row, C same column, they are not collinear with A unless B and C are also aligned? Points A, B, C: A-B horizontal, A-C vertical, so they are not collinear because vectors are orthogonal. So it's fine.
Now also note that the triangle could be counted from the perspective of each right angle vertex. Since each triangle has exactly one right angle (since triangle cannot have more than one right angle), each triangle will be counted exactly once: at its right angle vertex. So sum over vertices is correct.
But wait: Could a triangle be counted twice? Suppose a triangle is right-angled at A. B and C are other vertices. Then at vertex B, does triangle B-A-C have a right angle at B? Not generally, because AB is horizontal, BC is diagonal, so angle at B is not necessarily 90. So no double counting. So that's fine.
Thus answer = sum_{i,j: grid[i][j]='*'} (rowStars[i] - 1) * (colStars[j] - 1).
Now constraints: n,m up to 1000, so up to 10^6 cells. Counting rowStars and colStars is O(nm). Then sum is O(nm). That's fine.
Check examples:
Example1:
2 2
**
.
Coordinates (1,1) '', (1,2) '', (2,1) '', (2,2) '.'.
Rows: row1 has 2 stars, row2 has 1.
Cols: col1 has 2 stars, col2 has 1.
Now compute:
(1,1): (2-1)(2-1)=11=1
(1,2): (2-1)(1-1)=10=0
(2,1): (1-1)(2-1)=01=0
Sum=1. Output 1. OK.
Example2:
3 4
..
.**.
.*
Let's list stars:
Row1: positions (1,1), (1,4) -> row1 stars=2.
Row2: (2,2), (2,3) -> row2 stars=2.
Row3: (3,1), (3,3), (3,4) -> row3 stars=3.
Columns:
col1: (1,1),(3,1) -> 2 stars
col2: (2,2) -> 1
col3: (2,3),(3,3) -> 2
col4: (1,4),(3,4) -> 2
Now compute each star:
(1,1): (2-1)(2-1)=11=1
(1,4): (2-1)(2-1)=11=1
(2,2): (2-1)(1-1)=10=0
(2,3): (2-1)(2-1)=11=1
(3,1): (3-1)(2-1)=21=2
(3,3): (3-1)(2-1)=21=2
(3,4): (3-1)(2-1)=21=2
Sum = 1+1+0+1+2+2+2 = 9. Matches.
So approach is correct.
Now implementation: read n,m, then read grid. Compute rowCount array of size n, colCount array of size m. For each cell, if '', increment rowCount[i] and colCount[j]. Then iterate again and accumulate ans as long long (since number of triangles could be large: up to about (nm) points, each row count up to m, column up to n, product up to 10^610^6? Actually worst-case: n=m=1000, all cells are stars. Then each row has 1000 stars, each column 1000. For each star, (rowCount-1)(colCount-1) = 999*999 ~ 1e6. There are 10^6 stars, total triangles would be 10^6 * 1e6 = 10^12, which fits in 64-bit (2^63-1 ≈ 9.22e18). So long long is safe. Use 64-bit integer.
Edge cases: n=1 or m=1. In such grid, any triangle? Need at least two stars in row and two in column to get product non-zero. If n=1, rowCount for that row might be many but colCount for each column has at most 1 (since only one row), so product will be (rowCount-1)*(colCount-1) where colCount-1 might be 0 if column has exactly 1 star? Actually if there is a star, column count for that column is 1 (only that star). So (colCount-1)=0. So sum zero. Similarly m=1. So works.
Implementation details: Use 0-indexing.
Potential pitfalls: Input lines may contain '\n', we need to read correctly. Use string. Iterate i from 0 to n-1, get line, for j 0..m-1, if char is '', increment row[i] and col[j]. Then second pass over all cells, if '', ans += (row[i]-1)*(col[j]-1). Use long long.
Complexities: O(n*m) time, O(n+m) memory. Good.
Now output ans.
Thus solution is straightforward.
But wait: The problem says "right triangles with two sides parallel to the square sides". Could there be triangles where legs are parallel to axes but the right angle is not at a star? As argued, vertices must be star centers. So no.
Thus answer as above.
Now we need to produce final solution description and code.
Given that it's a typical problem, but let's ensure there is no alternative orientation: For a right triangle, the legs can be parallel to axes. That implies the triangle's sides are either horizontal, vertical, or diagonal. The right angle is at the intersection of a horizontal and vertical segment. So indeed it's exactly what we counted.
Double-check if triangles where the right angle vertex is at a star but the legs might be of zero length? No, because other stars must be distinct, so legs length > 0.
Thus done.
But before final answer, note: There is also possibility of counting triangles where the legs are aligned but the right angle vertex is not necessarily at a star if we consider points not at star centers? Problem statement: "vertices are in the centers of '*'-cells." So all vertices are stars. So only stars.
Thus final code:
#include
#include
#include
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector grid(n);
vector rowCount(n, 0);
vector colCount(m, 0);
for (int i = 0; i < n; ++i) {
cin >> grid[i];
for (int j = 0; j < m; ++j) {
if (grid[i][j] == '') {
rowCount[i]++;
colCount[j]++;
}
}
}
long long ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (grid[i][j] == '') {
ans += (long long)(rowCount[i] - 1) * (colCount[j] - 1);