#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
int n, c;
char col[26][26];
char vis[26][26];
void colordfs(int x, int y) {
vis[x][y] = 1;
if (x > 0 && vis[x - 1][y] != 1) {
if (col[x][y] == col[x - 1][y]) colordfs(x - 1, y);
else vis[x - 1][y] = 2;
}
if (x < n - 1 && vis[x + 1][y] != 1) {
if (col[x][y] == col[x + 1][y]) colordfs(x + 1, y);
else vis[x + 1][y] = 2;
}
if (y > 0 && vis[x][y - 1] != 1) {
if (col[x][y] == col[x][y - 1]) colordfs(x, y - 1);
else vis[x][y - 1] = 2;
}
if (y < n - 1 && vis[x][y + 1] != 1) {
if (col[x][y] == col[x][y + 1]) colordfs(x, y + 1);
else vis[x][y + 1] = 2;
}
}
char vis222[26][26];
queue<pair<int, int> > q;
void colordfs2(int x, int y) {
vis222[x][y] = 1;
if (x > 0 && vis222[x - 1][y] != 1) {
if (col[x][y] == col[x - 1][y]) colordfs2(x - 1, y);
else vis222[x - 1][y] = 2, q.push(make_pair(x - 1, y));
}
if (x < n - 1 && vis222[x + 1][y] != 1) {
if (col[x][y] == col[x + 1][y]) colordfs2(x + 1, y);
else vis222[x + 1][y] = 2, q.push(make_pair(x + 1, y));
}
if (y > 0 && vis222[x][y - 1] != 1) {
if (col[x][y] == col[x][y - 1]) colordfs2(x, y - 1);
else vis222[x][y - 1] = 2, q.push(make_pair(x, y - 1));
}
if (y < n - 1 && vis222[x][y + 1] != 1) {
if (col[x][y] == col[x][y + 1]) colordfs2(x, y + 1);
else vis222[x][y + 1] = 2, q.push(make_pair(x, y + 1));
}
}
bool colorit(int tocol) {
bool flg = 0;
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (vis[i][j] == 2 && col[i][j] == tocol) colordfs(i, j), flg = 1;
return flg;
}
int calcwill() {
bool tmp[8] = {};
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (vis[i][j] != 1) tmp[(int)col[i][j]] = 1;
int res = 0; for (int i = 0; i < 8; i++) res += tmp[i];
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) {
vis222[i][j] = vis[i][j]; if (vis222[i][j] == 2) q.push(make_pair(i, j));
}
int dep = 0, nowsize = q.size();
while (nowsize) {
for (int i = 0; i < nowsize; i++) {
if (vis222[q.front().first][q.front().second] == 2) colordfs2(q.front().first, q.front().second);
q.pop();
}
nowsize = q.size();
dep++;
}
return max(res, dep);
}
int IDAStar(int d, int maxd, int firstst = -1) {
int cw = calcwill();
if (d + cw > maxd) return -1; if (cw == 0) return firstst;
char vis2[26][26];
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) vis2[i][j] = vis[i][j];
for (int i = 0; i < c; i++) {
if (colorit(i)) {
int cc = IDAStar(d + 1, maxd, firstst == -1 ? i : firstst);
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) vis[i][j] = vis2[i][j];
if (cc != -1) return cc;
}
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) vis[i][j] = vis2[i][j];
}
return -1;
}
void mainpath() {
vis[0][0] = 2;
colorit(col[0][0]);
int res = 0, cc;
int path[1000];
for (; ; res++) {
if ((cc = IDAStar(0, res)) != -1) {
path[0] = cc;
break;
}
}
printf("%d\n", res);
}
int main() {
c = 6;
while (~scanf("%d", &n)) {
if (n == 0) break;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) scanf("%d", &col[i][j]);
}
mainpath();
}
return 0;
}
在题解的基础上添加了最少层数优化