You are given a description of a depot. It is a rectangular checkered field of n × m size. Each cell in a field can be empty (".") or it can be occupied by a wall ("*").
You have one bomb. If you lay the bomb at the cell (x, y), then after triggering it will wipe out all walls in the row x and all walls in the column y.
You are to determine if it is possible to wipe out all walls in the depot by placing and triggering exactly one bomb. The bomb can be laid both in an empty cell or in a cell occupied by a wall.
Input
The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and columns in the depot field.
The next n lines contain m symbols "." and "" each — the description of the field. j-th symbol in i-th of them stands for cell (i, j). If the symbol is equal to ".", then the corresponding cell is empty, otherwise it equals "" and the corresponding cell is occupied by a wall.
Output
If it is impossible to wipe out all walls by placing and triggering exactly one bomb, then print "NO" in the first line (without quotes).
Otherwise print "YES" (without quotes) in the first line and two integers in the second line — the coordinates of the cell at which the bomb should be laid. If there are multiple answers, print any of them.
Examples
Input
3 4
...
....
...
Output
YES
1 2
Input
3 3
..*
.*.
*..
Output
NO
Input
6 5
....
....
....
....
..*..
Output
YES
3 3
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to determine if there exists a cell (i,j) such that setting off a bomb at that cell will destroy all walls. The bomb destroys all walls in row i and column j. Walls not in row i or column j remain. So we need that all walls are in the union of row i and column j. Equivalently, for any wall at (r,c), we must have either r = i or c = j.
Given n,m up to 1000, we can count total number of walls, and for each row count walls, each column count walls. For a candidate (i,j), the bomb would destroy walls in row i + column j, but those counted twice? Actually walls at intersection (i,j) are counted once. So total destroyed = rowWalls[i] + colWalls[j] - (if cell (i,j) is wall then 1 else 0). We need this to equal totalWalls. So we need rowWalls[i] + colWalls[j] - (a[i][j]=='*') == totalWalls.
We can compute rowWalls and colWalls arrays. Then iterate over all (i,j) and check if condition holds. But n,m up to 1000, so total cells up to 1e6, checking all is okay (1e6). But careful: reading input, storing grid maybe as list of strings or boolean matrix. We'll need to know if cell (i,j) is wall for the adjustment. So we can store grid. Complexity O(n*m) which is fine.
Edge case: totalWalls = 0? Then any cell works? Let's see: If there are no walls, then bomb destroys nothing, but condition still holds because destroyed walls = 0 equals totalWalls. However the problem says "wipe out all walls", and there are none, so yes any placement works. But is it allowed? The bomb can be placed in any cell. So we should output YES and any coordinates, say 1 1. However, note that the bomb still destroys all walls in row and column, but there are none, so fine. So we can output any. But careful: the condition rowWalls[i] + colWalls[j] - (isWall) = 0. For any cell, rowWalls[i] and colWalls[j] are 0 because totalWalls=0 implies all row and col counts are 0. So condition holds. So we can handle.
But we can also precompute: iterate all cells, track total, row counts, col counts. Then for each cell check condition.
However there might be a more efficient way: Since we need to find i,j such that for every wall (r,c), r==i or c==j. That is equivalent to: the set of walls must be subset of (i x [all columns]) union ([all rows] x j). So all walls must lie on a cross. We can find candidate i and j. For any wall, if we pick i as its row, then for all other walls, either they have same row i or same column? Actually if we pick i as the row of first wall, then j must be such that all walls not in row i are in column j. So j must be common column among all walls not in row i. So we can try at most (first wall row, all columns that appear in walls not in that row) etc. But simpler brute-force all cells is fine.
But n,m up to 1000, grid size up to 1e6, iterating again O(nm) to check all cells is okay. Memory: store grid as list of strings (each length m) which is fine. Or we can compute row and col counts and then check each candidate by scanning again? Actually we need to check for each candidate (i,j) whether the condition holds. But we can compute rowWalls[i] + colWalls[j] - (grid[i][j]=='') and compare with total. That's O(1) per candidate. So O(n*m) total.
Thus algorithm:
Read n,m.
grid = [] of strings (0-indexed).
total = 0
row = [0]*n
col = [0]*m
For i in range(n):
line = input().strip()
grid.append(line)
for j,ch in enumerate(line):
if ch == '*':
total += 1
row[i] += 1
col[j] += 1
For i in range(n):
for j in range(m):
destroyed = row[i] + col[j] - (1 if grid[i][j]=='*' else 0)
if destroyed == total:
print("YES")
print(i+1, j+1) # because output uses 1-indexed
return
If none found, print "NO".
That's it.
But wait: There is a nuance: The bomb destroys all walls in the row and column. Does that include walls that are in row i but not in column j? Yes. And walls in column j but not row i. So destroyed count = rowWalls[i] + colWalls[j] - double counted intersection. So condition correct.
Edge Cases:
n=1,m=1: If wall present: total=1, row[0]=1, col[0]=1. Check (0,0): destroyed = 1+1-1=1 -> YES. If empty: total=0, row[0]=0, col[0]=0; destroyed =0+0-0=0 -> YES. So output YES with (1,1). Fine.
n=1,m=2: If grid "*." -> total=1, row[0]=1, col[0]=1, col[1]=0. Check (0,0): 1+1-1=1 -> YES. (0,1): 1+0-0=1? row[0]=1, col[1]=0, cell (0,1) is '.' so destroyed=1 -> YES as well. Actually bomb at (0,1) will destroy row 0 (which contains wall at (0,0)) and column 1 (empty). So it destroys the wall. So both positions work. So our condition yields YES for (0,1) as well. Good.
n=2,m=2: grid:
.
. *
We have walls at (0,0) and (1,1). total=2. row[0]=1, row[1]=1, col[0]=1, col[1]=1. Check (0,0): 1+1-1=1 -> no. (0,1): 1+1-0? row[0]=1, col[1]=1, cell (0,1) is '.' so destroyed=2? Wait row[0]=1, col[1]=1 -> 2, but does bomb at (0,1) destroy both walls? Bomb at row0 and col1. Row0 contains (0,0) wall -> destroyed. Column1 contains (1,1) wall -> destroyed. So both walls are destroyed. But is there any wall left? (1,1) is destroyed, (0,0) destroyed. So destroyed=2? But row[0]=1 (only (0,0)), col[1]=1 (only (1,1)). So destroyed=2. So condition says YES? Actually row[0] + col[1] = 2. Since cell (0,1) empty, subtract 0 => 2. So condition yields YES. But is it allowed? The bomb destroys all walls in row0 and column1. (0,0) is in row0, destroyed. (1,1) is in column1, destroyed. So yes, both destroyed. So answer is YES. But sample input 2? That one is:
3x3
..*
.*.
..
Which is like a diagonal. That total=3. row0=1, row1=1, row2=1; col0=1, col1=1, col2=1. Check any cell? For (0,1): row0=1, col1=1 -> destroyed=2 (since cell empty) not 3. (0,0): row0=1, col0=1 ->2 minus if wall? (0,0) is '.' ->2. (0,2): row0=1, col2=1 ->2 but (0,2) is '' so destroyed=1+1-1=1? Actually row0=1 (wall at (0,2) only), col2=1 (wall at (0,2) only) so destroyed =1. So no. So indeed no cell yields 3. So condition works.
Therefore algorithm is correct.
But note: We are iterating over all cells O(n*m). For n,m=1000, that's 1e6, fine.
But what about constraints: n and m up to 1000, but Python I/O and loops okay.
Potential pitfalls: Using input reading with .strip() is fine.
Double-check indexing: We'll output i+1, j+1.
Now let's implement.
But wait: There might be a scenario where totalWalls > 0 but the bomb must be placed on a cell that is a wall? Not necessarily, but our check includes both empty and wall cells.
One more nuance: What if the bomb is placed on a wall cell? The bomb destroys walls in its row and column, including that wall itself. So no problem.
Thus solution is straightforward.
But we must be careful: The depot is rectangular checkered field. "j-th symbol in i-th of them stands for cell (i, j)". So they used 1-indexing? The input description: "j-th symbol in i-th of them stands for cell (i, j)." They mention (i, j) with i row index, j column index. Likely 1-indexed in statement, but we can treat 0-indexed. Output should be coordinates probably 1-indexed as per examples: output "1 2" for first sample. So we output i+1, j+1.
Implementation details:
Read line with split: n,m = map(int, input().split())
grid = []
row = [0]*n
col = [0]*m
total = 0
For i in range(n):
s = input().strip()
grid.append(s)
for j in range(m):
if s[j] == '*':
total += 1
row[i] += 1
col[j] += 1
for i in range(n):
for j in range(m):
extra = 1 if grid[i][j] == '*' else 0
if row[i] + col[j] - extra == total:
print("YES")
print(i+1, j+1)
return # or exit
print("NO")
That solves.
But wait: There is a scenario where the depot has exactly one wall. Then row[i] where wall is, col[j] where wall is. For any cell, destroyed = row[i] + col[j] - (1 if same cell) could be 1. For instance, wall at (r,c). Check cell (r,c): row[r]=1, col[c]=1 => 1+1-1=1. Check (r, other j): row[r]=1, col[j]=0 => 1 if (r,c) destroyed? Wait if bomb at (r, j) with j != c, destroyed = row[r] + col[j] - (grid[r][j] is '.' => 0) = 1. But does bomb at (r, j) destroy the wall? Bomb destroys all walls in row r, which includes (r,c). So yes it destroys the wall. So destroyed =1, matches total. So many positions work. So algorithm yields YES.
Now consider scenario with two walls at (r1,c1) and (r2,c2) with r1 != r2 and c1 != c2. Condition is that we need row i and column j such that each wall is either in row i or column j. So if we pick i = r1, then we need all walls not in row i (i.e., wall at (r2,c2) if r2 != i) to be in column j. So j must be c2. So candidate (r1, c2). Check destroyed: row[r1] includes (r1,c1) and maybe also (r1,c2) if that is a wall? Actually (r1,c2) may or may not be wall. But destroyed count: row[r1] + col[c2] - (if (r1,c2) wall). row[r1] includes walls in row r1 (maybe only (r1,c1) if (r1,c2) is not wall). col[c2] includes walls in column c2 (maybe only (r2,c2) if (r1,c2) is not wall). So total destroyed = 2 if (r1,c2) not wall; if (r1,c2) is wall, then row[r1] also includes that wall? Actually if (r1,c2) is also a wall, then we have three walls? But we have only two walls defined, but if (r1,c2) is a wall, that would be third wall. So assuming only those two, (r1,c2) is empty, destroyed= row[r1] (1) + col[c2] (1) - 0 =2. So works. Similarly candidate (r2, c1) works. So condition is consistent.
Now if there are walls that are not on a cross shape (like three walls in L shape maybe), some can be destroyed? For example walls at (0,0), (0,1), (1,0) - total=3. Can we choose (0,0)? destroyed = row[0]=2 + col[0]=2 -1 =3 -> YES! Actually row0 has walls at (0,0),(0,1)=2; col0 has walls at (0,0),(1,0)=2; subtract 1 for (0,0) double count =>3. So bomb at (0,0) destroys all three? Yes, row0 destroys (0,0) and (0,1), col0 destroys (0,0) and (1,0) -> all three destroyed. So cross works. So condition is quite permissive.
Now consider walls at (0,0), (0,1), (1,1), (1,0) - a 2x2 block. total=4. Check any cell? For (0,0): row0=2, col0=2, subtract 1 =>3 <4. (0,1): row0=2, col1=2, subtract 1 =>3. (1,0): similar 3, (1,1): 3. Any other cell: row0 + col2? col2 out of range? Only columns 0,1. So max destroyed = 3, can't reach 4. So NO. Indeed no single cross covers all four corners because each cross misses one opposite corner. So condition works.
Thus algorithm is correct.
Potential performance: O(nm) time and O(nm) memory for grid. Could reduce memory by not storing grid? Because to compute extra we need to know if (i,j) is wall. However we can compute row and col, then when iterating over (i,j) we need to know if cell is wall. Without storing whole grid we could either read grid twice or store a boolean matrix. With n,m up to 1000, storing grid as list of strings is fine (1e6 characters). Alternatively, we could compute row and col, then iterate over all cells and recompute if it's wall by scanning again? But we can just keep the grid. So it's okay.
One more nuance: The input lines may contain newline, but we strip.
Let's implement.
But careful: In Python, reading 1e6 lines? Actually n lines each up to 1000 chars, total 1e6 chars, fine.
We'll use sys.stdin.read or sys.stdin.readline. Use sys.stdin.readline for speed.
We'll write:
import sys
def main():
import sys
data = sys.stdin.read().split()
# But we need to read lines that contain . and * without splitting? Actually splitting on whitespace will split each line into one token (since no spaces), but lines are like ".*..". So we can read using read().splitlines()? Better: use sys.stdin.readline.
Simpler: use sys.stdin.readline.
Implementation:
import sys
def solve():
n_m = sys.stdin.readline().split()
if not n_m:
return
n = int(n_m[0]); m = int(n_m[1])
grid = []
row = [0]*n
col = [0]m
total = 0
for i in range(n):
line = sys.stdin.readline().strip()
grid.append(line)
for j,ch in enumerate(line):
if ch == '':
total += 1
row[i] += 1
col[j] += 1
code
for i in range(n):
for j in range(m):
extra = 1 if grid[i][j] == '*' else 0
if row[i] + col[j] - extra == total:
sys.stdout.write("YES\n")
sys.stdout.write(f"{i+1} {j+1}\n")
return
sys.stdout.write("NO\n")
if name == "main":
solve()
This should work.
But wait: For n,m up to 1000, the inner loop is up to 1e6, and each check is constant. It's fine.
However, we need to ensure that for huge input, reading with .strip() might be slower but still okay.
We could also compute possible candidates more efficiently: since we need row[i] + col[j] - (grid[i][j]=='*') == total. Note that total = rowWalls in all rows = colWalls in all cols. So row[i] + col[j] - extra = total. This implies row[i] + col[j] >= total, and extra is 0 or 1. So either row[i] + col[j] = total and cell empty, or row[i] + col[j] = total+1 and cell is wall. Because if cell is wall, extra=1 so row[i] + col[j] -1 = total => row[i] + col[j] = total+1. If cell empty, then row[i] + col[j] = total. So we can search for rows and columns satisfying those sums. But brute-force works.
One more check: For total=0, row[i] + col[j] = 0, extra=0, so any cell works. Our code: row[i] and col[j] are 0, extra=0 => 0=0 -> True. So we output YES and first cell (0,0). That's fine.
Thus solution done.
But the problem may require to output coordinates with 1-indexing, as sample.
Now, I'll produce final answer.
But the instruction: "You are to write solution" but we are ChatGPT. We need to produce answer. So final output: code in Python. Add comments maybe.
I'll produce final solution with explanation.
But note: The problem might have large input, use fast I/O. We used sys.stdin.readline, that's fine.
Let's also consider using sys.stdout.write for output.
All good.
Now we need to output as final answer.
Given the conversation, we are to produce the final solution code.
I will write a comprehensive answer, explaining the approach and then provide the code.
Since the user said "You are ChatGPT, a large language model...", they want the solution. I'll provide solution.
Thus final. ```python
import sys
def solve() -> None:
data = sys.stdin.readline().split()
if not data:
return
n = int(data[0])
m = int(data[1])
code
grid = []
row_cnt = [0] * n
col_cnt = [0] * m
total = 0
for i in range(n):
line = sys.stdin.readline().strip()
grid.append(line)
for j, ch in enumerate(line):
if ch == '*':
total += 1
row_cnt[i] += 1
col_cnt[j] += 1
# Try every cell as bomb location
for i in range(n):
for j in range(m):
# number of walls destroyed = walls in row i + walls in column j
# minus one if the bomb cell itself is a wall (counted twice)
destroyed = row_cnt[i] + col_cnt[j] - (1 if grid[i][j] == '*' else 0)
if destroyed == total:
sys.stdout.write("YES\n")
sys.stdout.write(f"{i+1} {j+1}\n")
return
sys.stdout.write("NO\n")