Problem:
Problem D: Off Balance

You are working for an administration office of the International
Center for Picassonian Cubism (ICPC), which plans to build a new art
gallery for young artists. The center is organizing an architectural
design competition to find the best design for the new building.

Submitted designs will look like a screenshot of a well known
game as shown below.

This is because the center specifies that
the building should be constructed by stacking regular units
called pieces, each of which consists of four cubic blocks.
In addition, the center requires the competitors to submit designs
that satisfy the following restrictions.

All the pieces are aligned. When two pieces touch each other, the faces of
the touching blocks must be placed exactly at the same position.

All the pieces are stable. Since pieces are merely stacked,
their centers of masses must be carefully positioned so as not to
collapse.

The pieces are stacked in a tree-like form
in order to symbolize boundless potentiality of young artists.
In other words, one and only one
piece touches the ground, and each of other pieces touches one and
only one piece on its bottom faces.

The building has a flat shape.
This is because the construction site
is a narrow area located between a
straight moat and an expressway where
no more than one block can be placed
between the moat and the
expressway as illustrated below.

It will take many days to fully check stability of designs as it requires
complicated structural calculation.
Therefore, you are
asked to quickly check obviously unstable designs by focusing on
centers of masses. The rules of your quick check are as follows.

Assume the center of mass of a block is located at the center of the
block, and all blocks have the same weight.
We denote a location of a block by xy-coordinates of its
left-bottom corner. The unit length is the edge length of a block.

Among the blocks of the piece that touch another piece or the ground on their
bottom faces, let xL be the leftmost x-coordinate of
the leftmost block, and let xR be the rightmost
x-coordinate of the rightmost block.
Let the x-coordinate of its accumulated center of mass of
the piece be M, where the accumulated center of mass of a piece P
is the center of the mass of the pieces that are directly and indirectly
supported by P, in addition to P itself. Then the piece is
stable, if and only if
xL < M < xR. Otherwise, it is
unstable. A design of a building is unstable if any of its pieces is unstable.

Note that the above rules could judge some designs to be unstable even
if it would not collapse in reality. For example, the left one of the
following designs shall be judged to be unstable.

Also note that the rules judge boundary cases to be unstable. For example, the top piece in the above right design has its center of mass exactly above the right end of the bottom piece. This shall be judged to be unstable.

Write a program that judges stability of each design based on the above quick check rules.

Input

The input is a sequence of datasets. The end of the input is
indicated by a line containing two zeros separated by a space. Each
dataset, which represents a front view of a building, is formatted as follows.

w h

p0(h-1)p1(h-1)...p(w-1)(h-1)

...

p01p11...p(w-1)1

p00p10...p(w-1)0

The integers w and h separated by a space are the numbers of columns
and rows of the layout, respectively. You may assume 1 ≤ w ≤
10 and 1 ≤ h ≤ 60. The next h lines specify the placement of
the pieces. The character pxy indicates the status of a block at
(x,y), either `.', meaning empty, or one digit character between
`1' and `9', inclusive, meaning a block of a piece.
(As mentioned earlier, we denote a location of a block by xy-coordinates of its
left-bottom corner.)

When two blocks with the same number touch each other by any of their
top, bottom, left or right face, those blocks are of the same piece.
(Note that there might be two different pieces that are denoted by
the same number.)
The bottom of a block at (x,0) touches the ground.

You may assume that the pieces in each dataset are stacked in a tree-like form.

Output

For each dataset, output a line containing a word STABLE
when the design is stable with respect to the above quick check rules.
Otherwise, output UNSTABLE. The output
should be written with uppercase letters, and should not contain any other extra characters.

Sample Input

4 5
..33
..33
2222
..1.
.111
5 7
....1
.1..1
.1..1
11..1
.2222
..111
...1.
3 6
.3.
233
23.
22.
.11
.11
4 3
2222
..11
..11
4 5
.3..
33..
322.
2211
.11.
3 3
222
2.1
111
3 4
11.
11.
.2.
222
3 4
.11
.11
.2.
222
2 7
11
.1
21
22
23
.3
33
2 3
1.
11
.1
0 0

Output for the Sample Input

STABLE
STABLE
STABLE
UNSTABLE
STABLE
STABLE
UNSTABLE
UNSTABLE
UNSTABLE
UNSTABLE

Solution in C (judge verdict: Time Limit Exceeded)

#include <stdio.h>
#include <stdlib.h>

#define Q_MAX 4
#define PIECE_SIZE 4
#define W_MAX 10
#define H_MAX 60

/* Queue */

int Q[Q_MAX], sp, rp;

void Qinit(){ sp=0, rp=0; }
void Qstore(int N){ Q[sp++%Q_MAX]=N; }
void Qstore2(int N, int M){ Q[sp++%Q_MAX]=N; Q[sp++%Q_MAX]=M; }
int Qretrive(){ return Q[rp++%Q_MAX]; }
int Qempty(){ return sp==rp; }

