### constant

DXYS = [[1, 0], [0, -1], [-1, 0], [0, 1]]
MAX_K = 5
MAX_C = 6

### subroutines

def count_area(flds)
  return 0 if flds[0] != $c

  used = $hw.times.map{false}

  ar = 1
  used[0] = true
  q = [[0, 0]]

  while ! q.empty?
    x0, y0 = q.shift
    DXYS.each do |dx, dy|
      x1 = x0 + dx
      y1 = y0 + dy
      if x1 >= 0 && x1 < $w && y1 >= 0 && y1 < $h
        id = y1 * $w + x1
        if flds[id] == $c && ! used[id]
          ar += 1
          used[id] = true
          q << [x1, y1]
        end
      end
    end
  end

  ar
end

def paint(c, flds)
  c0 = flds[0]
  return if c0 == c

  flds[0] = c
  q = [[0, 0]]

  while ! q.empty?
    x0, y0 = q.shift
    DXYS.each do |dx, dy|
      x1 = x0 + dx
      y1 = y0 + dy
      if x1 >= 0 && x1 < $w && y1 >= 0 && y1 < $h
        id = y1 * $w + x1
        if flds[id] == c0
          flds[id] = c
          q << [x1, y1]
        end
      end
    end
  end
end

def count_rec(k, flds)
  c0 = flds[0]

  if k == 1
    return if c0 == $c

    paint($c, flds)
    ar = count_area(flds)
    $max_ar = ar if $max_ar < ar
    return
  end

  for c1 in (1..MAX_C)
    next if c0 == c1

    flds0 = flds.clone
    paint(c1, flds0)
    count_rec(k - 1, flds0)
  end
end

### main

loop do
  $h, $w, $c = gets.split.map(&:to_i)
  break if ($h | $w | $c) == 0

  $hw = $h * $w

  flds = []
  $h.times.each{flds += gets.split.map(&:to_i)}
  #p $flds

  $max_ar = 0
  count_rec(MAX_K, flds)

  p $max_ar
end