<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

527. Explode 'Em All
Time limit per test: 1.5 second(s)
Memory limit: 524288 kilobytes
input: standard
output: standard

The prime minister of Berland decided to build a new city in the country. It's hard to describe the excitement of all Berland citizens, but indeed this is great news from the economic, social and cultural standpoints.

The land in Berland is occupied almost entirely and it's very hard to find free space for construction, so it was decided to build the city on a stony terrain. The map of this terrain is represented as an nx m grid, where each cell of the grid is either an empty space or a rock.

Of course, before construction is started, the given terrain must be completely cleared from rocks. As you may guess, you were hired to complete this mission. Your goal is to destroy all rocks by dropping bombs from a plane. A bomb can be dropped on any cell of the map, and you are free to select where you want to drop each bomb. When a bomb targeted for cell (i, j) reaches the ground, it destroys all rocks in row i and also all rocks in column j of the grid. If cell (i, j) contains a rock, this rock is also destroyed.

Please help the prime minister of Berland to find the minimum number of bombs required to completely clear the given terrain from rocks.

Input
The first line of input contains two integers n and m (1 ≤ n,m ≤ 25) — the number of rows and columns correspondingly. Each of the next n lines contains m characters describing the terrain. An empty space is denoted by ".", while a rock is denoted by "*".

Output
Write a single integer to the output — the minimum numbers of bombs required for destroying all rocks on the terrain.

Example(s)
sample input
sample output
8 10
..........
..***..*.*
.*.......*
.*.......*
.*.......*
.....*****
..........
.........*
2

sample input
sample output
3 4
....
....
....
0



Note
In the first sample test it's only required to drop 2 bombs from a plane: one bomb to cell (2,2) and another bomb to cell (6, 10). Row and column indices in this explanation are 1-based.

<|response|>
1. Abridged problem statement  
You have an n×m grid (1≤n,m≤25) with some cells containing rocks (‘*’) and others empty (‘.’). Each bomb dropped at cell (i,j) destroys all rocks in row i and in column j. Find the minimum number of bombs needed to destroy every rock.

2. Key observations  
- Dropping k bombs involves at most k distinct rows and k distinct columns (you can reuse rows or columns across bombs).  
- Equivalently, we choose a set R of rows and a set C of columns so that every rock at (i,j) satisfies i∈R or j∈C. The number of bombs needed is max(|R|,|C|).  
- For any fixed R, the smallest C is the set of all columns that still have rocks in rows not in R. So we only need to enumerate subsets R of rows, compute C automatically, and take cost=max(|R|,|C|).  
- There are 2^n subsets R. With n≤25, 2^25≈33 million, which is borderline but feasible in optimized C++/Python with bit‐operations.  
- We represent each row by an m‐bit bitmask of where its rocks are. We build an array mask_union[subset_of_rows] = bitwise OR of the masks of rows in that subset. Using the “lowbit” DP trick, this table can be filled in O(2^n). Then for each R we look at mask_union[complement(R)] to see what columns remain needing coverage.

3. Full solution approach  
Step 1. Read n, m and the grid.  
Step 2 (optional). If m<n, transpose the grid (swap n,m and rewrite rows as columns) so that the dimension we enumerate over is the smaller one. This halves the work when columns<rows.  
Step 3. Build an array row_mask of length n, where row_mask[i] is an integer whose j-th bit is 1 if grid[i][j]=='*'.  
Step 4. Let N=1<<n. Allocate mask_union of size N. Initialize for single‐bit subsets: for each i in [0..n-1], mask_union[1<<i]=row_mask[i].  
Step 5. Fill mask_union for all subsets s from 1 to N-1 by:  
    lowbit = s & -s  
    mask_union[s] = mask_union[lowbit] | mask_union[s ^ lowbit]  