/* piece */

struct piece{
	int id;
	double gc;	/* gravity center */
	struct piece* firstChild;
	struct piece* nextBrother;
};

struct piece *piece[W_MAX*H_MAX/PIECE_SIZE]; int pp;	/* pieces dictionary */

void new_piece(){
	int i; struct piece *res=malloc(sizeof(struct piece));
	piece[++pp]=res;
	res->id=pp, res->gc=-1, res->firstChild=NULL, res->nextBrother=NULL;
}

void initPieces(){
	int i;
	for(i=0; i<=pp; i++) if(piece[i]!=NULL) free(piece[i]), piece[i]=NULL;
	pp=-1;
}

void affiliate(int childId, int parentId){	/* let A be B's child */
	struct piece *t; t=piece[parentId];
	
	if(t->firstChild==NULL){
	
	t->firstChild=piece[childId];
	
	}else{
	
	t=t->firstChild;
	while(t->nextBrother!=NULL) t=t->nextBrother;
	t->nextBrother=piece[childId];
	
	}
}

int has(int childId, int parentId){	/* if B has A as a child */
	struct piece *t; t=piece[parentId]->firstChild;
	while(t!=NULL){
		if(t==piece[childId]) return 1;
		t=t->nextBrother;
	}
	return 0;
}

/* main */

int map[W_MAX][H_MAX];
int input[W_MAX][H_MAX], W, H;

void tokenizePiece(int x, int y){	/* analyze a piece */
	double sum=0; int id; new_piece();
	id=input[x][y], Qinit(), input[x][y]=-1; Qstore2(x,y);
	while(!Qempty()){
		x=Qretrive(); y=Qretrive();
		map[x][y]=pp, sum+=x+0.5;
		
		if(x+1<W && input[x+1][y]==id) input[x+1][y]=-1, Qstore2(x+1,y);
		if(y+1<H && input[x][y+1]==id) input[x][y+1]=-1, Qstore2(x,y+1);
		if(x>0 && input[x-1][y]==id) input[x-1][y]=-1, Qstore2(x-1,y);
		if(y>0 && input[x][y-1]==id) input[x][y-1]=-1, Qstore2(x,y-1);
	}
	piece[pp]->gc=(double)sum/PIECE_SIZE;
}

int L(int id){	/* the XL value of the piece */
	int x,y,l=W;
	for(y=0; y<H; y++)
		for(x=0; x<W; x++)
			if(map[x][y]==id && (y+1==H || (map[x][y+1]!=-1 && map[x][y+1]!=id) ) && x<l) l=x;
	return l;
}

int R(int id){	/* the XR value of the piece */
	int x,y,r=0;
	for(y=0; y<H; y++)
		for(x=0; x<W; x++)
			if(map[x][y]==id && (y+1==H || (map[x][y+1]!=-1 && map[x][y+1]!=id) ) && x>r) r=x;
	return r+1;
}

double GC(int id){	/* GravityCenter of the subtree */
	double sum=0; int i=0; Qinit();
	
	sum+=piece[id]->gc, i++;
	if(piece[id]->firstChild!=NULL) Qstore(piece[id]->firstChild->id);
	
	while(!Qempty()){
		id=Qretrive();
		sum+=piece[id]->gc, i++;
		if(piece[id]->firstChild!=NULL) Qstore(piece[id]->firstChild->id);
		if(piece[id]->nextBrother!=NULL) Qstore(piece[id]->nextBrother->id);
	}
	return sum/i;
}

int main(void){
	int x, y, i; char c; struct piece *t;
	
	while(scanf("%d %d\n",&W,&H), W!=0){
	
		/* initialize */
		initPieces();
		for(x=0; x<=W; x++) for(y=0; y<=H; y++) map[x][y]=-1;
	
		/* get input */
		for(y=0; y<H; y++){
			for(x=0; x<W; x++){
				getitem:
			
				scanf("%c",&c);
				
				if(c=='\n') goto getitem;
				else if(c=='.') input[x][y]=-1;
				else input[x][y]=c-'0';
			}
		}
		
		/* tokenize */
		for(y=0; y<H; y++)
			for(x=0; x<W; x++)
				if(input[x][y]!=-1) tokenizePiece(x,y);

		/* parse relations */
		for(y=0; y<H-1; y++)
			for(x=0; x<W; x++)
				if(map[x][y]!=-1 && map[x][y+1]!=-1 && map[x][y]!=map[x][y+1])
					if(!has(map[x][y],map[x][y+1]))
						affiliate(map[x][y],map[x][y+1]);
		
		/* judge */
		for(i=0; i<=pp; i++)
			if(L(i)>=GC(i) || GC(i)>=R(i)){ printf("UNSTABLE\n"); break; }
		if(i>pp) printf("STABLE\n");
	}
}