// Arup Guha
// 4/29/2018
// Solution to 2016 AP Computer Science A FR Question 3

import java.util.*;

public class Crossword {

	private Square[][] puzzle;

	public Crossword(boolean[][] blackSquares) {

		// Allocate space.
		puzzle = new Square[blackSquares.length][blackSquares[0].length];

		// The number we are on.
		int cur = 1;

		// Loop through each square.
		for (int i=0; i<blackSquares.length; i++) {
			for (int j=0; j<blackSquares[0].length; j++) {

				// Now see if a number is necessary.
				if (toBeLabeled(i, j, blackSquares))
					puzzle[i][j] = new Square(blackSquares[i][j], cur++);

				// I'll just store 0 for a square w/o a number.
				else
					puzzle[i][j] = new Square(blackSquares[i][j], 0);
			}
		}
	}

	private boolean toBeLabeled(int r, int c, boolean[][] blackSquares) {

		// Definitely no!
		if (blackSquares[r][c]) return false;

		// Can't have a white square on either left or up.
		if (r == 0 || c == 0) return true;

		// One of these must be black, otherwise, we are out of luck.
		return blackSquares[r-1][c] || blackSquares[r][c-1];
	}

	// For testing...
	public String toString() {
		String res = "";
		for (int i=0; i<puzzle.length; i++)
			for (int j=0; j<puzzle[0].length; j++)
				if (puzzle[i][j].getNumber() > 0)
					res = res + "At ("+i+", "+j+"), val = "+puzzle[i][j].getNumber()+"\n";

		return res;
	}

	public static void main(String[] args) {

		// This is their sample in the picture.
		boolean[][] sq = new boolean[7][9];
		int[] r = {0,0,0,0,1,2,2,2,3,3,4,4,4,5,6,6,6,6};
		int[] c = {0,3,4,5,4,6,7,8,2,6,0,1,2,4,3,4,5,8};
		for (int i=0; i<r.length; i++)
			sq[r[i]][c[i]] = true;

		// Ta da!
		Crossword times = new Crossword(sq);
		System.out.println(times);
	}
}