#include <queue>
#include <fstream>
using namespace std;
ifstream fin("barbar.in");
ofstream fout("barbar.out");
const int Nmax = 1005;
char mat[Nmax][Nmax];
int matd[Nmax][Nmax], matver[Nmax][Nmax];///matrice cu distantele pana la cel mai apropiat dragon din fiecare celula a temnitei
int dirl[] = {1, 0,-1, 0};
int dirc[] = {0, 1, 0,-1};
int n, m, l, c, lstart, cstart, lfinal, cfinal;
queue <pair <int, int>> qd;
void dist_drag()
{
int i;
while(!qd.empty())
{
int lc = qd.front().first, cc = qd.front().second;
for(i = 0; i < 4; i++)
{
int lvec = lc + dirl[i], cvec = cc + dirc[i];
if((1 <= lvec && lvec <= n) and (1 <= cvec && cvec <= m) and mat[lvec][cvec] != '*' and mat[lvec][cvec] != 'D' && !matd[lvec][cvec])
matd[lvec][cvec] = matd[lc][cc] + 1, qd.push({lvec, cvec});
}
qd.pop();
}
}
bool ver(int dist)
{
int i;
queue <pair <int, int>> q;
for(l = 1; l <= n; l++)
for(c = 1; c <= m; c++)
matver[l][c] = 0;
q.push({lstart, cstart});
while(!q.empty())
{
int lc = q.front().first, cc = q.front().second;
if(lc == lfinal && cfinal == cc)
return true;
for(i = 0; i < 4; i++)
{
int lvec = lc + dirl[i], cvec = cc + dirc[i];
if((1 <= lvec && lvec <= n) and (1 <= cvec && cvec <= m) and matd[lvec][cvec] >= dist && !matver[lvec][cvec])
matver[lvec][cvec] = matver[lc][cc] + 1, q.push({lvec, cvec});
}
q.pop();
}
if(matver[lfinal][cfinal])
return true;
return false;
}
int main()
{
fin >> n >> m;
fin.get();
for(l = 1; l <= n; l++)
{
for(c = 1; c <= m; c++)
{
fin.get(mat[l][c]);
if(mat[l][c] == 'D')
qd.push({l, c});
if(mat[l][c] == 'I')
lstart = l, cstart = c;
if(mat[l][c] == 'O')
lfinal = l, cfinal = c;
}
fin.get();
}
dist_drag();
int st = 0, dr = 2005, ans;
while(st < dr)
{
int mij = (st + dr) >> 1;
if(!ver(mij))
dr = mij - 1;
else
st = mij + 1, ans = mij;
}
fout << ans;
return 0;
}