Step 6. Let FULL = N-1. Initialize answer = n+m (an upper bound).  
Step 7. For each subset s in [0..N-1] representing chosen rows R:  
    rows_chosen = popcount(s)  
    columns_needed = popcount(mask_union[FULL ^ s])  
    cost = max(rows_chosen, columns_needed)  
    answer = min(answer, cost)  
Step 8. Print answer.  
This runs in O(2^d · d) time where d=min(n,m) plus the transpose cost. Memory O(2^d).

4. C++ implementation with detailed comments  
```cpp
#include <bits/stdc++.h>
using namespace std;

// Fast popcount
inline int popcnt(int x) {
    return __builtin_popcount(x);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;
    vector<string> grid(n);
    for(int i = 0; i < n; i++) {
        cin >> grid[i];
    }

    // Optional transpose if columns < rows
    bool transposed = false;
    if(m < n) {
        transposed = true;
        vector<string> g2(m, string(n, '.'));
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++) {
                g2[j][i] = grid[i][j];
            }
        }
        grid = move(g2);
        swap(n, m);
    }

    // Build bitmask for each row
    vector<int> row_mask(n, 0);
    for(int i = 0; i < n; i++) {
        int mask = 0;
        for(int j = 0; j < m; j++) {
            if(grid[i][j] == '*') {
                mask |= (1 << j);
            }
        }
        row_mask[i] = mask;
    }

    int N = 1 << n;
    vector<int> mask_union(N, 0);
    // Initialize single‐row subsets
    for(int i = 0; i < n; i++) {
        mask_union[1 << i] = row_mask[i];
    }
    // Build all subsets by combining lowbit
    for(int s = 1; s < N; s++) {
        int lb = s & -s;           // lowest set bit
        if(s != lb) {
            mask_union[s] = mask_union[lb] | mask_union[s ^ lb];
        }
    }

    int FULL = N - 1;
    int answer = n + m;  // worst‐case upper bound
    // Enumerate subsets of rows
    for(int s = 0; s < N; s++) {
        int rows_chosen = popcnt(s);
        int cols_needed = popcnt(mask_union[FULL ^ s]);
        int cost = max(rows_chosen, cols_needed);
        if(cost < answer) {
            answer = cost;
        }
    }

    cout << answer << "\n";
    return 0;
}
```

5. Python implementation with detailed comments  
```python
import sys
input = sys.stdin.readline

def main():
    n, m = map(int, input().split())
    grid = [input().rstrip('\n') for _ in range(n)]

    # Optional transpose if columns < rows
    if m < n:
        # build transposed grid of size m×n
        grid = [''.join(grid[i][j] for i in range(n)) for j in range(m)]
        n, m = m, n

    # Build bitmask for each row: m bits
    row_mask = [0]*n
    for i in range(n):
        mask = 0
        for j, ch in enumerate(grid[i]):
            if ch == '*':
                mask |= 1 << j
        row_mask[i] = mask

    N = 1 << n
    mask_union = [0]*N
    # initialize single‐row subsets
    for i in range(n):
        mask_union[1<<i] = row_mask[i]

    # build all subsets by lowbit DP
    for s in range(1, N):
        lb = s & -s
        if s != lb:
            mask_union[s] = mask_union[lb] | mask_union[s ^ lb]

    FULL = N - 1
    answer = n + m  # upper bound
    # enumerate subsets of rows
    for s in range(N):
        rows_chosen = s.bit_count()
        cols_needed = mask_union[FULL ^ s].bit_count()
        cost = rows_chosen if rows_chosen >= cols_needed else cols_needed
        if cost < answer:
            answer = cost

    print(answer)

if __name__ == "__main__":
    main()
```

Explanation summary: We reduce the bombing problem to covering all '*' positions by choosing some rows R and columns C so that each '*' is in R×anything or anything×C. The number of bombs needed is max(|R|,|C|). Enumerate all subsets of rows (or columns) using bitmasks, compute the minimal needed columns for each choice, and take the minimum cost. This runs in O(2^d·d) time and O(2^d) memory with d=min(n,m).