using System.Collections.Generic;
using System.Linq;
using static System.Math;
using System;

public class P
{
    public int t { get; set; }
    public int c { get; set; }
    public string via { get; set; }
}

public class P2
{
    public int x { get; set; }
    public int y { get; set; }

}

public class Hello
{
    public static int[,] map; public static int h, w;
    public static int[] dx, dy;

    public static void Main()
    {
        dx = new int[] { 0, 1, 0, -1 };
        dy = new int[] { 1, 0, -1, 0 };
        while (true)
        {
            string[] line = Console.ReadLine().Trim().Split(' ');
            h = int.Parse(line[0]);
            w = int.Parse(line[1]);
            var L = int.Parse(line[2]);
            if (h == 0 && w == 0 && L == 0) break;
            map = new int[h, w];
            for (int i = 0; i < h; i++)
            {
                line = Console.ReadLine().Trim().Split(' ');
                for (int j = 0; j < w; j++) map[i, j] = int.Parse(line[j]);
            }
            var ans = getAns(L);
            Console.WriteLine(ans);
        }
    }
    static int go1(string via)
    {
        var map2 = new int[h, w];
        var st = new Stack<P2>();
        for (int i = 0; i < h; i++)
            for (int j = 0; j < w; j++) map2[i, j] = map[i, j];
        for (int i = 0; i < 5; i++)
        {
            var a = int.Parse(via[i].ToString());
            var a0 = map2[0, 0];
            st.Push(new P2 { x = 0, y = 0 });
            while (st.Count() > 0)
            {
                var ww = st.Pop();
                map2[ww.x, ww.y] = a;
                for (int j = 0; j < 4; j++)
                {
                    var nx = ww.x + dx[j];
                    var ny = ww.y + dy[j];
                    if (nx >= 0 && nx < h && ny >= 0 && ny < w && map2[nx, ny] == a0)
                        st.Push(new P2 { x = nx, y = ny });
                }
            }
        }
        var c00 = map2[0, 0];
        var visited = new bool[h, w];
        st.Push(new P2 { x = 0, y = 0 });
        var count = 0;
        while (st.Count() > 0)
        {
            var ww = st.Pop();
            if (map2[ww.x, ww.y] == c00)
            {
                if (!visited[ww.x, ww.y]) count++;
                visited[ww.x, ww.y] = true;
                for (int j = 0; j < 4; j++)
                {
                    var nx = ww.x + dx[j];
                    var ny = ww.y + dy[j];
                    if (nx >= 0 && nx < h && ny >= 0 && ny < w && !visited[nx, ny])
                        st.Push(new P2 { x = nx, y = ny });
                }
            }
        }
        return count;
    }
    static int getAns(int L)
    {
        var ans = 0;
        var a = map[0, 0];
        var st = new Stack<P>();
        st.Push(new P { t = 0, c = a, via = "" });
        while (st.Count() > 0)
        {
            var w = st.Pop();
            if (w.t == 5)
            {
                var tt = go1(w.via);
                ans = Max(ans, tt);
                continue;
            }
            if (w.t == 4 && w.c != L)
            {
                st.Push(new P { t = 5, c = L, via = w.via + L.ToString() });
                continue;
            }
            if (w.t == 4 && w.c == L) continue;
            for (int i = 1; i <= 6; i++)
                if (i != w.c) st.Push(new P { t = w.t + 1, c = i, via = w.via + i.ToString() });
        }
        return ans;
    }
}



Measured memory_bytes: 20716