#!/usr/bin/ruby
#
# 1174.rb: Identically Colored Panels Connection
#

### constant

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

### global variables

### subroutines

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

  used = $h.times.map{$w.times.map{false}}

  ar = 1
  used[0][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 &&
          $flds[y1][x1] == $c && ! used[y1][x1]
        ar +=1 
        used[y1][x1] = true
        q << [x1, y1]
      end
    end
  end

  ar
end

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

  c0 = $flds[0][0]
  $flds[0][0] = c
  q = [[0, 0]]
  i = 0

  while i < q.length
    x0, y0 = q[i]
    i += 1
    DXYS.each do |dx, dy|
      x1 = x0 + dx
      y1 = y0 + dy
      if x1 >= 0 && x1 < $w && y1 >= 0 && y1 < $h && $flds[y1][x1] == c0
        q << [x1, y1]
      end
    end
  end

  q
end

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

  if k >= MAX_K
    return if c0 == $c

    changed = paint($c)
    ar = count_area()
    $max_ar = ar if $max_ar < ar
    changed.each{|x, y| $flds[y][x] = c0}
    return
  end

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

    changed = paint(c1)
    count_rec(k + 1)
    changed.each{|x, y| $flds[y][x] = c0}
  end
end

### main

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

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

  $max_ar = 0
  count_rec(1)

  p $max_ar
end