#ifdef LOCAL111 #else #define NDEBUG #endif #include const int INF = 1e9; using namespace std; template ostream& operator<< (ostream& os, const pair& p) { cout << '(' << p.first << ' ' << p.second << ')'; return os; } #define endl '\n' #define ALL(a) (a).begin(),(a).end() #define SZ(a) int((a).size()) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--) #define REP(i,n) FOR(i,0,n) #define RREP(i,n) for (int i=(n)-1;i>=0;i--) #define RBP(i,a) for(auto& i : a) #ifdef LOCAL111 #define DEBUG(x) cout<<#x<<": "<<(x)< void dpite(T a, T b){ for(T ite = a; ite != b; ite++) cout << (ite == a ? "" : " ") << *ite; cout << endl;} #else #define DEBUG(x) true template void dpite(T a, T b){ return; } #endif #define F first #define S second #define SNP string::npos #define WRC(hoge) cout << "Case #" << (hoge)+1 << ": " #define rangej(a,b,c) ((a) <= (c) and (c) < (b)) #define rrangej(b,c) rangej(0,b,c) template void pite(T a, T b){ for(T ite = a; ite != b; ite++) cout << (ite == a ? "" : " ") << *ite; cout << endl;} template bool chmax(T& a, T b){if(a < b){a = b; return true;} return false;} template bool chmin(T& a, T b){if(a > b){a = b; return true;} return false;} typedef long long int LL; typedef unsigned long long ULL; typedef pair P; typedef pair LP; //library typedef long long cost_t; class Edge { public: int to; cost_t cost; Edge(){ } Edge(int x,cost_t y){ to = x; cost = y; } bool operator< (const Edge& x) const { if(cost != x.cost) return cost < x.cost; return to < x.to; } bool operator> (const Edge& x) const { if(cost != x.cost) return cost > x.cost; return to > x.to; } }; class Graph { private: //const long long int INF = (long long)1e18; vector > v; public: Graph(int x){ v = vector >(x); } Graph(){ } vector& operator[](int x){ return v[x]; } const vector& operator[](int x) const { return v[x]; } int size() const { return v.size(); } void add_edge(int from, Edge e){ if(from >= size()){ v.resize(from+1); } if(e.to >= size()){ v.resize(e.to+1); } v[from].push_back(e); } void add_edge(int from, int to, cost_t cost){ add_edge(from,Edge(to,cost)); } void add_uedge(int from, int to, cost_t cost){ add_edge(from,to,cost); add_edge(to,from,cost); } }; vector dijkstra(int from, const Graph& v) { vector dist(v.size(),INF); priority_queue,greater> que; que.push(Edge(from,0)); while(!que.empty()){ Edge e = que.top(); que.pop(); if(dist[e.to] == INF){ dist[e.to] = e.cost; for(auto to : v[e.to]){ if(dist[to.to] == INF) que.push(Edge(to.to, e.cost+to.cost)); } } } return dist; } vector bellmanford(int from, const Graph& g) { vector res(g.size(),INF); res[from] = 0; bool conf = true; int cnt = 0; while(conf){ conf = false; for(int i = 0; i < g.size(); i++){ if(res[i] != INF) for(const auto& e : g[i]){ if(res[e.to] > res[i]+e.cost){ conf = true; res[e.to] = res[i]+e.cost; } } } if(cnt > g.size()+5) return vector(); cnt++; } return res; } Graph prim(const Graph g) { using T = tuple; priority_queue, greater> qu; int n = g.size(); assert(n != 0); vector added(n,false); qu.emplace(0,0,0); Graph res(n); while(!qu.empty()){ int from, to; cost_t cost; tie(cost, from, to) = qu.top(); qu.pop(); if(!added[to]){ added[to] = true; res.add_edge(from, to, cost); res.add_edge(to, from, cost); for(const auto &e : g[to]){ if(!added[e.to]){ qu.emplace(e.cost, to, e.to); } } } } return res; } vector tree_getorder(const Graph &g, int root) { vector res; function func = [&](int p, int pre){ for(auto &e : g[p]){ if(e.to != pre){ func(e.to,p); } } res.push_back(p); }; func(root,-1); return res; } vector tree_getpar(const Graph &g, int root) { vector par(g.size(),root); function func = [&](int p, int pre){ par[p] = pre; for(auto &e : g[p]){ if(e.to != pre){ func(e.to,p); } } }; func(root,root); return par; } //未検証 vector toposort(const Graph &g, int start) { int n = g.size(); vector res; res.reserve(n); vector used(n,false); function rec = [&](int p){ if(used[p]) return; used[p] = true; for(auto&& e : g[p]) { rec(e.to); } res.push_back(p); }; rec(start); return res; } //liblary void vizGraph(const Graph &g, bool with_cap = false){ ofstream ofs("./out.dot"); ofs << "digraph graph_name {" << endl; for(int i = 0; i < (int)g.size(); i++){ if (!g[i].size()) continue; for(const auto &e : g[i]){ ofs << " \"" << i << "\" -> \"" << e.to << '"'; if (with_cap) { ofs << " [ label = \"" << (e.cost == INF ? "inf" : to_string(e.cost)) << "\"];"; } ofs << endl; } } ofs << "}" << endl; ofs.close(); system("dot -T png out.dot > sample.png"); } void ios_init(){ //cout.setf(ios::fixed); //cout.precision(12); #ifdef LOCAL111 return; #endif ios::sync_with_stdio(false); cin.tie(0); } const int dx[] = {1,0,-1,0}; const int dy[] = {0,-1,0,1}; int main() { ios_init(); int n,m; while(cin >> n >> m) { vector grid(n); REP(i,n) cin >> grid[i]; vector gr(n+4,string(m+4,'x')); gr[1] = gr[n+2] = 'x'+string(m+2,'.')+'x'; REP(i,n){ gr[i+2] = "x."+grid[i]+".x"; } Graph g; vector> used(n+4,vector(m+4,false)); REP(i,n+4){ DEBUG(gr[i]); } vector costv; function dfs = [&](int x, int y) -> int{ function judge = [&](int x, int y)->bool{ return (0 <= x and x < SZ(gr) and 0 <= y and y < SZ(gr[x])); }; REP(i,SZ(used)) dpite(ALL(used[i])); if(used[x][y]) return -1; int self = costv.size(); // DEBUG(x); DEBUG(y); DEBUG(self); // queue

