//* Problem  : 2947 - SLIKAR
//* Contest  : acm.tju.edu.cn
//* Origin   : 
//* Type     : Solution
//* Date     : 2008.04.29
//* Author   : alt
//* Language : C++
//* Compiler : Microsoft Visual C++ 6.0
//* Algoritm : 

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

#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <iostream>
using namespace std;
#define int64 long long

#define PI 2*acos(0.0)

struct point{
	int x, y;
};

int n, m;

char a[64][64];
int vis[64][64];

int d[1000], nd, res;

int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};

void solve()
{
	queue <point> q, nq; point p, np;
	int x0, y0;
	for (int x = 1; x <= n; x++)
		for (int y = 1; y <= m; y++)
			if (a[x][y] == 'S')
			{
				p.x = x; p.y = y; q.push(p); vis[x][y] = 1; a[x][y] = '.';
			}
			else if (a[x][y] == '*')
			{
				p.x = x; p.y = y; nq.push(p); vis[x][y] = -1;
			}
	while (!q.empty())
	{
		p = q.front(); int x = p.x, y = p.y; q.pop();
		if (!d[vis[x][y]])
		{
			d[vis[x][y]] = 1;
			while (!nq.empty())
			{
				np = nq.front();
				int nx = np.x, ny = np.y;
				if (vis[nx][ny] != -vis[x][y]) break;
				nq.pop();
				for (int k = 0; k < 4; k++)
				{
					int xx = nx + dx[k], yy = ny + dy[k];
					if (!vis[xx][yy] && a[xx][yy] == '.')
					{
						vis[xx][yy] = vis[nx][ny] - 1;
						np.x = xx; np.y = yy; nq.push(np);
						a[xx][yy] = '*';
					}
				}
			}
		}
		////////
		for (int k = 0; k < 4; k++)
		{
			int xx = x + dx[k], yy = y + dy[k];
			if (!vis[xx][yy] && (a[xx][yy] == '.' || a[xx][yy] == 'D'))
			{
				p.x = xx; p.y = yy; vis[xx][yy] = vis[x][y] + 1; q.push(p);
				if (a[xx][yy] == 'D') res = vis[x][y];
			}
		}
	}
	

}

int main()
{
#ifdef _DEBUG
	freopen("1366", "r", stdin);
#endif
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++)
		scanf("%s", &a[i][1]);
	solve();
	if (res)
		printf("%d\n", res);
	else
		printf("KAKTUS\n");
	return 0;
}
