Off Balance

No attempts yetTime limit1sMemory limit128 MB

Problem

You work at the administration office of the International Center for Picassonian Cubism (ICPC), which is planning a new art gallery for young artists. To pick the best design, the center is holding an architectural design competition.

Every submitted design looks like a screenshot of a well-known block-stacking game, as shown below.

This is because the center requires every building to be built by stacking regular units called pieces, where each piece is made of four cubic blocks. Designs must also satisfy the following rules.

  • All pieces are aligned. When two pieces touch, the faces of the touching blocks must line up exactly.
  • All pieces are stable. Because the pieces are merely stacked, their centers of mass must be positioned so the structure does not collapse.
  • The pieces form a tree. Exactly one piece touches the ground, and every other piece touches exactly one piece with its bottom faces. This tree shape symbolizes the boundless potential of young artists.
  • The building is flat. The site is a narrow strip between a straight moat and an expressway, where at most one block fits across, so the whole building is effectively two-dimensional (a front view).

Fully checking stability requires complicated structural analysis and takes many days, so you are asked to quickly reject the obviously unstable designs by looking only at centers of mass. The quick check works as follows.

Assume every block has the same weight and that its center of mass sits at the block's center. A block's position is given by the $xy$-coordinates of its lower-left corner, and the unit length is a block's edge length, so a block placed at $(x, y)$ occupies the unit square from $(x, y)$ to $(x+1, y+1)$.

For a piece, look only at the blocks that touch another piece or the ground with their bottom faces. Let $x_L$ be the left $x$-coordinate of the leftmost such block, and let $x_R$ be the right $x$-coordinate of the rightmost such block. Let $M$ be the $x$-coordinate of the piece's accumulated center of mass, where the accumulated center of mass of a piece $P$ is the center of mass of $P$ together with every piece that $P$ supports directly or indirectly. The piece is stable if and only if $x_L < M < x_R$; otherwise it is unstable. A whole design is unstable if any of its pieces is unstable.

These rules may call a design unstable even though it would not actually collapse; the left design below is judged unstable for that reason.

The rules also treat boundary cases as unstable. For example, in the right design above the top piece has its center of mass exactly above the right end of the piece beneath it, so it is judged unstable.

Write a program that judges the stability of each design with these quick-check rules.

Input

The input is a sequence of datasets and ends with a line containing two zeros separated by a space. Each dataset describes the front view of one building and is formatted as follows.

w h
(row for y = h-1)   top row, w characters
...
(row for y = 1)
(row for y = 0)     bottom row, resting on the ground

The integers $w$ and $h$ (separated by a space) are the numbers of columns and rows of the layout, with $1 \le w \le 10$ and $1 \le h \le 60$. The next $h$ lines give the placement of the pieces, from the top row ($y = h-1$) down to the bottom row ($y = 0$). Each character $p_{xy}$ is the status of the cell at $(x, y)$: either . (empty) or a single digit 19 (a block belonging to some piece).

Two blocks with the same digit that touch through their top, bottom, left, or right face belong to the same piece. (Two different pieces may use the same digit as long as they do not touch.) A block at $(x, 0)$ rests its bottom face on the ground.

You may assume the pieces in each dataset form a valid tree as described above.

Output

For each dataset, print STABLE if the design is stable under the quick-check rules, or UNSTABLE otherwise. Use uppercase letters only, with no other characters.