using System;

/*!!!GORIOSHI PROGRAM!!!*/
namespace V1174_1{
    public class Program{
        public static void Main(string[] args){
            var sr = new StreamReader();
            //---------------------------------
            var dx = new[]{0, 1, 0, -1};
            var dy = new[]{1, 0, -1, 0};
            while(true){
                var H = sr.Next<int>();
                var W = sr.Next<int>();
                var C = sr.Next<int>();
                var P = sr.Next<int>(H, W);
                if(H == 0 && W == 0 && C == 0) break;

                var max = 0;
                Func<int, int, bool> isInside = (x, y) => 0 <= x && x < W && 0 <= y && y < H;
                Action<int[][], int, int> dfs1 = null;
                dfs1 = (p, cnt, nc) =>{
                    var cc = p[0][0];
                    if(nc == cc) return;

                    if(cnt == 5){
                        var res = 0;
                        if(cc == C){
                            Action<int, int> dfs3 = null;
                            dfs3 = (x, y) =>{
                                res++;
                                p[y][x] = -1;
                                for(var i = 0; i < 4; i++){
                                    var nx = x + dx[i];
                                    var ny = y + dy[i];
                                    if(isInside(nx, ny) && p[ny][nx] == cc){
                                        dfs3(nx, ny);
                                    }
                                }
                            };
                            dfs3(0, 0);
                        }
                        max = Math.Max(max, res);
                        return;
                    }
                    
                    Action<int, int> dfs2 = null;
                    dfs2 = (x, y) =>{
                        p[y][x] = nc;
                        for(var i = 0; i < 4; i++){
                            var nx = x + dx[i];
                            var ny = y + dy[i];
                            if(isInside(nx, ny) && p[ny][nx] == cc){
                                dfs2(nx, ny);
                            }
                        }
                    };
                    dfs2(0, 0);

                    for(var i = 1; i <= 6; i++){
                        dfs1(p.DeepCopy(), cnt + 1, i);
                    }
                };

                for(var i = 1; i <= 6; i++){
                    dfs1(P.DeepCopy(), 0, i);
                }

                Console.WriteLine(max);
            }
            //---------------------------------
        }
    }

    public static class ExMethod{
        public static T[] DeepCopy<T>(this T[] src){
            var res = new T[src.Length];
            for(var i = 0; i < src.Length; i++){
                res[i] = src[i];
            }
            return res;
        }

        public static T[][] DeepCopy<T>(this T[][] src){
            var res = new T[src.Length][];
            for(var i = 0; i < src.Length; i++){
                res[i] = src[i].DeepCopy();
            }
            return res;
        }
    }

    public class StreamReader{
        private readonly char[] _c = {' '};
        private int _index = -1;
        private string[] _input = new string[0];
        private readonly System.IO.StreamReader _sr = new System.IO.StreamReader(Console.OpenStandardInput());

        public T Next<T>(){
            if(_index == _input.Length - 1){
                _index = -1;
                while(true){
                    string rl = _sr.ReadLine();
                    if(rl == null){
                        if(typeof(T).IsClass) return default(T);
                        return (T)typeof(T).GetField("MinValue").GetValue(null);
                    }
                    if(rl != ""){
                        _input = rl.Split(_c, StringSplitOptions.RemoveEmptyEntries);
                        break;
                    }
                }
            }
            return (T)Convert.ChangeType(_input[++_index], typeof(T), System.Globalization.CultureInfo.InvariantCulture);
        }

        public T[] Next<T>(int x){
            var ret = new T[x];
            for(var i = 0; i < x; ++i) ret[i] = Next<T>();
            return ret;
        }

        public T[][] Next<T>(int y, int x){
            var ret = new T[y][];
            for(var i = 0; i < y; ++i) ret[i] = Next<T>(x);
            return ret;
        }
    }
}

Measured memory_bytes: 10380