Finding smallest number of moves
I see a number of things that could help you improve your code.

Use consistent formatting

Almost every other line in the program is a blank line.  This doesn't really help readability, and then we have lines like these:

if ((j&lt;m-1) &amp;&amp; (i&lt;n-2)) { if (map[i+2][j+1]!='x'){ graph[a].push_back(a+2*m+1); }} // x+1 y+2
if ((j&lt;m-1) &amp;&amp; (i&gt;1)) { if (map[i-2][j+1]!='x'){ graph[a].push_back(a-2*m+1); }} // x+1 y-2


First, add some whitespace and second, don't jam nested if statements all on one line like that.  These could be reformatted like this:

if ((j &lt; m - 1) &amp;&amp; (i &lt; n - 2)) {
    if (map[i + 2][j + 1] != 'x') {
        graph[a].push_back(a + 2 * m + 1);
    }
}                   // x+1 y+2
if ((j &lt; m - 1) &amp;&amp; (i &gt; 1)) {
    if (map[i - 2][j + 1] != 'x') {
        graph[a].push_back(a - 2 * m + 1);
    }
}                   // x+1 y-2


Don't abuse using namespace std

Putting using namespace std at the top of every program is a bad habit that you'd do well to avoid.  

Avoid the use of global variables

graph, visited, mydistance and others are declared as global variables rather than as local variables. It's generally better to explicitly pass variables your function will need rather than using the vague implicit linkage of a global variable. For example, the visited is only used within BFS, so it should be a local variable there instead of a global.

Eliminate "magic numbers"

In a number of cases, the code uses "magic numbers" such as 990000 that have no obvious meaning.  These would be better as named constants.  

Fix the error

This line may be accepted by your compiler, but it is not compliant with the C++ standard:

char map[n][m];


The reason is that the standard will only allow this if both n and m are const and they are not in this program.  One could address that by dynamically allocating the object, as:

char *map = new char[m*n];


And then re-doing all references to change from this:

map[i][j] = x;


To this: 

map[i * m + j] = x;


However, I think the better way is in the following suggestion.

Use object orientation

Because you're writing in C++, it would make sense to have methods that operate on a class such as Board and Knight (because the legal moves you describe are those that a knight can make in the game of chess) be member functions rather than separate functions.  You may not yet have learned about objects or classes, but they're one of the main strengths of C++ and something you should learn soon if you haven't already.  Use objects where they make sense.

Have functions return something useful

Instead of a void function, it would make more sense to have BFS() return the distance.

Omit return 0

When a C++ program reaches the end of main the compiler will automatically generate code to return 0, so there is no reason to put return 0; explicitly at the end of main.