const int N = 35;
int g[N][N];
int n;
int move_x[6] = { 0,-1,0,1,0 };
int move_y[6] = { 0,0,1,0,-1 };
int save[35][35];
bool gi[N][N];
queue <pair<int, int>> q;
int bfs(int x, int y)
{
int judge = 0;
int ret = 0;
memset(gi, -1, sizeof(gi));
memset(save, 0, sizeof(save));
gi[x][y] = 1;
save[x][y] = 2;
q.push({ x,y });
while (q.size() && judge == 0)
{
judge = 0;
pair<int, int> temp = q.front();
q.pop();
for (int i = 1; i <= 4; i++)
{
int a = temp.first + move_x[i]; int b = temp.second + move_y[i];
if (a < 1 || a > n || b < 1 || b > n) continue;
if (g[a][b] == 1) continue;
if (gi[a][b] == 1) continue;
if ((g[a][b] == 0) && (a == 1 || a == n || b == 1 || b == n))
{
judge = 1;
break;
}
q.push({ a,b });
gi[a][b] = 1;
save[a][b] = 2;
}
}
if (judge == 0)
return 1;
if (judge == 1)
return 0;
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
cin >> g[i][j];
for(int i = 2; i <= n - 1; i++)
for (int j = 2; j <= n - 1; j++)
{
memset(save, 0, sizeof(save));
while (!q.empty())
{
q.pop();
}
if (g[i][j] == 0)
{
if (bfs(i, j))
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (save[i][j] == 2)
cout << 2 << " ";
else
cout << g[i][j] << " ";
}
cout << endl;
}
return 0;
}
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cout << g[i][j] << " ";
}
cout << endl;
}
return 0;
}