#include <cstdio>
#include <cstdint>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>

class union_find {
  std::vector<intmax_t> tree;

public:
  union_find(size_t n): tree(n, -1) {}

  size_t find(size_t v) {
    if (tree[v] < 0)
      return v;

    return (tree[v] = find(tree[v]));
  }

  bool unite(size_t u, size_t v) {
    u = find(u);
    v = find(v);
    if (u == v)
      return false;

    if (-tree[u] >= -tree[v])
      std::swap(u, v);

    tree[v] += tree[u];
    tree[u] = v;
    return true;
  }

  bool connected(size_t u, size_t v) {
    return (find(u) == find(v));
  }

  size_t size(size_t v) {
    return -tree[find(v)];
  }
};

size_t di[]={size_t(-1), size_t(-1), 0, 0, 1, 1};
size_t dj[]={size_t(-1), 0, size_t(-1), 1, 0, 1};

int score(const std::vector<std::vector<int>> &S, int C) {
  size_t N = S.size();

  union_find uf(N*(N+1)/2);
  for (size_t i=0; i<N; ++i) {
    for (size_t j=0; j<=i; ++j) {
      for (size_t k=0; k<6; ++k) {
        size_t ni = i+di[k];
        size_t nj = j+dj[k];
        if (!(ni < N && nj <= ni)) continue;
        if (S[i][j] != S[ni][nj]) continue;

        size_t f0 = i*(i+1)/2+j;
        size_t f1 = ni*(ni+1)/2+nj;
        uf.unite(f0, f1);
      }
    }
  }

  std::set<size_t> safe;
  for (size_t i=0; i<N; ++i) {
    for (size_t j=0; j<=i; ++j) {
      if (S[i][j] != 0) continue;
      for (size_t k=0; k<6; ++k) {
        size_t ni = i+di[k];
        size_t nj = j+dj[k];
        if (!(ni < N && nj <= ni)) continue;

        size_t f1 = ni*(ni+1)/2+nj;
        safe.insert(uf.find(f1));
      }
    }
  }

  int res = 0;
  for (size_t i=0; i<N; ++i) {
    for (size_t j=0; j<=i; ++j) {
      if (S[i][j] == 0) continue;
      size_t f0 = i*(i+1)/2+j;
      if (safe.count(uf.find(f0))) continue;
      (S[i][j] == C)? --res:++res;
    }
  }
  return res;
}

int testcase_ends() {
  size_t N;
  int C;
  scanf("%zu %d", &N, &C);
  if (N == 0 && C == 0) return 1;

  std::vector<std::vector<int>> S(N);
  for (size_t i=0; i<N; ++i) {
    S[i].resize(i+1);
    for (size_t j=0; j<=i; ++j)
      scanf("%d", &S[i][j]);
  }

  int res = -100;
  for (size_t i=0; i<N; ++i) {
    for (size_t j=0; j<=i; ++j) {
      if (S[i][j] != 0) continue;
      S[i][j] = C;
      res = std::max(res, score(S, C));
      S[i][j] = 0;
    }
  }
  printf("%d\n", res);
  return 0;
}

int main() {
  while (!testcase_ends()) {}
}

