#include #include #include #include #include #include #include using namespace std; typedef complex POS; int TABLE[10][10]; int N, C; bool IsOut(const POS& rh) { return !(0 <= rh.real() && 0 <= rh.imag() && rh.imag() < N && rh.real() <= rh.imag()); } int VISITED[10][10]; const POS DIR[] = {POS(-1,-1),POS(0,-1),POS(-1,0),POS(1,0),POS(0,1),POS(1,1)}; bool Ref(const POS& p, int num, int& cnt) { int x = p.real(); int y = p.imag(); if (IsOut(p) || VISITED[x][y]){ return false; } VISITED[x][y] = true; ++cnt; bool is_ok = false; for (int dir = 0; dir < 6; ++dir){ POS next = p + DIR[dir]; if (IsOut(next)){ continue; } if (TABLE[next.real()][next.imag()] == num){ is_ok |= Ref(next, num, cnt); } else if (TABLE[next.real()][next.imag()] == 0){ is_ok = true; } } return is_ok; } void Count(int counter[10]) { memset(VISITED, 0, sizeof(VISITED)); //copy(counter, counter + 10, ostream_iterator(cerr, ",")); for (int y = 0; y < N; ++y){ for (int x = 0; x <= y; ++x){ int c = 0; int num = TABLE[x][y]; if (num == 0 || VISITED[x][y]){ continue; } bool b = Ref(POS(x, y), num, c); //cerr << "[" << num << "," << c << "," << b << "]"; if (!b){ counter[num] += c; } } } for (int i = 1; i <= 9; ++i){ //cerr << counter[i] << " "; } //cerr << endl; } int Check() { int res = INT_MIN; for (int y = 0; y < N; ++y){ for (int x = 0; x <= y; ++x){ if (TABLE[x][y] == 0){ TABLE[x][y] = C; for (int yy = 0; yy < N; ++yy){ for (int xx = 0; xx <= yy; ++xx){ //cerr << TABLE[xx][yy] << " "; } //cerr << endl; } int counter[10]; memset(counter, 0, sizeof(counter)); Count(counter); int temp = 0; for (int i = 1; i <= 9; ++i){ if (i == C){ temp -= counter[i]; } else { temp += counter[i]; } } res = max(res, temp); TABLE[x][y] = 0; } } } return res; } main() { while (cin >> N >> C && N && C){ for (int y = 0; y < N; ++y){ for (int x = 0; x <= y; ++x){ cin >> TABLE[x][y]; } } cout << Check() << endl; } }