function bomb(y,x,first,color){
   var dy=[-1,0,0,1];
   var dx=[0,-1,1,0];
   for(var i=0;i<4;i++){
      var X=x+dx[i];
      var Y=y+dy[i];
      if(X<0 || Y<0 || X>=w || Y>=h)continue;
      if(yx[Y][X]==first){
         yx[Y][X]=color;
         cnt++;
         bomb(Y,X,first,color);
      }
   }
}
var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var arr=input.trim().split("\n");
var all=[];
var loop=function (ary){
   if(ary.length==4)all.push(ary);
   else{
      for(var i=1;i<=6;i++)loop(ary.concat(i));
   }
};
loop([]);
while(true){
   var hwc=arr.shift();
   if(hwc=="0 0 0")break;
   hwc=hwc.split(" ").map(Number);
   var h=hwc[0];
   var w=hwc[1];
   var c=hwc[2];
   var panel=[]
   for(var i=0;i<h;i++)panel.push(arr.shift().split(" ").map(Number));
   var max=0;
   var yx=[];
   var cnt=1;
   all.forEach(function(v,index){
      yx=[];
      for(var i=0;i<h;i++)yx.push(panel[i].slice());
      v=v.concat(c);
      v.forEach(function(value){
         var first=yx[0][0];
         yx[0][0]=value;
         if(value!=first)bomb(0,0,first,value);
      });
      cnt=1;
      yx[0][0]=0;
      bomb(0,0,c,0);
      max=Math.max(max,cnt);
   });
   console.log(max);
}