#include #include #include #include int table[20][20]; bool cache[20][20]; bool touch[20][20]; bool touch2[20][20]; int N, C; //#define F(x) x #define F(x) void visit(int x, int y, int p){ if(x < 0 || y < 0 || x >= N || y > x){return;} if(table[x][y] != p){return;} if(touch2[x][y]){return;} cache[x][y] = true; touch2[x][y] = true; visit(x, y - 1, p); visit(x, y + 1, p); visit(x + 1, y, p); visit(x + 1, y + 1, p); visit(x - 1, y, p); visit(x - 1, y - 1, p); } bool alive(int x, int y){ if(touch[x][y]){return cache[x][y];} touch[x][y] = true; int p = table[x][y]; if(x != 0){ if(y != 0){ if(p == table[x - 1][y - 1] && alive(x - 1, y - 1) || table[x - 1][y - 1] == 0){ F(std::cout << "1" << std::endl); return cache[x][y] = true; } } if(y != x){ if(p == table[x - 1][y] && alive(x - 1, y) || table[x - 1][y] == 0){ F(std::cout << "2" << std::endl); return cache[x][y] = true; } } } if(x != N - 1){ if(p == table[x + 1][y] && alive(x + 1, y) || table[x + 1][y] == 0){ F(std::cout << "3" << std::endl); return cache[x][y] = true; } if(p == table[x + 1][y + 1] && alive(x + 1, y + 1) || table[x + 1][y + 1] == 0){ F(std::cout << "4" << std::endl); return cache[x][y] = true; } } if(y != 0){ if(p == table[x][y - 1] && alive(x, y - 1) || table[x][y - 1] == 0){ F(std::cout << "5" << std::endl); return cache[x][y] = true; } } if(y != x){ if(p == table[x][y + 1] && alive(x, y + 1) || table[x][y + 1] == 0){ F(std::cout << "6" << std::endl); return cache[x][y] = true; } } F(std::cout << "7" << std::endl); return false; } int pointOf(){ memset(cache, 0, sizeof(cache)); memset(touch, 0, sizeof(touch)); int point = 0; for(int x = 0; x < N; x++){ for(int y = 0; y <= x; y++){ if(alive(x, y)){ memset(touch2, 0, sizeof(touch2)); visit(x, y, table[x][y]); } if(table[x][y] != 0 && !alive(x, y)){ point += (table[x][y] == C? -1: 1); } } } return point; } int main(){ while(true){ int point = -100000; std::cin >> N >> C; if(N == 0 && C == 0){return 0;} std::vector >puttableStones; for(int i = 0; i < N; i++){ for(int j = 0; j <= i; j++){ std::cin >> table[i][j]; if(table[i][j] == 0){ puttableStones.push_back(std::make_pair(i, j)); } } } for(int i = 0; i < puttableStones.size(); i++){ int x = puttableStones[i].first; int y = puttableStones[i].second; table[x][y] = C; //std::cout << pointOf() << std::endl; point = std::max(point, pointOf()); table[x][y] = 0; } std::cout << point << std::endl; } }