#include #include #include using namespace std; typedef pair coor; typedef struct{ int x; int y; }dir; const dir D[]={ {0,-1},{0,1},{-1,0},{1,0} }; int main(){ int W,H,N; scanf("%d%d%d",&W,&H,&N); vector >M(H*2-1); for(auto &e:M)e.resize(W*2-1); for(;N--;){ int n,x,y; scanf("%d%d",&n,&x); for(int i=0;i0)for(int i=0;i0)for(int i=0;i m={{{0,0},0}}; queue q; for(q.push({0,0});!q.empty();){ coor cur=q.front();q.pop(); if(cur==make_pair(W*2-2,H*2-2))return !printf("%d\n",m[cur]); for(auto &d:D){ if(0<=cur.first+d.x&&cur.first+d.x