#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#define MAXN 105
using namespace std;

char s[105][105];
pair<int,int> Start, End, snake[10];
int n, m;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
bool vis[105][105][10];

struct State{
    pair<int,int> here;
    int cnt;
    int step;
    State(pair<int,int> here, int cnt, int step): here(here), cnt(cnt), step(step) {}
};
bool judge(int x, int y){
    if(x<0||x>=n||y<0||y>=n) return false;
    if(s[x][y]=='#') return false;
    return true;
}
queue<State> q;
int bfs(){
    memset(vis,0,sizeof vis);
    while(!q.empty()) q.pop();
    q.push(State(Start,0,0));
    vis[Start.first][Start.second][0]=true;
    while(!q.empty()){
        State u = q.front(); q.pop();
        pair<int,int> cc;
        int step=u.step;
        int cnt=u.cnt;
//        cout<<u.here.first<<" "<<u.here.second<<endl;
        if(u.here==End&&cnt==m){
//            cout<<"~~"<<cnt<<" "<<step<<endl;
            return step;
        }
        for(int z=0; z<4; z++){
            cc.first = u.here.first + dx[z];
            cc.second = u.here.second+ dy[z];
            if(judge(cc.first,cc.second)){
                if(isdigit(s[cc.first][cc.second])){
                    if(s[cc.first][cc.second]-'0'<=cnt){
                        if(!vis[cc.first][cc.second][cnt]){
                            q.push(State(cc,step+1,cnt));
                            vis[cc.first][cc.second][cnt]=true;
                        }
                    }
                    else if(s[cc.first][cc.second]-'0'==cnt+1){
                        if(!vis[cc.first][cc.second][cnt+1]){
                            q.push(State(cc,step+1,cnt+1));
                            vis[cc.first][cc.second][cnt+1]=true;
                        }
                    }
                }
                else{
                    if(!vis[cc.first][cc.second][cnt]){
                        q.push(State(cc,step+1,cnt));
                        vis[cc.first][cc.second][cnt]=true;
                    }
                }
            }
        }
    }
    return -1;
}

int main()
{
//freopen("/home/acm/code/input.txt","r", stdin);

    while(~scanf("%d %d",&n,&m),n||m){
        int top=0;
        for(int i=0; i<n; i++){
            scanf("%s",s[i]);
            for(int j=0; j<n; j++){
                if(s[i][j]=='K'){
                    Start=make_pair(i,j);
                    s[i][j]='.';
                }
                if(s[i][j]=='T'){
                    End=make_pair(i,j);
                    s[i][j]='.';
                }
                if(s[i][j]=='S'){
                    snake[top++]=make_pair(i,j);
                }
            }
        }
        cout<<"top="<<top<<endl;
        int ans=0x3f3f3f3f;
        for(int i=0; i<(1<<top); i++){
            int cnt=0;
            for(int j=0; j<top; j++){
                int idxi=snake[j].first;
                int idxj=snake[j].second;
                if((i>>j)&1){
                    s[idxi][idxj]='.';
                    cnt++;
                }
                else{
                    s[idxi][idxj]='#';
                }
            }
            int temp=bfs();
            //cout<<temp<<endl;
            if(temp!=-1){
                cout<<cnt<<endl;
                ans=min(ans,cnt+temp);
            }
        }
        if(ans!=0x3f3f3f3f) printf("%d\n",ans);
        else printf("impossible\n");
    }
    return 0;

}