#include <iostream>
#include <algorithm>
#include <cstring>
#define MAXN 200
using namespace std;
int map[MAXN][MAXN];
char arr[MAXN][MAXN];
bool visited[MAXN][MAXN];
int N,M;
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};
int cnt = 0 ;
char word[] = {'W','A','K','J','S','D'};
int time;
bool is_range(int x, int y){
return (x>=0 && y >=0 && x < N && y <4*M);
}
void check_background(int x, int y){
visited[x][y] = true;
for (int k = 0 ;k<4; k++){
int nx = x + dx[k];
int ny = y + dy[k];
if (!is_range(nx,ny)) continue;
if (!map[nx][ny] && !visited[nx][ny]){
check_background(nx,ny);
}
}
}
void dfs(int x, int y, int mode){
visited[x][y] = true;
if (mode){
for (int k = 0;k<4;k++){
int nx = x + dx[k];
int ny = y + dy[k];
if (!is_range(nx,ny)) continue;
if (visited[nx][ny] || map[nx][ny]) continue;
dfs(nx,ny,mode);
}
}
else{
for (int k = 0 ;k< 4; k++){
int nx = x + dx[k];
int ny = y + dy[k];
if (!is_range(nx,ny)) continue;
if (visited[nx][ny]) continue;
if (map[nx][ny]){
dfs(nx,ny,mode);
}
else{
cnt++;
dfs(nx,ny,1);
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
while (1){
time++;
cin >> N >> M ;
if (N == 0 && M == 0) break;
memset(visited,false,sizeof(visited));
for (int i = 0 ; i <N;i++){
for (int j = 0; j<M;j++){
char tmp;
cin >> tmp;
int num;
if (tmp>='a'&& tmp<='f') num = tmp -'a'+10;
else num = tmp - '0';
for (int k = 3; k>=0; k--){
map[i][4*j+k] = num%2;
num /= 2;
}
}
}
for (int i = 0 ; i < N; i++){
if (!visited[i][0] && !map[i][0]){
check_background(i,0);
}
if (!visited[i][4*M-1] && !map[i][4*M-1]) check_background(i,4*M-1);
}
for (int i = 0 ; i < 4*M; i++){
if (!visited[0][i] && !map[0][i]){
check_background(0,i);
}
if (!visited[N-1][i] && !map[N-1][i]){
check_background(N-1,i);
}
}
string ans = "";
for (int i = 0 ; i <N ;i++){
for (int j = 0;j<4*M; j++){
if (!visited[i][j] && map[i][j]){
cnt = 0;
dfs(i,j,0);
ans += word[cnt];
}
}
}
sort(ans.begin(),ans.end());
cout << "Case "<< time <<": " << ans <<"\n";
}
return 0;
}