/* Problem 6--Sudoku
   A great deal of good work has come out of analysis of Sudoku and
   similar puzzles.  There are some excellent algorithms for solving
   Sudoku problems, and they work very well for very large Sudoku grids.
   However, they are simply not necessary here.  9x9 Sudoku, to be quite
   blunt, just isn't that hard a game (which is probably why it is so
   popular), and brute force trial and error will solve even the most
   challenging Sudoku problem quite quickly.  Thus, this solution boasts
   no sophistication whatsoever. */

import java.io.*;
import java.util.*;

public class prob6 {

 public static Scanner in=null;
 public static PrintWriter out=null;
 public final static int sz=9; // We are solving 9x9 Sudoku
 public static char[][] puzzle = new char[sz][];
 public static int cs=0;

 public static void main (String[] args) throws Exception {

  in = new Scanner (new File ("prob6.in"));
  out = new PrintWriter ("prob6.out");
  while (in.hasNextLine()) { // Read in puzzxle grid
   for (int i=0; i < sz; i++) puzzle[i] = in.nextLine().toCharArray();
   in.nextLine();
   out.println ("Case "+(++cs));
   out.println ();
   Process (); // Recursively solve Sudoku
  }
  in.close();
  out.close();
 }

 /* Process recursively solves Sudoku by trying all legal values in the
    first empty square it finds and attempting to solve that puzzle. */
 public static void Process () {

  for (int i=0; i < sz; i++)
   for (int j=0; j < sz; j++)
    if (puzzle[i][j]==' ') { // Found a space
     tryloop:
     for (int k=1; k <= sz; k++) { // Try each digit
      for (int r=0; r < sz; r++) if (puzzle[r][j]==(char)(k+'0'))
        continue tryloop; // If found in the row, try again!
      for (int c=0; c < sz; c++) if (puzzle[i][c]==(char)(k+'0'))
        continue tryloop; // If found in the column, try again!
      int bd = (int)(Math.sqrt(sz)+0.5); // If found in the subgrid,
      for (int r=i/bd*bd; r < bd+i/bd*bd; r++) // try again!
       for (int c=j/bd*bd; c < bd+j/bd*bd; c++)
        if (puzzle[r][c]==(char)(k+'0')) continue tryloop;
      puzzle[i][j] = (char)(k+'0'); // This one works, so try it!
      Process(); // Solve the more completed puzzle.
     }
     puzzle[i][j] = ' '; // Put the space back after testing.
     return; // We only want to try one square, so quit here.
    }
  for (int i=0; i < sz; i++) { // To make it here, there are no spaces.
   for (int j=0; j < sz; j++) out.print (puzzle[i][j]); // Print out.
   out.println();
  }
  out.println();
 }
}