qu; queue

quu; // qu.emplace(x, y); int cnt = 0; function dfss = [&](int x, int y, int px, int py) -> bool{ if(gr[x][y] == '.' and used[x][y]) return true; DEBUG(x); DEBUG(y); DEBUG(gr[x][y]); DEBUG(used[x][y]); bool res = true; if(gr[x][y] == 'x'){ if(used[x][y]){ DEBUG(222222222222222); DEBUG(px); DEBUG(py); return false; } used[x][y] = true; cnt++; FOR(dx,-1,2) FOR(dy,-1,2){ if(dx == 0 and dy == 0) continue; DEBUG(x+dx); DEBUG(y+dy); DEBUG(px); DEBUG(py); if(judge(x+dx,y+dy) and (px != x+dx or py != y+dy)){ res &= dfss(x+dx,y+dy,x,y); } } }else{ if(!used[x][y]) quu.emplace(x,y); else return true; } return res; }; bool ff = dfss(x,y,-1,-1); if(ff) return -1; costv.push_back(cnt); stack

sta; while(!quu.empty()) { int x,y; tie(x, y) = quu.front(); // DEBUG(x); DEBUG(y); quu.pop(); if(!used[x][y]) { if(gr[x][y] == '.') { used[x][y] = true; REP(i,4) if(judge(x+dx[i], y+dy[i])){ quu.emplace(x+dx[i], y+dy[i]); } } else { sta.emplace(x,y); } } } while(!sta.empty()){ int x, y; tie(x, y) = sta.top(); sta.pop(); auto res = dfs(x, y); if(res != -1){ g.add_uedge(self,res,-1); } } return self; }; dfs(0,0); REP(i,SZ(used)){ dpite(ALL(used[i])); } vizGraph(g); if(SZ(g) == 0){ cout << 0 << endl; continue; } auto topo = tree_getorder(g,0); vector> dp(SZ(g),vector(2,0)); for(auto&& e : topo) { dp[e][1] = costv[e]; for(auto&& ee : g[e]) { dp[e][0] += max(dp[ee.to][0], dp[ee.to][1]); dp[e][1] += dp[ee.to][0]; } } cout << dp[0][0] << endl; } return 0; }