#include <bits/stdc++.h>
using namespace std;
class SudokuSolver
{
const static int SIZE = 3;
const static int LENGTH = SIZE * SIZE;
int board[LENGTH][LENGTH] = {};
vector<pair<int, int>> emptyList;
bool bFinished = false;
int CubeStartrPos[LENGTH] = { 0, SIZE, SIZE * 2,
LENGTH * SIZE , LENGTH * SIZE + SIZE, LENGTH * SIZE + SIZE * 2,
LENGTH * 2 * SIZE * SIZE, LENGTH * 2 * SIZE + SIZE, LENGTH * 2 * SIZE + SIZE * 2 };
public:
void Input()
{
for (int i = 0; i < LENGTH; i++)
{
for (int j = 0; j < LENGTH; j++)
{
cin >> board[i][j];
if (board[i][j] == 0)
{
emptyList.push_back({ i,j });
}
}
}
}
void Solve()
{
backtrk(0);
}
void Print()
{
for (int i = 0; i < LENGTH; i++)
{
for (int j = 0; j < LENGTH; j++)
{
cout << board[i][j] << " ";
}
cout << "\n";
}
}
private:
void backtrk(int curr)
{
if (emptyList.size() <= curr)
{
Print();
bFinished = true;
}
if (bFinished)
return;
pair<int, int>& target = emptyList[curr];
for (int i = 1; i <= LENGTH; i++)
{
if (CanFill(target, i))
{
board[target.first][target.second] = i;
backtrk(curr + 1);
}
}
board[target.first][target.second] = 0;
}
bool CanFill(pair<int,int>& pos, int K)
{
if (!CanFillRowColumn(pos, K))
{
return false;
}
if (!CanFillCube(pos, K))
{
return false;
}
return true;
}
bool CanFillRowColumn(pair<int, int>& pos, int K)
{
int y = pos.first;
int x = pos.second;
for (int i = 0; i < LENGTH; i++)
{
if (board[y][i] == K)
{
return false;
}
if (board[i][x] == K)
{
return false;
}
}
return true;
}
bool CanFillCube(pair<int, int>& pos, int K)
{
int cube_start_pos = GetCubeStartrPos(pos);
int start_y, start_x, y, x;
start_x = cube_start_pos % LENGTH;
start_y = cube_start_pos / LENGTH;
for (int y = start_y; y < start_y + SIZE; y++)
{
for (int x = start_x; x < start_x + SIZE; x++)
{
if (board[y][x] == K)
{
return false;
}
}
}
return true;
}
int GetCubeStartrPos(pair<int,int>& pos)
{
int pos_y, pos_x, start_y, start_x;
pos_y = pos.first;
pos_x = pos.second;
for (auto start_pos : CubeStartrPos)
{
start_x = start_pos % LENGTH;
start_y = start_pos / LENGTH;
if (pos_y < start_y + SIZE && pos_x < start_x + SIZE)
{
return start_pos;
}
}
return -1; // error;
}
};
int main() {
SudokuSolver ss;
ss.Input();
ss.Solve();
return 0;
}