#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int H,W;
int M = 10000;
int s,S,k,K;
vector<int> dist;




void bfs_s2(vector<char> g,int dis,bool sfound){
  queue<int> que;
  for(int i=0;i<H*W;i++){
    dist[i] = 10000;
  }
  que.push(S);
  dist[S] = dis;
  while(!que.empty()){
    int p = que.front();
    //cout << "p:" << p << endl;
    que.pop();
    //cout << "next_p:" << que.front() << endl;
    if(p==s-W){
      int d = dist[p]+1;
      if(p%W!=0 and g[s-1]=='a')d++;
      if(p%W!=W-1 and g[s+1]=='a')d++;
      if(p/W<H and g[s+W]=='a')d++;
      if(d<M){
        M=d;
      }
      continue;
    }
    vector<int> next;
    if(p%W!=0)next.push_back(p-1);
    if(p%W!=W-1)next.push_back(p+1);
    if(p/W<s/W-1)next.push_back(p+W);
    for(int i=0;i<next.size();i++){
      int d = dist[p]+1;
      switch(g[next[i]]){
        case('.'):
        if(dist[next[i]] > d){
          dist[next[i]] = d;
          que.push(next[i]);
        }
        break;
        case('a'):
        d++;
        case('K'):
        d++;
        //cout << sfound <<endl;
        if(sfound==1){
          if(d<M)M=d;
          return;
        }
        for(int j=0;j<H*W;j++){
          if(g[j]=='a' and dist[j]>d){
            dist[j] = d;
            que.push(j);
          }
          if(g[j]=='K' and dist[j]>d and g[next[i]]!='K'){
            dist[j] = d-1;
            que.push(j);
          }
          if(g[j]=='K' and dist[j]>d and g[next[i]]=='K'){
            dist[j] = d;
            que.push(j);
          }
        }
        break;
      }
    }
  }
}

void dfs_s2(int p,vector<char> g,int dis,bool out){
  if(p/W >= s/W or M<=dis)return;
  if(g[p]=='a' or g[p]=='S'){
    g[p] = 'b';
    if(out==1)M = dis;
    if(out==0){
      if(p%W!=0)dfs_s2(p-1,g,dis,0);
      if(p%W!=W-1)dfs_s2(p+1,g,dis,0);
      if(p/W<s/W-1)dfs_s2(p+W,g,dis,0);
    }
  }
  if(g[p]=='.'){
    g[p] = 'b';
    if(p%W!=0)dfs_s2(p-1,g,dis+1,1);
    if(p%W!=W-1)dfs_s2(p+1,g,dis+1,1);
    if(p/W<s/W-1)dfs_s2(p+W,g,dis+1,1);
  }
}
/*
void bfs_k1(vector<char> g){
  int d=0;
  queue<int> Q;
  Q.push(K);
  Q.push(-1);
  while(!Q.empty()){
    int p=Q.front();
    Q.pop();
    if(p==-1)d++;
    vector<int> next;
    if(p%W!=0)next.push_back(p-1);
    if(p%W!=W-1)next.push_back(p+1);
    if(p/W<k/W-1)next.push_back(p+W);
    for(int i=0;i<next.size();i++){
      if(g[next[i]]=='.')Q.push(next[i]);
      if(next[i]==k-W){
        limdis=d+8;
        break;
      }
    }
  }
}
*/

void dfs_k1(int p,vector<char> g,int dis,bool sfound,bool Sfound){
  //if(dis>limdis)return;
  if(p!=K)g[p]='a';
  if(g[p+W]=='k'){
    dis++;
    for(int i=0;i<H*W;i++){
      if(g[i]=='l')g[i]='.';
    }
    if(Sfound==0){
      bfs_s2(g,dis,sfound);
      //cout << sfound <<Sfound <<endl;
    }
    else if(sfound==1)dfs_s2(S,g,dis+4,0);
    return;
  }
  dis++;
  if(p/W<k/W-1){
    switch(g[p+W]){
      case('S'):
      dfs_k1(p+W,g,dis,sfound,1);
      break;
      case('s'):
      sfound = 1;
      case('.'):
      dfs_k1(p+W,g,dis,sfound,Sfound);
      break;
    }
  }
  vector<int> next;
  if(p%W!=0)next.push_back(p-1);
  if(p%W!=W-1)next.push_back(p+1);
  int P=p+W;
  while(P<H*W){
    if(g[P]=='.')g[P]='l';
    else break;
    P += W;
  }
  for(int i=0;i<next.size();i++){
    switch(g[next[i]]){
      case('S'):
      dfs_k1(next[i],g,dis,sfound,1);
      break;
      case('s'):
      sfound = 1;
      case('.'):
      dfs_k1(next[i],g,dis,sfound,Sfound);
      break;
    }
  }
}




int main(){
  cin >> H >> W;
  vector<char> g;
  g.resize(H*W);
  dist.resize(H*W);
  for(int i=0;i<H*W;i++){
    cin >> g[i];
    if(g[i]=='k')k=i;
    if(g[i]=='K')K=i;
    if(g[i]=='s')s=i;
    if(g[i]=='S')S=i;
  }
  dfs_k1(K,g,0,0,0);

  g[k]='s';
  g[s]='k';
  g[S]='K';
  g[K]='S';
  int koukan;
  koukan = s;
  s=k;
  k=koukan;
  koukan=S;
  S=K;
  K=koukan;

  //cout << "---------------------------"<<endl;
  dfs_k1(K,g,0,0,0);


  if(M==10000)cout << "CONTRACT"<<endl;
  else cout << M <<endl;
}



Measured memory_bytes: 3172