#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;
class HexagonalBoard { public:
typedef pair<int,int> vert;
int minColors(vector <string> board)
{
int H = board.size();
int W = board[0].size();
map<vert, vector<vert>> G;
bool has_edge = false;
for(int y=0; y<H; ++y)
for(int x=0; x<W; ++x) if(board[y][x]=='X') {
G[vert(y,x)];
for(int Y=0; Y<H; ++Y)
for(int X=0; X<W; ++X) if(board[Y][X]=='X')
if(juxt(y,x,Y,X)) {
has_edge = true;
G[vert(y,x)].emplace_back(Y,X);
}
}
if(G.empty())
return 0;
if(!has_edge)
return 1;
return two(G) ? 2 : 3;
}
bool juxt(int y, int x, int Y, int X)
{
if(y==Y && x==X)
return false;
if(y==Y)
return abs(x-X) == 1;
if(y+1==Y)
return x-1==X || x==X;
if(Y+1==y)
return X-1==x || X==x;
return false;
}
bool two(map<vert, vector<vert>>& G)
{
map<vert, int> C;
for(auto it=G.begin(); it!=G.end(); ++it)
if(!C.count(it->first))
if(!rec(it->first, G, C, 0))
return false;
return true;
}
bool rec(vert v, map<vert, vector<vert>>& G, map<vert,int>& C, int c)
{
if(C.count(v))
return C[v] == c;
C[v] = c;
auto& ne = G[v];
for(int i=0; i<ne.size(); ++i)
if(!rec(ne[i], G, C, c^1))
return false;
return true;
}
};
// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
{ ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
{ os << "{ ";
for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
bool ok = (Expected == Received);
if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl;
cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END verify_case(_, HexagonalBoard().minColors(board));}
int main(){
CASE(0)
string board_[] = {"---",
"---",
"---"}
;
vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_));
int _ = 0;
END
CASE(1)
string board_[] = {"-X--",
"---X",
"----",
"-X--"};
vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_));
int _ = 1;
END
CASE(2)
string board_[] = {"XXXX",
"---X",
"---X",
"---X"};
vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_));
int _ = 2;
END
CASE(3)
string board_[] = {"XX-XX--"
,"-XX-XXX"
,"X-XX--X"
,"X--X-X-"
,"XX-X-XX"
,"-X-XX-X"
,"-XX-XX-"};
vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_));
int _ = 3;
END
/*
CASE(4)
string board_[] = ;
vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_));
int _ = ;
END
CASE(5)
string board_[] = ;
vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_));
int _ = ;
END
*/
}
// END CUT HERE