#include <cstdio>
#include <cmath>

using namespace std;

struct coor
{
    int x,y;
};

coor Q[25000];
int x,y;
int Ix, Iy, Cx, Cy;
int tX, tY;
int head=0, tail=-1;
char polje[500][500];
int dr=-1;
coor drvo[25000];
int min;



void pop ( void )
{
    head++;
}

void push ( int x, int y )
{
    tail++;
    Q[tail].x = x;
    Q[tail].y = y;
}

coor top ( void )
{
    return Q[head];
}

bool empty ( void )
{
    return ( head == tail );
}

int mindrvo ( int x, int y )
{
    int tm =1000000;
    for (int i=0; i<dr; ++i)
    {
        if ( fabs( x - drvo[dr].x) + fabs( y - drvo[dr].y ) < tm ) tm = fabs( x - drvo[dr].x) + fabs( y - drvo[dr].y );
    }
    return tm;
}

void bfs ( void )
{
    min = mindrvo( Ix,Iy );
    tX = Ix;
    tY = Iy;
    while ( !empty() )
    {
        pop();
        int s=0;
        if ( polje[tX+1][tY] == '.' && mindrvo( tX+1, tY ) == min ) { push ( tX, tY ); polje[tX+1][tY]=='#'; s=1; }
        if ( polje[tX-1][tY] == '.' && mindrvo( tX-1, tY ) == min ) { push ( tX, tY ); polje[tX-1][tY]=='#'; s=1; }
        if ( polje[tX][tY+1] == '.' && mindrvo( tX, tY+1 ) == min ) { push ( tX, tY ); polje[tX][tY+1]=='#'; s=1; }
        if ( polje[tX][tY-1] == '.' && mindrvo( tX, tY-1 ) == min ) { push ( tX, tY ); polje[tX][tY-1]=='#'; s=1; }
        if ( s = 0 )
        {
            if ( polje[tX+1][tY] == '.') { push ( tX, tY ); polje[tX+1][tY]=='#'; min--; }
            if ( polje[tX-1][tY] == '.') { push ( tX, tY ); polje[tX-1][tY]=='#'; min--; }
            if ( polje[tX][tY+1] == '.') { push ( tX, tY ); polje[tX][tY+1]=='#'; min--; }
            if ( polje[tX][tY-1] == '.') { push ( tX, tY ); polje[tX][tY-1]=='#'; min--; }
        }
    }
}

int main ( void )
{
	scanf("%d%d",&x,&y);
	for ( int i=0; i<x; ++i)
	{
	    scanf("%s", polje[i]);
	    for ( int j=0; j<x;++j)
	    {
	        if ( polje[i][j] = '+') { dr++; drvo[dr].x = i; drvo[dr].y = j; }
	        if ( polje[i][j] = 'J') { Cx = i; Cy = j; }
	        if ( polje[i][j] = 'V') { Ix = i; Iy = j; }
	    }
	}
    push (Ix,Iy);
    bfs();
    printf("%d", min);
	return 0;
}
