Language: C++

// In the name of God

#include <iostream>
#include <algorithm>
#include <fstream>
#include <vector>
#include <deque>
#include <assert.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <math.h>
#include <bitset>
#include <iomanip>

using namespace std;

#define rep(i, n) for (int i = 0, _n = (int)(n); i < _n; ++i)
const int N = (int) 2e5 + 5, mod = (int) 0, oo = 1e9 + 9;
int n, m, mark[N], to[N], head[N], prev[N], cap[N], cnt = 0;
string mat[N];
int f(int i, int j) { return i * m + j; }
bool outer(int i, int j) {
    return (i == 0 || j == 0 || i == n - 1 || j == m - 1);
}
void add_edge(int u, int v, int uv) {
    to[cnt] = v, cap[cnt] = uv, prev[cnt] = head[u], head[u] = cnt++;
    to[cnt] = u, cap[cnt] = +0, prev[cnt] = head[v], head[v] = cnt++;
}
int dfs(int v, int sink, int flow = oo) {
    if (mark[v]++) return 0;
    if (v == sink) return flow;
    for (int e = head[v]; e != -1; e = prev[e]) {
        int u = to[e];
        if (cap[e]) {
            int x = dfs(u, sink, min(flow, cap[e]));
            if (x > 0) {
                cap[e ^ 0] -= x;
                cap[e ^ 1] += x;
                return x;
            }
        }
    }
    return 0;
}
int max_flow(int source, int sink) {
    int res = 0;
    while (true) {
        memset(mark, 0, sizeof mark);
        int x = dfs(source, sink);
        if (!x) break;
        res += x;
    }
    return res;
}
int dx[] = {0, -1, 0, 1};
int dy[] = {1, 0, -1, 0};
int32_t main() {
    memset(head, -1, sizeof head);
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> m;
    for (int i = 0; i < n; ++i)
        cin >> mat[i];
    int sink = N - 1, source = N - 2;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j) {
            for (int d = 0; d < 4; ++d) {
                int x = i + dx[d], y = j + dy[d];
                if (x >= 0 && y >= 0 && x < n && y < m) {
                    add_edge(f(i, j) << 1 | 1, f(x, y) << 1, oo);
                }
            }
            if (mat[i][j] == 'X')
                add_edge(source, f(i, j) << 1, oo);
            add_edge(f(i, j) << 1, f(i, j) << 1 | 1, 1 + oo * (mat[i][j] == 'X'));
            if (outer(i, j)) {
                if (mat[i][j] == 'X') {
                    cout << -1 << endl;
                    return 0;
                }
                add_edge(f(i, j) << 1 | 1, sink, oo);
            }
        }
    cout << max_flow(source, sink) << endl;

}


Compiler output:
a.cc: In function 'void add_edge(int, int, int)':
a.cc:31:33: error: reference to 'prev' is ambiguous
   31 |     to[cnt] = v, cap[cnt] = uv, prev[cnt] = head[u], head[u] = cnt++;
      |                                 ^~~~
In file included from /usr/include/c++/14/string:47,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:24:36: note:                 'int prev [200005]'
   24 | int n, m, mark[N], to[N], head[N], prev[N], cap[N], cnt = 0;
      |                                    ^~~~
a.cc:32:33: error: reference to 'prev' is ambiguous
   32 |     to[cnt] = u, cap[cnt] = +0, prev[cnt] = head[v], head[v] = cnt++;
      |                                 ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:24:36: note:                 'int prev [200005]'
   24 | int n, m, mark[N], to[N], head[N], prev[N], cap[N], cnt = 0;
      |                                    ^~~~
a.cc: In function 'int dfs(int, int, int)':
a.cc:37:40: error: reference to 'prev' is ambiguous
   37 |     for (int e = head[v]; e != -1; e = prev[e]) {
      |                                        ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:24:36: note:                 'int prev [200005]'
   24 | int n, m, mark[N], to[N], head[N], prev[N], cap[N], cnt = 0;
      |                                    ^~~~