#include<bits/stdc++.h>#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);using namespace std;const int N = 2e3 + 5;const int INF = 0x7f7f7f7f;struct Node { int x; int y;} node;int a[N][N],p[N][N];int b[3] = {-1,1,0};int c[3] = {0,0,1};int n,m;int bfs() { queue<Node> qu; node.x =...