Under construction

Still being built. Things may change

Light Up

Time limit1sMemory limit1024 MB

Summary
Given an n by n Light Up grid with bulbs placed, the task checks whether every open cell is lit, no bulbs see each other, and every numbered wall has the right bulb count.
Level

Easy3 of 10

Topics
Matrix, Simulation, Implementation
Solved
No attempts yet

Description

Light Up is a pencil puzzle. Your job here is not to play Light Up, but to judge whether a player's solution is correct.

The game is played on a square grid. Some cells are blocked, and some blocked cells have a number from 00 to 44. The player places light bulbs in open cells. Each light bulb lights all open cells above, below, left, and right (but not diagonally) until the light reaches the edge of the grid or a blocked cell. The player must place light bulbs so that:

  • Every open cell is lit.
  • No two light bulbs shine on each other.
  • Any blocked cell with a number must have exactly that many light bulbs immediately adjacent above, below, left, or right. Diagonals do not count.

The following is an example grid with its solution:

Given a grid with light bulbs placed, determine whether it is a solution. If a grid has no open cells and violates no other constraints, it is trivially solved.

Input

The first line of input contains a single integer nn (1≤n≤301≤n≤30), the number of rows and columns in the grid.

Each of the next nn lines contains exactly nn characters from the set {., X, ?, 0, 1, 2, 3, 4}. . is an open cell, X is a blocked cell, ? is a light bulb, and the digits 0 through 4 are blocked cells with a constraint on the number of adjacent light bulbs.

Output

Output a single integer: 11 if the input is a valid solution, and 00 otherwise.

Examples2

  1. Example 1

    Input
    7
    .?.0..?
    ..X.1?.
    .X.?.2.
    .....?.
    ?3?..2.
    .?3.X?.
    ..?X?..
    
    Expected output
    1
    
  2. Example 2

    Input
    7
    .?.0..?
    ..X.1?.
    .X...2.
    .....?.
    ?3?..2.
    .?3.X?.
    ..?X?..
    
    Expected output
    0