Problem:
Disaster Response Simulation
PCK is participating in the World Robot Challenge in the Disaster Response Simulation category. The mission of this category is to rescue people who are stranded in a building.
The interior of the building is represented by a plane divided into $H$ rows and $W$ columns, where each square is either a wall, a path, or an obstacle. There are $N$ people left in the building, and each of the $N$ rescue robots rescues one person at a time. All the robots leave from one base station at once, and each goes to the person to be rescued. Each robot can move to the adjacent squares in the same row and column from its current position, but cannot move to squares outside of buildings or walls. The path and obstacle squares can be occupied by multiple robots at the same time. If a robot enters an obstacle square, it will take 1 damage.
The first task for PCK to accomplish the mission is to select a suitable location for the base station where he can rescue all the people who are stranded. Whenever there is a path for the robots to reach the location of the people from the base station, you can always choose the path that minimizes the damage the robots will take. Based on this assumption, we decided to look for the base station that would have the smallest maximum value of damage suffered by each robot. However, the base station must be placed in a square that represents a path, but it can be placed even if there is a person in that square.
Given information about the inside of a building and the location of a person, create a program that selects the location of a base station and reports one candidate.
Input
The input is given in the following format.
$H$ $W$ $N$
$s_{1,1}s_{1,2}$...$s_{1,W}$
$s_{2,1}s_{2,2}$...$s_{2,W}$
:
$s_{H,1}s_{H,2}$...$s_{H,W}$
$r_1$ $c_1$
$r_2$ $c_2$
:
$r_N$ $c_N$
In the first line, the number of rows $H$ ($2 \leq H \leq 300$) and the number of columns $W$ ($2 \leq W \leq 300$) of the plane representing the interior of the building, and the number of people $N$ ($ 1 \leq N \leq 300$) are given. The letters $s_{i,j}$ representing the squares in row $i$ and column $j$ are given in the subsequent $H$ lines. If $s_{i,j}$ is '
.
', '
#
', and '
@
' represent paths, walls, and obstacles, respectively. In the following $N$ lines, a pair of integers $r_i$ ($1 \leq r_i \leq H$) and $c_i$ ($1 \leq c_i \leq W$) are given to represent the location of each person. The $r_i$ and $c_i$ indicate that there is a person in the square in the $r_i$ row and $c_i$ column. However, the square with a person in it is a path and there can be no more than two people in one square.
Output
Outputs a pair of integers representing the location of the base station on a single line, separated by spaces. If there are multiple candidates, output the one with the smallest row number. If there are multiple such candidates, output the one with the smallest column number. However, if there are no candidates, output "
-1 -1
".
Sample Input and Ou
...(truncated)

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;

int dy[] = {0, 1, 0, -1}, dx[] = {1, 0, -1, 0};

int main(){
	int h, w, n;
	cin >> h >> w >> n;
	vector<string> S(h);
	for(auto &i : S) cin >> i;
	vector<array<int, 2>> A(n);
	for(auto &i : A) cin >> i[0] >> i[1];
	for(auto &i : A) i[0]--, i[1]--;
	
	int bs = 99824435, by = -1, bx = -1;
	
	vector<vector<int>> ans(h, vector<int>(w, -1));
	
	for(int a = 0; a < n; a++){
		int i = A[a][0], j = A[a][1];
		vector<vector<int>> dist(h, vector<int>(w, 998244353));
		deque<array<int, 3>> Q;
		dist[i][j] = 0;
		Q.push_back({i, j});
		while(!Q.empty()){
			int y = Q.front()[0], x = Q.front()[1], d = Q.front()[2];
			Q.pop_front();
			if(d != dist[y][x]) continue;
			
			for(int t = 0; t < 4; t++){
				int ny = y+dy[t], nx = x+dx[t];
				if(ny < 0 || nx < 0 || ny >= h || nx >= w) continue;
				if(S[ny][nx] == '#') continue;
				
				if(S[ny][nx] == '.'){
					if(dist[ny][nx] > d){
						dist[ny][nx] = d;
						Q.push_front({ny, nx, d});
					}
				} else {
					if(dist[ny][nx] > d+1){
						dist[ny][nx] = d + 1;
						Q.push_back({ny, nx, d+1});
					}
				}
			}
		}
		
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				ans[i][j] = max(ans[i][j], dist[i][j]);
			}
		}
	}
	
	for(int i = 0; i < h; i++){
		for(int j = 0; j < w; j++){
			if(bs > ans[i][j]){
				if(S[i][j] != '.') continue;
				bs = ans[i][j];
				by = i+1;
				bx = j+1;
			}
		}
	}
	
	cout << by << ' ' << bx << endl;
}
accuracy: 1
time_ms: 880
memory_kb: 4772
final_rank: 6

code_snippet: #include<bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int i=0;i<(n);i++)
#define rng(i,l,r) for(int i=(l);i<(r);i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define rrng(i,l,r) for(int i=(r)-1;i>=(l);i--)
#define int long long
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(),(x).end()

signed main(){
	int H,W,N;cin>>H>>W>>N;
	vector<string> S(H);for(auto&&e:S)cin>>e;
	
	vector<vector<int>> ans(H,vector<int>(W,-1));
	
	while(N--){
		int si,sj;cin>>si>>sj;si--,sj--;
		vector<vector<int>> d(H,vector<int>(W,1e9));
		deque<pair<int,int>> q;
		q.pb({si,sj});
		d[si][sj]=0;
		
		const int di[4]={-1,0,1,0};
		const int dj[4]={0,1,0,-1};
		
		while(q.size()){
			auto[pi,pj]=q.front();q.pop_front();
			rep(k,4){
				int ni=pi+di[k],nj=pj+dj[k];
				if(0<=ni&&ni<H&&0<=nj&&nj<W){
					if(S[ni][nj]=='@'){
						if(d[ni][nj]>d[pi][pj]+1){
							d[ni][nj]=d[pi][pj]+1;
							q.pb({ni,nj});
						}
					}
					if(S[ni][nj]=='.'){
						if(d[ni][nj]>d[pi][pj]){
							d[ni][nj]=d[pi][pj];
							q.push_front({ni,nj});
						}
					}
				}
			}
		}
		
		rep(i,H)rep(j,W){
			ans[i][j]=max(ans[i][j],d[i][j]);
		}
		
	}
	
	int mi=-2,mj=-2,mc=1e9;
	rep(i,H)rep(j,W){
		if(mc>ans[i][j]&&S[i][j]=='.'){
			mc=ans[i][j];
			mi=i,mj=j;
		}
	}
	
	cout<<mi+1<<" "<<mj+1<<"\n";
}
accuracy: 1
time_ms: 940
memory_kb: 5544
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;
//#include<atcoder/all>
//using namespace atcoder;
//using mint = modint998244353;
#define int long long
signed main() {
  int h,w,n;
  cin >> h >> w >> n;
  vector<string> s(h);
  for(int i = 0; i < h; i++) cin >> s[i];
  vector<int> a(n),b(n);
  for(int i = 0; i < n; i++) cin >> a[i] >> b[i], a[i]--, b[i]--;
  vector<vector<int>> v(h,vector<int>(w,0));
  vector<int> t = {0,1,0,-1,0};
  for(int i = 0; i < n; i++) {
    vector<vector<int>> v2(h,vector<int>(w,1e9));
    deque<pair<int,int>> q;
    q.push_back({a[i],b[i]});
    v2[a[i]][b[i]] = 0;
    while(q.size() > 0) {
      int x,y;
      tie(x,y) = q.front();
      q.pop_front();
      for(int j = 0; j < 4; j++) {
        int X = x+t[j], Y = y+t[j+1];
        if(X < 0 || X >= h || Y < 0 || Y >= w) continue;
        int z = v2[x][y];
        if(s[X][Y] == '#') continue;
        if(s[X][Y] == '@') z++;
        if(v2[X][Y] > z) {
          v2[X][Y] = z;
          if(s[X][Y] == '.') q.push_front({X,Y});
          else q.push_back({X,Y});
        }
      }
    }
    for(int j = 0; j < h; j++) for(int k = 0; k < w; k++) v[j][k] = max(v[j][k],v2[j][k]);
  }
  tuple<int,int,int> ans = {1e18,1e18,1e18};
  for(int i = 0; i < h; i++) for(int j = 0; j < w; j++) {
    if(s[i][j] == '.') {
      tuple<int,int,int> z = {v[i][j],i,j};
      ans = min(ans,z);
    }
  }
  int B,x,y;
  tie(B,x,y) = ans;
  if(B < 1e7) cout << x+1 << ' ' << y+1 << endl;
  else cout << -1 << ' ' << -1 << endl;
}
accuracy: 1
time_ms: 660
memory_kb: 5608
final_rank: 4

code_snippet: //#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
const ll INF32 = 2e9;
const ll INF64 = 4e18;
const vector<int> dr = {1,0,-1,0};
const vector<int> dc = {0,1,0,-1};

bool comp(pair<int, pair<int, int>> &a,pair<int, pair<int, int>> &b){
	return a.first > b.first;
}

signed main(){
	int H, W, N;
	cin >> H >> W >> N;
	vector<string> s(H);
	rep(i, H)cin >> s[i];
	vector<vector<int>> max_damage(H, vector<int>(W, 0));
	rep(i, N){
		int r, c;
		cin >> r >> c;
		r--; c--;
		vector<vector<int>> damage(H, vector<int>(W, INF32));
		vector<vector<bool>> kaku(H, vector<bool>(W, false));
		damage[r][c] = 0;
		priority_queue<pair<int,pair<int, int>>,
					vector<pair<int,pair<int, int>>>, greater<pair<int,pair<int, int>>>> pq;
		pq.push({0,{r,c}});
		while(!pq.empty()){
			auto [t, nrc] = pq.top(); pq.pop();
			int nr = nrc.first, nc = nrc.second;
			if(kaku[nr][nc])continue;
			kaku[nr][nc] = true;
			rep(j, dr.size()){
				int nxr = nr+dr[j], nxc = nc+dc[j];
				if(nxr<0||H<=nxr||nxc<0||W<=nxc)continue;
				if(s[nxr][nxc]=='#')continue;
				int dd = (s[nxr][nxc]=='@');
				if(damage[nxr][nxc]>damage[nr][nc]+dd){
					damage[nxr][nxc]=damage[nr][nc]+dd;
					pq.push({damage[nxr][nxc],{nxr,nxc}});
				}
			}
		}
		rep(j, H)rep(k, W)max_damage[j][k] = max(max_damage[j][k], damage[j][k]);
	}
	int mi = INF32;
	int ansr = -1, ansc = -1;
	rep(i, H)rep(j, W){
		if(s[i][j]=='#'||s[i][j]=='@')continue;
		if(max_damage[i][j]<mi){
			mi = max_damage[i][j];
			ansr = i; ansc = j;
		}
	}
	if(ansr==-1){
		cout << "-1 -1" << endl;
		return 0;
	}
	ansr++; ansc++;
	cout << ansr << " " << ansc << endl;
	return 0;
}
accuracy: 1
time_ms: 2510
memory_kb: 5596
final_rank: 13

code_snippet: #include <iostream>
#include <deque>
using namespace std;
int main(){
    int H,W,N;
    cin >> H >> W >> N;
    char road[H+2][W+2];
    deque<pair<int,int>> deq;
    int INF = 1e8;
    for(int i = 0; i < H+2; i++){
        for(int j = 0; j < W+2; j++){
            road[i][j] = '#';
        }
    }
    for(int i = 1; i <= H; i++){
        for(int j = 1; j <= W; j++){
            cin >> road[i][j];
        }
    }
    pair<int,int> rc[N];
    for(int i = 0; i < N; i++){
        int r,c;
        cin >> r >> c;
        rc[i] = {r,c};
    }
    int D[H+2][W+2] = {0};
    for(int i = 0; i < H+2; i++){
        for(int j = 0; j < W+2; j++){
            D[i][j] = -1;
        }
    }
    int dx[] = {1,0,-1,0};
    int dy[] = {0,1,0,-1};
    bool V[H+2][W+2] = {false};
    for(int i = 0; i < N; i++){
        int d[H+2][W+2];
        bool visited[H+2][W+2] = {false};
        deq.push_back(rc[i]);
        auto[x0,y0] = rc[i];
        d[x0][y0] = 0;
        while(!deq.empty()){
            auto[x,y] = deq.front();
            deq.pop_front();
            for(int j = 0; j < 4; j++){
                int X = x+dx[j];
                int Y = y+dy[j];
                if(visited[X][Y]){
                    continue;
                }
                if(road[X][Y] == '.'){
                    deq.push_front({X,Y});
                    visited[X][Y] = true;
                    d[X][Y] = d[x][y];
                    D[X][Y] = max(d[X][Y],D[X][Y]);
                }
                if(road[X][Y] == '@'){
                    visited[X][Y] = true;
                    deq.push_back({X,Y});
                    d[X][Y] = d[x][y]+1;
                    D[X][Y] = max(d[X][Y],D[X][Y]);
                }
            }
        }
        for(int j = 0; j < H+2; j++){
            for(int k = 0; k < W+2; k++){
                if(!visited[j][k]){
                    V[j][k] = true;
                }
            }
        }
    }
    pair<int,int>ans;
    int M = INF;
    for(int i = 1; i <= H; i++){
        for(int j = 1; j <= W; j++){
            if(V[i][j] || road[i][j] != '.'){
                continue;
            }
            if(M > D[i][j]){
                ans = {i,j};
                M = D[i][j];
            }
        }
    }
    if(M == INF){
        cout << -1 << ' ' << -1;
    }else{
        auto [x,y] = ans;
        cout << x << ' ' << y;
    }
    cout << endl;
    return 0;
}
accuracy: 1
time_ms: 570
memory_kb: 4864
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main() {
    // input
    int h, w, n; cin >> h >> w >> n;
    vector<string> s(h, "");
    vector<int> x(n, 0), y(n, 0);
    for (int i = 0; i < h; i++) cin >> s[i];
    for (int i = 0; i < n; i++) {
        cin >> x[i] >> y[i];
        x[i]--; y[i]--;
    }

    // brute
    vector<vector<int>> val(h, vector<int>(w, 0));
    for (int p = 0; p < n; p++) {
        vector<vector<int>> dst(h, vector<int>(w, 100000));
        vector<vector<bool>> used(h, vector<bool>(w, false));
        deque<pair<int, int>> q;
        q.push_back(make_pair(x[p], y[p]));
        dst[x[p]][y[p]] = 0;

        // bfs
        while (!q.empty()) {
            int sx = q.front().first;
            int sy = q.front().second; q.pop_front();
            if (used[sx][sy]) continue;
            used[sx][sy] = true;
            for (int d = 0; d < 4; d++) {
                int tx = sx + dx[d];
                int ty = sy + dy[d];
                if (tx < 0 || tx >= h || ty < 0 || ty >= w || s[tx][ty] == '#') continue;
                int cost = (s[tx][ty] == '@' ? 1 : 0);
                if (dst[tx][ty] > dst[sx][sy] + cost) {
                    dst[tx][ty] = dst[sx][sy] + cost;
                    if (s[tx][ty] == '@') {
                        q.push_back(make_pair(tx, ty));
                    }
                    else {
                        q.push_front(make_pair(tx, ty));
                    }
                }
            }
        }

        // update
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                val[i][j] = max(val[i][j], dst[i][j]);
            }
        }
    }

    // output
    int minx = (1 << 30);
    pair<int, int> min_id = make_pair(-2, -2);
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (s[i][j] != '.') continue;
            if (minx > val[i][j]) {
                minx = val[i][j];
                min_id = make_pair(i, j);
            }
        }
    }
    if (minx > 90000) {
        cout << "-1 -1" << endl;
    }
    else {
        cout << min_id.first + 1 << " " << min_id.second + 1 << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 650
memory_kb: 4612
final_rank: 3

code_snippet: #include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <set>
#include <queue>
#include <deque>
using ll = long long;
using namespace std;
struct Edge {
	int to;
	int cost;
};
int main() {
	int H, W, N;cin >> H >> W >> N;
	vector<string> S(H);
	for (int i = 0;i < H;i++) cin >> S[i];

	vector<vector<Edge>> G(H*W);
	vector<pair<int,int>> df = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
	for (int i = 0;i < H;i++) for (int j = 0;j < W;j++) {
		if (S[i][j] == '#') continue;
		int now = (W*i) + j;
		for (auto d : df) {
			int nx = i + d.first;
			int ny = j + d.second;
			if (nx < 0 or nx >= H) continue;
			if (ny < 0 or ny >= W) continue;
			if (S[nx][ny] == '#') continue;
			int nv = (W * nx) + ny;
			if (S[nx][ny] == '@') {
				G[now].push_back({nv, 1});
			} else {
				G[now].push_back({nv, 0});
			}
		}
	}	

	vector<vector<int>> dist(N, vector<int>(H*W, -1));
	for (int i = 0;i < N;i++) {
		int x, y;cin >> x >> y;
		x--;y--;
		int st = (W * x) + y;
		dist[i][st] = 0;
		deque<int> pq;
		pq.push_back(st);
		while (!pq.empty()) {
			int v = pq.front();pq.pop_front();
			for (auto nv : G[v]) {
				if (dist[i][nv.to] == -1) {
					dist[i][nv.to] = dist[i][v] + nv.cost;
					if (nv.cost == 1) {
						pq.push_back(nv.to);
					} else {
						pq.push_front(nv.to);
					}
				}
			}
		}
	}
	pair<int, int> res = {-1, -1};
	int mxs = (int)1e9;
	for (int i = 0;i < H;i++) for (int j = 0;j < W;j++) {
		if (S[i][j] != '.') continue;
		int now = (W * i) + j;
		int mx = 0;
		bool flg = true;
		for (int k = 0;k < N;k++) {
			if (dist[k][now] == -1) {
				flg = false;
				break;
			}
			mx = max(mx, dist[k][now]);
		}
		if (flg and mx < mxs) {
			res = {i+1, j+1};
			mxs = mx;
		}
	}
	
	cout << res.first << " " << res.second << endl;
}
accuracy: 1
time_ms: 540
memory_kb: 115752
final_rank: 10

code_snippet: #include<bits/stdc++.h>

using namespace std;

#define rep(i, l, n) for(int i = int(l); i < int(n); i++)
#define ll long long
#define ld long double
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

template<class T> bool chmin(T &a, T b) {if(a > b) {a = b; return true;} return false;}
template<class T> bool chmax(T &a, T b) {if(a < b) {a = b; return true;} return false;}

const int inf = 2e9;
const ll INF = 2e18;
const ld eps = 1e-12;
const ld pi = acos(-1);

struct Point {
    
    long double x, y;

    Point() : x(0.00), y(0.00) {}
    Point(long double a, long double b) : x(a), y(b) {}
    
    Point &operator+=(const Point &p) {
        x += p.x; y += p.y;
        return (*this);
    }
    Point &operator-=(const Point &p) {
        x -= p.x; y -= p.y;
        return (*this);
    }
    Point &operator*=(const long double &k) {
        x *= k; y *= k;
        return (*this);
    }
    Point &operator*=(const Point &p) {
        long double a = x * p.x - y * p.y;
        long double b = x * p.y + y * p.x;
        x = a; y = b;
        return (*this);
    }
    Point &operator/=(const long double &k) {
        x /= k; y /= k;
        return (*this);
    }
    bool operator==(const Point &p) {
        return abs(x - p.x) >= eps && abs(y - p.y) < eps;
    }
    bool operator!=(const Point &p) {
        return abs(x - p.x) >= eps || abs(y - p.y) >= eps;
    }
    Point operator+(const Point &p) {return Point(*this) += p;}
    Point operator-(const Point &p) {return Point(*this) -= p;}
    Point operator*(const long double &k) {return Point(*this) *= k;}
    Point operator*(const Point &p) {return Point(*this) *= p;}
    Point operator/(const long double &k) {return Point(*this) /= k;}
    
    long double dot(const Point &p) {
        return x * p.x + y * p.y;
    }
    long double det(const Point &p) {
        return x * p.y - y * p.x;
    }
    
    long double arg() {
        return atan2(y, x);
    }    
    
    friend istream &operator>>(istream &is, Point &p) {
        is >> p.x >> p.y;
        return is;
    }
    
    friend ostream &operator<<(ostream &os, Point &p) {
        os << '(' << p.x << " " << p.y << ")";
        return os;
    }
    
};

struct Segment {
    Point a, b;
    Segment() : a(), b() {}
    Segment(Point a_, Point b_) : a(a_), b(b_) {}
};

long double angle(Point a, Point b, Point c) {
    Point v = (a - b), u = (c - b);
    long double theta = abs(v.arg() - u.arg());
    return min(theta, 2.00 * pi - theta);
}

int ccw(Point a, Point b, Point c) {
    long double k = (b - a).det(c - a);
    if(k > eps) return 1;
    else if(k < -eps) return -1;
    if((b - a).dot(c - a) < -eps) return -2;
    if((a - b).dot(c - b) < -eps) return 2;
    return 0;
}

bool intersect(Segment s, Segment t) {
    if(ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) > 0) return false;
    return (ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0);
}

Point crosspoint(Segment l, Segment m) {
    long double s = (l.b - l.a).det(m.b - m.a);
    long double t = (l.b - l.a).det(l.b - m.a);
    if(abs(s) < eps && abs(t) < eps) return m.a;
    return m.a + (m.b - m.a) * t / s;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

int main() {
    int h, w, n; cin >> h >> w >> n;
    string g[h];
    rep(i, 0, h) {
        cin >> g[i];
    }
    pair<int, int> p[n];
    rep(i, 0, n) cin >> p[i].first >> p[i].second;
    int ans[h][w], dist[h][w];
    rep(i, 0, h) rep(j, 0, w) {
        ans[i][j] = 0;
    }
    for(auto[sx, sy] : p) {
        sx--, sy--;
        rep(i, 0, h) rep(j, 0, w) dist[i][j] = inf;
        deque<pair<int, int>> q;
        q.emplace_back(sx, sy);
        dist[sx][sy] = 0;
        while(!q.empty()) {
            auto[x, y] = q.front(); q.pop_front();
            rep(k, 0, 4) {
                int nx = x + dx[k], ny = y + dy[k];
                if(nx < 0 || nx >= h || ny < 0 || ny >= w) continue;
                if(g[nx][ny] == '#') continue;
                int cost = 0;
                if(g[nx][ny] == '@') cost++;
                if(chmin(dist[nx][ny], dist[x][y] + cost)) {
                    if(cost) q.emplace_back(nx, ny);
                    else q.emplace_front(nx, ny);
                }
            }
        }
        rep(i, 0, h) rep(j, 0, w) {
            chmax(ans[i][j], dist[i][j]);
        }
    }
    int total = inf;
    pair<int, int> place(inf, inf);
    rep(i, 0, h) rep(j, 0, w) {
        if(g[i][j] == '@') continue;
        if(chmin(total, ans[i][j])) {
            place = make_pair(i, j);
        }
    }
    if(total == inf) cout << "-1 -1\n";
    else cout << ++place.first << " " << ++place.second << "\n";
}
accuracy: 1
time_ms: 800
memory_kb: 4592
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)

vector<int> di = {0, 1, 0, -1};
vector<int> dj = {1, 0, -1, 0};

int main() {
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	
	int H, W, N;
	cin >> H >> W >> N;
	vector S(H, vector<char>(W));
	rep(i, 0, H) rep(j, 0, W) cin >> S[i][j];
	
	vector res(N, vector(H, vector<int>(W, -1)));
	deque<pair<int, int>> que;
	rep(i, 0, N) {
		int r, c;
		cin >> r >> c;
		--r, --c;
		res[i][r][c] = 0;
		que.push_front({r, c});
		
		while (!que.empty()) {
			int vi = que.front().first;
			int vj = que.front().second;
			que.pop_front();
			
			rep(dir, 0, 4) {
				int ni = vi + di[dir];
				int nj = vj + dj[dir];
				if (ni < 0 || H <= ni || nj < 0 || W <= nj) continue;
				if (S[ni][nj] == '#' || res[i][ni][nj] != -1) continue;
				
				if (S[ni][nj] == '.') {
					res[i][ni][nj] = res[i][vi][vj];
					que.push_front({ni, nj});
				} else {
					res[i][ni][nj] = res[i][vi][vj] + 1;
					que.push_back({ni, nj});
				}
			}
		}
	}
	
	int mini= 1 << 30;
	int ans_i = -1, ans_j = -1;
	rep(i, 0, H) rep(j, 0, W) if (S[i][j] == '.') {
		int maxi = -1;
		bool cant = false;
		rep(k, 0, N) {
			if (res[k][i][j] == -1) cant = true;
			maxi = max(maxi, res[k][i][j]);
		}
		
		if (cant) continue;
		if (mini > maxi) {
			mini = maxi;
			ans_i = i + 1;
			ans_j = j + 1;
		}
	}
	
	cout << ans_i << ' ' << ans_j << '\n';
}
accuracy: 1
time_ms: 680
memory_kb: 112760
final_rank: 11

code_snippet: #include <bits/stdc++.h>

#define ll int
#define endl "\n"

using namespace std;

int main(){
    ll h,w,n;
    cin >> h >> w >> n;
    string s[h+1];
    for(ll i=1;i<=h;i++){
        cin >> s[i];
        s[i] = " "+s[i];
    }
    vector<vector<vector<ll>>>t(n,vector<vector<ll>>(h+1,vector<ll>(w+1,INT32_MAX)));
    vector<vector<vector<bool>>>CH(n,vector<vector<bool>>(h+1,vector<bool>(w+1,true)));
    for(ll i=0;i<n;i++){
        ll a,b;cin>>a>>b;
        vector<vector<bool>> ch (h+1,vector<bool>(w+1,false));
        priority_queue<pair<ll,pair<ll,ll>>,vector<pair<ll,pair<ll,ll>>>,greater<pair<ll,pair<ll,ll>>>> S;
        S.push({0,{a,b}});
        t[i][a][b] = 0;
        while(!S.empty()){
            ll now_nom = S.top().first;
            pair<ll,ll> now = S.top().second;
            S.pop();

            if(t[i][now.first][now.second] <= now_nom&&ch[now.first][now.second])continue;
            ch[now.first][now.second] = true;

            if(now.first-1>0&&s[now.first-1][now.second]!='#'){
                ll K = 0;
                if(s[now.first-1][now.second]=='@')K++;
                if(t[i][now.first-1][now.second]>now_nom+K){
                    t[i][now.first-1][now.second] = now_nom+K;
                    S.push({t[i][now.first-1][now.second],{now.first-1,now.second}});
                    CH[i][now.first-1][now.second] = false;
                }
            }
            if(now.first+1<=h&&s[now.first+1][now.second]!='#'){
                ll K = 0;
                if(s[now.first+1][now.second]=='@')K++;
                if(t[i][now.first+1][now.second]>t[i][now.first][now.second]+K){
                    t[i][now.first+1][now.second] = t[i][now.first][now.second]+K;
                    S.push({t[i][now.first+1][now.second],{now.first+1,now.second}});
                    CH[i][now.first+1][now.second] = false;
                }
            }
            if(now.second+1<=w&&s[now.first][now.second+1]!='#'){
                ll K = 0;
                if(s[now.first][now.second+1]=='@')K++;
                if(t[i][now.first][now.second+1]>t[i][now.first][now.second]+K){
                    t[i][now.first][now.second+1] = t[i][now.first][now.second]+K;
                    S.push({t[i][now.first][now.second+1],{now.first,now.second+1}});
                    CH[i][now.first][now.second+1] = false;
                }
            }
            if(now.second-1>0&&s[now.first][now.second-1]!='#'){
                ll K = 0;
                if(s[now.first][now.second-1]=='@')K++;
                if(t[i][now.first][now.second-1]>t[i][now.first][now.second]+K){
                    t[i][now.first][now.second-1] = t[i][now.first][now.second]+K;
                    S.push({t[i][now.first][now.second-1],{now.first,now.second-1}});
                    CH[i][now.first][now.second-1] = false;
                }
            }
        }
    }
    ll ans = INT32_MAX;
    bool FLAG = false;
    bool Cheak = false;
    pair<ll,ll>pos={-1,-1};
    for(ll i=1;i<=h;i++){
        for(ll j=1;j<=w;j++){
            ll Max = 0;
            bool flag = true;
            bool pp = true;
            for(ll k=0;k<n;k++){
                Max = max(Max,t[k][i][j]);
                if(t[k][i][j] == INT32_MAX||s[i][j]=='@'||s[i][j]=='#')flag = false;
                // if(CH[k][i][j])pp = false;
            }
            if(pp&&flag && ans > Max){
                FLAG = true;
                Cheak = true;
                ans = Max;
                pos.first = i;
                pos.second = j;
            }
        }
    }

    // for(ll i=0;i<n;i++){
    //     for(ll j=1;j<=h;j++){
    //         for(ll k=1;k<= w;k++){
    //             cout << t[i][j][k] << " ";
    //         }
    //         cout << endl;
    //     }
    // }
    // cout << endl << endl;

    if(FLAG && Cheak)cout << pos.first << " " << pos.second << endl;
    else cout << "-1 -1\n";
}
accuracy: 1
time_ms: 2570
memory_kb: 120704
final_rank: 15

code_snippet: #include <bits/stdc++.h>
//
using namespace std;
typedef long long ll;
typedef long double ld;
using Graph = vector<vector<ll>>;
template <typename T>
bool chmax(T &a, const T &b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
template <typename T>
bool chmin(T &a, const T &b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
#define reps(i, a, n) for (ll i = (a); i < (ll)(n); ++i)
#define rep(i, n) reps(i, 0, n)
#define rrep(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define rreps(i ,a, n) for (ll i = (ll)(n)-1; i >=(ll) a; i--)
#define ALL(box) (box).begin(), (box).end()
#define all(box) (box).begin(), (box).end()
#define RALL(box) (box).rbegin(), (box).rend()
#define rall(box) (box).rbegin(), (box).rend()
ll inf=((1LL<<62)-(1LL<<31))/15;
ll sum = 0;
ll num = 0;
int pum = 0;
ll mum = 0;
int min1 = 1000000;
int max1 = 0;
ll min2 = inf;
ll max2 = -inf;
ll MOD1 = 1000000007;
ll MOD = 998244353;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0}; // 右、下、左、上
int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy8[8] = {1, 1, 0, -1, -1, -1, 0, 1};
// sort(box.rbegin(), box.rend());
// printf("%.7Lf", n);
// reverse(t.begin(), t.end());
// unique(box.begin(), box.end());
// auto It = lower_bound(ALL(box), n);以上 4
// auto It = upper_bound(ALL(box), n);含まない上 7
// cout << box.end() - It ; 末尾までの距離
// cout << It - a.begin() ; 先頭までの距離
// auto It =box.upper_bound( k);set,multiset
// pqueue < int, vector<int>, greater<int>> q;
// priority_queue<pii, vector<pii>, greater<pii>> pq;//小さい順
// segtree<ll, op, e> seg(N);
// lazy_segtree<S, op, e, F, mapping, composition, id> seg(N);
// box.erase(unique(all(box), box.end());
// st.erase(st.find(5));
ll zaatu(vector<ll>&A){map<ll,ll>m;for(auto&&x:A)m[x]=0;ll ret = 0;for(auto&&[key,val]:m)val=ret++;for(auto&&x:A)x=m[x];return ret;}
char ban[330][330];
int box[330][330];
int ans_box[330][330];
int H,W;
void query(int A,int B){
    deque<tuple<int,int,int>> qu;
    qu.push_front({A,B,0});
    rep(i,H){
        rep(j,W){
            box[i][j]=100000000;
        }
    }
    box[A][B]=0;
    while(!qu.empty()){
        auto[a,b,c]=qu.front();
        qu.pop_front();
        rep(d,4){
            int n=dx[d]+a;
            int m=dy[d]+b;
            if(n<0||m<0||n>=H||m>=W) continue;
            int k=0;
            if(ban[n][m]=='@') k=1;
            if(ban[n][m]=='#') continue;
            if(chmin(box[n][m],c+k)){
                if(k==0){
                    qu.push_front({n,m,c+k});
                }
                else{
                    qu.push_back({n,m,c+k});
                }
            }
        }
    }
    rep(i,H){
        rep(j,W){
            chmax(ans_box[i][j],box[i][j]);
        }
    }
}
int main(){
    cin>>H>>W;
    int N;
    cin>>N;
    rep(i,H){
        rep(j,W){
            cin>>ban[i][j];
            ans_box[i][j]=-2;
        }
    }
    rep(i,N){
        int a,b;cin>>a>>b;a--;b--;
        query(a,b);
    }
    int min2=100000000;
    rep(i,H){
        rep(j,W){
            if(ban[i][j]=='.')chmin(min2,ans_box[i][j]);
        }
    }
    //cout<<ans_box[140][147]<<" "<<ans_box[134][142]<<" "<<endl;
    rep(i,H){
            rep(j,W){
                //cout <<ans_box[i][j]<<" ";
            }
            //cout <<endl;
        }
    if(min2>=100000000){
        cout <<-1<<" "<<-1<<endl;
    }
    else{
        rep(i,H){
            rep(j,W){
                if(min2==ans_box[i][j]){
                    cout <<i+1<<" "<<j+1<<endl;
                    return 0;
                }
            }
        }
    }
        
}
accuracy: 0.8666666666666667
time_ms: 790
memory_kb: 4560
final_rank: 20

code_snippet: #include <bits/stdc++.h>
//
using namespace std;
typedef long long ll;
typedef long double ld;
using Graph = vector<vector<ll>>;
template <typename T>
bool chmax(T &a, const T &b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
template <typename T>
bool chmin(T &a, const T &b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
#define reps(i, a, n) for (ll i = (a); i < (ll)(n); ++i)
#define rep(i, n) reps(i, 0, n)
#define rrep(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define rreps(i ,a, n) for (ll i = (ll)(n)-1; i >=(ll) a; i--)
#define ALL(box) (box).begin(), (box).end()
#define all(box) (box).begin(), (box).end()
#define RALL(box) (box).rbegin(), (box).rend()
#define rall(box) (box).rbegin(), (box).rend()
ll inf=((1LL<<62)-(1LL<<31))/15;
ll sum = 0;
ll num = 0;
int pum = 0;
ll mum = 0;
int min1 = 1000000;
int max1 = 0;
ll min2 = inf;
ll max2 = -inf;
ll MOD1 = 1000000007;
ll MOD = 998244353;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0}; // 右、下、左、上
int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy8[8] = {1, 1, 0, -1, -1, -1, 0, 1};
// sort(box.rbegin(), box.rend());
// printf("%.7Lf", n);
// reverse(t.begin(), t.end());
// unique(box.begin(), box.end());
// auto It = lower_bound(ALL(box), n);以上 4
// auto It = upper_bound(ALL(box), n);含まない上 7
// cout << box.end() - It ; 末尾までの距離
// cout << It - a.begin() ; 先頭までの距離
// auto It =box.upper_bound( k);set,multiset
// pqueue < int, vector<int>, greater<int>> q;
// priority_queue<pii, vector<pii>, greater<pii>> pq;//小さい順
// segtree<ll, op, e> seg(N);
// lazy_segtree<S, op, e, F, mapping, composition, id> seg(N);
// box.erase(unique(all(box), box.end());
// st.erase(st.find(5));
ll zaatu(vector<ll>&A){map<ll,ll>m;for(auto&&x:A)m[x]=0;ll ret = 0;for(auto&&[key,val]:m)val=ret++;for(auto&&x:A)x=m[x];return ret;}
char ban[330][330];
int box[330][330];
int ans_box[330][330];
int H,W;
void query(int A,int B){
    deque<pair<int,int>> qu;
    qu.push_front({A,B});
    rep(i,H){
        rep(j,W){
            box[i][j]=100000;
        }
    }
    box[A][B]=0;
    while(!qu.empty()){
        auto[a,b]=qu.front();
        qu.pop_front();
        rep(d,4){
            int n=dx[d]+a;
            int m=dy[d]+b;
            if(n<0||m<0||n>=H||m>=W) continue;
            int k=0;
            if(ban[n][m]=='@') k=1;
            if(ban[n][m]=='#') continue;
            if(chmin(box[n][m],box[a][b]+k)){
                if(k==0){
                    qu.push_front({n,m});
                }
                else{
                    qu.push_back({n,m});
                }
            }
        }
    }
    rep(i,H){
        rep(j,W){
            chmax(ans_box[i][j],box[i][j]);
        }
    }
}
int main(){
    cin>>H>>W;
    int N;
    cin>>N;
    rep(i,H){
        rep(j,W){
            cin>>ban[i][j];
            ans_box[i][j]=-2;
        }
    }
    rep(i,N){
        int a,b;cin>>a>>b;a--;b--;
        query(a,b);
    }
    int min2=100000;
    rep(i,H){
        rep(j,W){
            if(ban[i][j]=='.')chmin(min2,ans_box[i][j]);
        }
    }
    rep(i,H){
            rep(j,W){
                //cout <<ans_box[i][j]<<" ";
            }
            //cout <<endl;
        }
    if(min2>=100000){
        cout <<-1<<" "<<-1<<endl;
    }
    else{
        rep(i,H){
            rep(j,W){
                if(min2==ans_box[i][j]){
                    cout <<i+1<<" "<<j+1<<endl;
                    return 0;
                }
            }
        }
    }
        
}
accuracy: 0.8666666666666667
time_ms: 770
memory_kb: 4476
final_rank: 19

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
using ll = long long;
bool chmin(ll &a,ll b){
  if(a>b){
    a=b;
    return true;
  }
  return false;
}
int main(){
  int n,m,v;
  cin>>n>>m>>v;
  vector<string> s(n);
  for(int i=0;i<n;i++) cin>>s[i];
  vector<vector<ll>> a(n,vector<ll>(m));
  vector<vector<ll>> b(n,vector<ll>(m));
  vector<int> dx={0,1,0,-1},dy={1,0,-1,0};
  for(int i=0;i<v;i++){
    int x,y;
    cin>>x>>y;
    x--;
    y--;
    for(int j=0;j<n;j++){
      for(int k=0;k<m;k++) b[j][k]=(1<<30);
    }
    b[x][y]=0;
    deque<pair<int,int>> d;
    d.push_back({x,y});
    while(d.size()){
      auto e=d.front();
      d.pop_front();
      for(int j=0;j<4;j++){
        int nx=e.fi+dx[j],ny=e.se+dy[j];
        if(nx<0||ny<0||nx>=n||ny>=m) continue;
        if(s[nx][ny]=='#') continue;
        if(s[nx][ny]=='.'){
          if(chmin(b[nx][ny],b[e.fi][e.se])) d.push_front({nx,ny});
        }
        else{
          if(chmin(b[nx][ny],b[e.fi][e.se]+1)) d.push_back({nx,ny});
        }
      }
    }
    for(int j=0;j<n;j++){
      for(int k=0;k<m;k++) a[j][k]=max(a[j][k],b[j][k]);
    }
  }
  ll now=(1<<30)-1,x=-1,y=-1;
  for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
      if(s[i][j]=='@') continue;
      if(chmin(now,a[i][j])) x=i+1,y=j+1;
    }
  }
  cout<<x<<" "<<y<<endl;
}
accuracy: 1
time_ms: 910
memory_kb: 5148
final_rank: 8

code_snippet: #pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

int main(){
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	int h,w,n;
	cin >> h >> w >> n;
	vector<vector<char>> s(h,vector<char>(w));
	for(int i = 0;i < h;i++){
		string S;
		cin >> S;
		for(int j = 0;j < w;j++){
			s[i][j] = S[j];
		}
	}
	vector<pair<int,int>> p(n);
	for(int i = 0;i < n;i++){
		cin >> p[i].first >> p[i].second;
		p[i].first--;
		p[i].second--;
	}
	vector<vector<vector<int>>> t(n,vector<vector<int>>(h,vector<int>(w,-1)));
	vector<int> dx = {0,0,1,-1};
	vector<int> dy = {1,-1,0,0};
	for(int i = 0;i < n;i++){
		deque<pair<int,pair<int,int>>> q;
		q.push_back({0,p[i]});
		while(!q.empty()){
			pair<int,pair<int,int>> it = q.front();
			q.pop_front();
			if(t[i][it.second.first][it.second.second] != -1) continue;
			t[i][it.second.first][it.second.second] = it.first;
			for(int j = 0;j < 4;j++){
				int ny = dy[j] + it.second.first;
				int nx = dx[j] + it.second.second;
				if(ny < 0 || ny >= h || nx < 0 || nx >= w) continue;
				if(t[i][ny][nx] != -1) continue;
				if(s[ny][nx] == '#') continue;
				if(s[ny][nx] == '@'){
					q.push_back({it.first + 1,{ny,nx}});
				}else{
					q.push_front({it.first,{ny,nx}});
				}
			}
		}
	}
	vector<vector<int>> c(h,vector<int>(w,-1));
	int ans = INT_MAX;
	for(int i = 0;i < h;i++){
		for(int j = 0;j < w;j++){
			if(s[i][j] != '.') continue;
			for(int k = 0;k < n;k++){
				if(t[k][i][j] == -1){
					c[i][j] = INT_MAX;
				}else{
					c[i][j] = max(c[i][j],t[k][i][j]);
				}
			}
			ans = min(ans,c[i][j]);
		}
	}
	if(ans == INT_MAX){
		cout << "-1 -1" << endl;
		exit(0);
	}
	for(int i = 0;i < h;i++){
		for(int j = 0;j < w;j++){
			if(s[i][j] != '.') continue;
			if(c[i][j] == ans){
				cout << i + 1 << " " << j + 1 << endl;
				exit(0);
			}
		}
	}
}
accuracy: 1
time_ms: 1050
memory_kb: 113156
final_rank: 12

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
using ll = long long;
bool chmin(ll &a,ll b){
  if(a>b){
    a=b;
    return true;
  }
  return false;
}
int main(){
  int n,m,v;
  cin>>n>>m>>v;
  vector<string> s(n);
  for(int i=0;i<n;i++) cin>>s[i];
  vector<vector<ll>> a(n,vector<ll>(m));
  vector<vector<ll>> b(n,vector<ll>(m));
  vector<int> dx={0,1,0,-1},dy={1,0,-1,0};
  for(int i=0;i<v;i++){
    int x,y;
    cin>>x>>y;
    x--;
    y--;
    for(int j=0;j<n;j++){
      for(int k=0;k<m;k++) b[j][k]=(1<<30);
    }
    b[x][y]=0;
    deque<pair<int,int>> d;
    d.push_back({x,y});
    while(d.size()){
      auto e=d.front();
      d.pop_front();
      for(int j=0;j<4;j++){
        int nx=e.fi+dx[j],ny=e.se+dy[j];
        if(nx<0||ny<0||nx>=n||ny>=m) continue;
        if(s[nx][ny]=='#') continue;
        if(s[nx][ny]=='.'){
          if(chmin(b[nx][ny],b[e.fi][e.se])) d.push_front({nx,ny});
        }
        else{
          if(chmin(b[nx][ny],b[e.fi][e.se]+1)) d.push_back({nx,ny});
        }
      }
    }
    for(int j=0;j<n;j++){
      for(int k=0;k<m;k++) a[j][k]=max(a[j][k],b[j][k]);
    }
  }
  ll now=(1<<30)-1,x=-1,y=-1;
  for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
      if(s[i][j]=='@') continue;
      if(chmin(now,a[i][j])) x=i+1,y=j+1;
    }
  }
  cout<<x<<" "<<y<<endl;
}
accuracy: 1
time_ms: 910
memory_kb: 5080
final_rank: 7

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
using namespace std;
using ll = long long;
ll INF = (1LL << 60);
struct Edge {
	int to;
	int cost;
};
using Graph = vector<vector<Edge>>;
vector<pair<int, int>> near(int x, int y, int h, int w) {
	vector<pair<int, int>> vec = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
	vector<pair<int, int>> ret;
	for (auto vc : vec) {
		int nx = x +vc.first;int ny = y + vc.second;
		if (nx < 0 or nx >= h) continue;
		if (ny < 0 or ny >= w)continue;
		ret.push_back({nx, ny});
	}
	return ret;
}
int cst(int x, int y, int H, int W) {
	return (W*x)+y;
}
using pp = pair<ll, int>;
void DJK(Graph& G, vector<ll>& dist, int st) {
	dist[st] = 0;
	priority_queue<pp, vector<pp>, greater<pp>> pq;
	pq.push({0, st});
	while (!pq.empty()) {
		auto tp = pq.top();pq.pop();
		int v = tp.second;ll d = tp.first;
		if (dist[v] < d) continue;
		for (auto nv : G[v]) {
			ll nd = d + nv.cost;
			if (nd < dist[nv.to]) {
				dist[nv.to] = nd;
				pq.push({nd, nv.to});
			}
		}
	}
}
int main() {
	int H, W, N;cin >> H >> W >> N;
	vector<string> S(H);
	for (int i = 0;i < H;i++) cin >> S[i];
	Graph G(H * W);
	for (int i = 0;i < H;i++) for (int j = 0;j < W;j++) {
		if (S[i][j] == '#') continue;
		int now = cst(i, j, H, W);
		for (auto nr : near(i, j, H, W)) {
			if (S[nr.first][nr.second] == '#') continue;
			int cost = (S[nr.first][nr.second] == '@' ? 1 : 0);
			int to = cst(nr.first, nr.second, H, W);
			G[now].push_back({to, cost});
		}
	}
	vector<vector<ll>> dist(N, vector<ll>(H * W, INF));
	vector<int> R(N), C(N);
	for (int i = 0;i < N;i++) {
		int r, c;cin >> r >> c;r--;c--;
		R[i] = r;C[i] = c;
		int st = cst(r, c, H, W);
		DJK(G, dist[i], st);
	}
	pair<int, int> ret = {-1, -1};
	ll mn = INF;
	for (int i = 0;i < H;i++) for (int j = 0;j < W;j++) {
		if (S[i][j] != '.') continue;
		ll sc = 0;
		for (int k = 0;k < N;k++) {
			ll d = dist[k][cst(i, j, H, W)];
			sc = max(sc, d);
		}
		if (sc < mn) {
			mn = sc;
			ret = {i + 1, j + 1};
		}
		
	}
	cout << ret.first << " " << ret.second << endl;
}
accuracy: 1
time_ms: 2190
memory_kb: 221368
final_rank: 18

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
using namespace std;
using ll = long long;



vector<vector<int>> dd = {{0,1},{0,-1},{1,0},{-1,0}};

void dijkstra(const vector<string> &G, pair<int,int> s, vector<vector<int>> &dist) {
    int h = G.size();
    int w = G[0].size();
    dist.assign(h, vector<int>(w,1e9));
    
    priority_queue<pair<int, pair<int,int>>, vector<pair<int, pair<int,int>>>, greater<pair<int, pair<int,int>>>> pq;
    dist[s.first][s.second] = 0;
    pq.push({0, s});
    
    vector<pair<int, int>> directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
    
    while (!pq.empty()) {
        auto [currentDist, coord] = pq.top();
        pq.pop();
        int y = coord.first;
        int x = coord.second;
        
        if (currentDist > dist[y][x]) continue;
        
        for (auto [dy, dx] : directions) {
            int ny = y + dy;
            int nx = x + dx;
            if (0 <= ny && ny < h && 0 <= nx && nx < w) {
                if (G[ny][nx] == '#') continue; 
                
                int nd = dist[y][x] + (G[ny][nx] == '@');
                
                if (nd < dist[ny][nx]) {
                    dist[ny][nx] = nd;
                    pq.push({nd, {ny, nx}});
                }
            }
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    
    int h,w,n;
    cin >> h >> w >> n;
    vector<string> s(h);
    rep(i,h) cin >> s[i];


    vector<int> y(n),x(n);
    vector<vector<vector<int>>> ds(n);
    rep(i,n){
        cin >> y[i] >> x[i];
        y[i]--;x[i]--;
        vector<vector<int>> dist(h,vector<int>(w,1e9));
        dijkstra(s, make_pair(y[i],x[i]), dist);
        ds[i] = dist;
    }

    int ans = 1e9;

    pair<int,int> z = {-1,-1};
    rep(i,h){
        rep(j,w){
            if(s[i][j]!='.') continue;
            int ma = 0;
            rep(k,n){
                ma = max(ma,ds[k][i][j]);
            }
            if(ans>ma){
                z = make_pair(i,j);
                ans = ma;
            }
        }
    }
    if(ans>=1e9) cout << -1 << " " << -1 << endl;
    else cout << z.first+1 << " " << z.second+1 << endl;

}
accuracy: 1
time_ms: 2470
memory_kb: 112612
final_rank: 14

code_snippet: #include <bits/stdc++.h>

int dx[4]{ 1, 0, -1, 0 }, dy[4]{ 0, 1, 0, -1 };

int main() {
    int H, W, N;
    std::cin >> H >> W >> N;
    std::vector<std::string> S(H);
    for (auto& s : S) std::cin >> s;
    const int INF{(int)2e9};
    std::vector sum(H, std::vector<int>(W));
    for (int i{} ; i < N ; i++) {
        int r, c;
        std::cin >> r >> c;
        r--; c--;
        assert(S[r][c] == '.');
        std::vector dist(H, std::vector<int>(W, INF));
        dist[r][c] = 0;
        std::deque<std::pair<int, int>> que;
        que.push_back(std::make_pair(r, c));
        while (que.size()) {
            auto [x, y]{que.front()};
            que.pop_front();
            for (int d{} ; d < 4 ; d++) {
                int nx{x + dx[d]}, ny{y + dy[d]};
                if (nx < 0 or nx >= H) continue;
                if (ny < 0 or ny >= W) continue;
                if (S[nx][ny] == '#') continue;
                int w{S[nx][ny] == '@'};
                if (dist[nx][ny] > dist[x][y] + w) {
                    dist[nx][ny] = dist[x][y] + w;
                    if (w) que.push_back(std::make_pair(nx, ny));
                    else que.push_front(std::make_pair(nx, ny));
                }
            }
        }
        for (int x{} ; x < H ; x++) {
            for (int y{} ; y < W ; y++) {
                sum[x][y] = std::max(sum[x][y], dist[x][y]);
            }
        }     
    }
    int x{-2}, y{-2}, val{INF};
    for (int i{} ; i < H ; i++) {
        for (int j{} ; j < W ; j++) {
            if (sum[i][j] == INF or S[i][j] != '.') continue;
            if (val > sum[i][j]) {
                x = i;
                y = j;
                val = sum[i][j];
            }
        }
    }
    std::cout << x + 1 << ' ' << y + 1 << '\n';
}
accuracy: 1
time_ms: 640
memory_kb: 4700
final_rank: 2

code_snippet: #line 1 "main.cpp"
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,fma")
#line 1 "/opt/library/template.hpp"
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using i64 = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;

template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'000'001'000;
template <>
constexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>;
template <>
constexpr double infty<double> = infty<ll>;
template <>
constexpr long double infty<long double> = infty<ll>;
#define inf infty<ll>

using pi = pair<ll, ll>;
using vi = vector<ll>;
using vvi = vector<vector<ll>>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;

#define vv(type, name, h, ...) \
  vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...)   \
  vector<vector<vector<type>>> name( \
      h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)       \
  vector<vector<vector<vector<type>>>> name( \
      a, vector<vector<vector<type>>>(       \
             b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))

#define rep1(a) for (ll _ = 0; _ < (ll)(a); ++_)
#define rep2(i, a) for (ll i = 0; i < (ll)(a); ++i)
#define rep3(i, a, b) for (ll i = a; i < (ll)(b); ++i)
#define rep4(i, a, b, c) for (ll i = a; i < (ll)(b); i += (c))
#define rrep1(a) for (ll i = (a)-1; i >= (ll)(0); --i)
#define rrep2(i, a) for (ll i = (a)-1; i >= (ll)(0); --i)
#define rrep3(i, a, b) for (ll i = (b)-1; i >= (ll)(a); --i)
#define rrep4(i, a, b, c) for (ll i = (b)-1; i >= (ll)(a); i -= (c))
#define overload4(a, b, c, d, e, ...) e
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep(...) overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)

#define all(x) (x).begin(),(x).end()
#define len(x) (ll)(x.size())
#define elif else if
#define bit(x, i) (((x)>>(i))&1)

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second

#define stoi stoll
#define abs llabs

#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) \
  sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()

ll popcnt(ll x) { return __builtin_popcountll(x); }
ll popcnt(u64 x) { return __builtin_popcountll(x); }
ll popcnt_mod_2(ll x) { return __builtin_parityll(x); }
ll popcnt_mod_2(u64 x) { return __builtin_parityll(x); }
ll topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
ll topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
ll lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
ll lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }

template <typename T>
T floor(T a, T b) {
  return a / b - (a % b && (a ^ b) < 0);
}
template <typename T>
T ceil(T x, T y) {
  return floor(x + y - 1, y);
}
template <typename T>
T bmod(T x, T y) {
  return x - y * floor(x, y);
}
template <typename T>
pair<T, T> divmod(T x, T y) {
  T q = floor(x, y);
  return {q, x - q * y};
}

template<typename T> T SUM(const vector<T> &A) {
  T s = 0; for (auto &&a: A) s += a;
  return s;
}

template <typename T>
T POP(queue<T> &que) {
  T a = que.front();
  que.pop();
  return a;
}
template <typename T>
T POP(deque<T> &que) {
  T a = que.front();
  que.pop_front();
  return a;
}
template <typename T>
T POP(pq<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(pqg<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(vc<T> &que) {
  T a = que.back();
  que.pop_back();
  return a;
}

template <typename F>
ll binary_search(F check, ll ok, ll ng, bool check_ok = true) {
  if (check_ok) assert(check(ok));
  while (abs(ok - ng) > 1) {
    auto x = (ng + ok) / 2;
    (check(x) ? ok : ng) = x;
  }
  return ok;
}
template <typename F>
f128 binary_search_real(F check, f128 ok, f128 ng, ll iter = 100) {
  rep(iter) {
    f128 x = (ok + ng) / 2;
    (check(x) ? ok : ng) = x;
  }
  return (ok + ng) / 2;
}

// ? は -1
vc<int> s_to_vi(const string &S, char first_char) {
  vc<int> A(S.size());
  rep(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }
  return A;
}

template <typename T, typename U>
vc<T> cumsum(vc<U> &A, ll off = 1) {
  ll N = A.size();
  vc<T> B(N + 1);
  rep(i, N) { B[i + 1] = B[i] + A[i]; }
  if (off == 0) B.erase(B.begin());
  return B;
}

// stable sort
template <typename T>
vi argsort(const vector<T> &A) {
  vi ids(len(A));
  iota(all(ids), 0);
  sort(all(ids),
       [&](ll i, ll j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });
  return ids;
}

// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vi &I) {
  vc<T> B(len(I));
  rep(i, len(I)) B[i] = A[I[i]];
  return B;
}

template<typename T> inline bool chmax(T &a, T b) {return ((a<b)?(a=b,true):(false));}
template<typename T> inline bool chmin(T &a, T b) {return ((a>b)?(a=b,true):(false));}

inline void wt(const char c) { cout << c; }
inline void wt(const string s) { cout << s; }
inline void wt(const char *s) { cout << s; }

template <typename T>
void wt_integer(T x) {
  cout << (x);
}
template <typename T>
void wt_real(T x) {
  cout << fixed << setprecision(15) << (long double)(x);
}
template <typename T>
void wt_integer128(T x) {
  char buf[64];
  char *d = end(buf);
  d--; *d = '\0';
  __uint128_t tmp = ((x < 0)? -x : x);
  do {
    d--; *d = char(tmp%10 + '0'); tmp /= 10;
  } while (tmp);
  if (x < 0) {
    d--; *d = '-';
  }
  cout << d;
}

inline void wt(int x) { wt_integer(x); }
inline void wt(ll x) { wt_integer(x); }
inline void wt(i128 x) { wt_integer128(x); }
inline void wt(u32 x) { wt_integer(x); }
inline void wt(u64 x) { wt_integer(x); }
inline void wt(u128 x) { wt_integer128(x); }
inline void wt(double x) { wt_real(x); }
inline void wt(long double x) { wt_real(x); }
inline void wt(f128 x) { wt_real(x); }

template <class T, class U>
void wt(const pair<T, U> val) {
  wt(val.first); wt(' '); wt(val.second);
}
template <size_t N = 0, typename T>
void wt_tuple(const T t) {
  if constexpr (N < std::tuple_size<T>::value) {
    if constexpr (N > 0) { wt(' '); }
    const auto x = std::get<N>(t);
    wt(x);
    wt_tuple<N + 1>(t);
  }
}
template <class... T>
void wt(tuple<T...> tpl) {
  wt_tuple(tpl);
}
template <class T, size_t S>
void wt(const array<T, S> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}
template <class T>
void wt(const vector<T> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}

void print() { wt('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
  wt(head);
  if (sizeof...(Tail)) wt(' ');
  print(forward<Tail>(tail)...);
}

void YES(bool t = 1) { print(t ? "YES" : "NO"); }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { print(t ? "Yes" : "No"); }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { print(t ? "yes" : "no"); }
void no(bool t = 1) { yes(!t); }
void onez(bool t = 1) { print(t ? 1 : 0); }
#define endl '\n'
#define dump(x) {cerr << #x " = " << x << '\n';}
#line 4 "main.cpp"

int solve();
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  while (!solve()) {break;}
  return 0;
}

int solve() {
  int H, W, N;
  cin >> H >> W >> N;
  vc<string> s(H);
  rep(i, H) {cin >> s[i];}
  vc<int> r(N), c(N);
  rep(i, N) {
    cin >> r[i] >> c[i];
    --r[i]; --c[i];
  }
  vvv(int, d, N, H, W, infty<int>);
  rep(k, N) {
    pqg<tuple<int,int,int>> Q;
    int t = (s[r[k]][c[k]] == '@');
    Q.emplace(mt(t, r[k], c[k]));
    d[k][r[k]][c[k]] = t;
    while (!Q.empty()) {
      auto [t, y, x] = POP(Q);
      static vc<int> dy = {0, 1, 0, -1};
      static vc<int> dx = {1, 0, -1, 0};
      if (t > d[k][y][x]) {continue;}
      rep(i, len(dy)) {
        int ny = y + dy[i];
        int nx = x + dx[i];
        if (ny < 0 or ny >= H or nx < 0 or nx >= W) {continue;}
        if (s[ny][nx] == '#') {continue;}
        int nt = t + (s[ny][nx] == '@');
        if (chmin(d[k][ny][nx], nt)) {
          Q.emplace(mt(nt, ny, nx));
        }
      }
    }
  }
  int ans = infty<int>;
  int ya=-1, xa=-1;
  rep(y, H) rep(x, W) {
    if (s[y][x] != '.') {continue;}
    auto check = [&]() -> int {
      int dmax = 0;
      rep(k, N) {
        if (d[k][y][x] == inf) {return infty<int>;}
        chmax(dmax, d[k][y][x]);
      }
      return dmax;
    };
    if (chmin(ans, check())) {
      ya = y+1;
      xa = x+1;
    }
  }
  print(ya, xa);
  return 0;
}
accuracy: 1
time_ms: 2720
memory_kb: 112652
final_rank: 17

code_snippet: #line 1 "main.cpp"
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,fma")
#line 1 "/opt/library/template.hpp"
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using i64 = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;

template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'000'001'000;
template <>
constexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>;
template <>
constexpr double infty<double> = infty<ll>;
template <>
constexpr long double infty<long double> = infty<ll>;
#define inf infty<ll>

using pi = pair<ll, ll>;
using vi = vector<ll>;
using vvi = vector<vector<ll>>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;

#define vv(type, name, h, ...) \
  vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...)   \
  vector<vector<vector<type>>> name( \
      h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)       \
  vector<vector<vector<vector<type>>>> name( \
      a, vector<vector<vector<type>>>(       \
             b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))

#define rep1(a) for (ll _ = 0; _ < (ll)(a); ++_)
#define rep2(i, a) for (ll i = 0; i < (ll)(a); ++i)
#define rep3(i, a, b) for (ll i = a; i < (ll)(b); ++i)
#define rep4(i, a, b, c) for (ll i = a; i < (ll)(b); i += (c))
#define rrep1(a) for (ll i = (a)-1; i >= (ll)(0); --i)
#define rrep2(i, a) for (ll i = (a)-1; i >= (ll)(0); --i)
#define rrep3(i, a, b) for (ll i = (b)-1; i >= (ll)(a); --i)
#define rrep4(i, a, b, c) for (ll i = (b)-1; i >= (ll)(a); i -= (c))
#define overload4(a, b, c, d, e, ...) e
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep(...) overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)

#define all(x) (x).begin(),(x).end()
#define len(x) (ll)(x.size())
#define elif else if
#define bit(x, i) (((x)>>(i))&1)

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second

#define stoi stoll
#define abs llabs

#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) \
  sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()

ll popcnt(ll x) { return __builtin_popcountll(x); }
ll popcnt(u64 x) { return __builtin_popcountll(x); }
ll popcnt_mod_2(ll x) { return __builtin_parityll(x); }
ll popcnt_mod_2(u64 x) { return __builtin_parityll(x); }
ll topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
ll topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
ll lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
ll lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }

template <typename T>
T floor(T a, T b) {
  return a / b - (a % b && (a ^ b) < 0);
}
template <typename T>
T ceil(T x, T y) {
  return floor(x + y - 1, y);
}
template <typename T>
T bmod(T x, T y) {
  return x - y * floor(x, y);
}
template <typename T>
pair<T, T> divmod(T x, T y) {
  T q = floor(x, y);
  return {q, x - q * y};
}

template<typename T> T SUM(const vector<T> &A) {
  T s = 0; for (auto &&a: A) s += a;
  return s;
}

template <typename T>
T POP(queue<T> &que) {
  T a = que.front();
  que.pop();
  return a;
}
template <typename T>
T POP(deque<T> &que) {
  T a = que.front();
  que.pop_front();
  return a;
}
template <typename T>
T POP(pq<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(pqg<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(vc<T> &que) {
  T a = que.back();
  que.pop_back();
  return a;
}

template <typename F>
ll binary_search(F check, ll ok, ll ng, bool check_ok = true) {
  if (check_ok) assert(check(ok));
  while (abs(ok - ng) > 1) {
    auto x = (ng + ok) / 2;
    (check(x) ? ok : ng) = x;
  }
  return ok;
}
template <typename F>
f128 binary_search_real(F check, f128 ok, f128 ng, ll iter = 100) {
  rep(iter) {
    f128 x = (ok + ng) / 2;
    (check(x) ? ok : ng) = x;
  }
  return (ok + ng) / 2;
}

// ? は -1
vc<int> s_to_vi(const string &S, char first_char) {
  vc<int> A(S.size());
  rep(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }
  return A;
}

template <typename T, typename U>
vc<T> cumsum(vc<U> &A, ll off = 1) {
  ll N = A.size();
  vc<T> B(N + 1);
  rep(i, N) { B[i + 1] = B[i] + A[i]; }
  if (off == 0) B.erase(B.begin());
  return B;
}

// stable sort
template <typename T>
vi argsort(const vector<T> &A) {
  vi ids(len(A));
  iota(all(ids), 0);
  sort(all(ids),
       [&](ll i, ll j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });
  return ids;
}

// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vi &I) {
  vc<T> B(len(I));
  rep(i, len(I)) B[i] = A[I[i]];
  return B;
}

template<typename T> inline bool chmax(T &a, T b) {return ((a<b)?(a=b,true):(false));}
template<typename T> inline bool chmin(T &a, T b) {return ((a>b)?(a=b,true):(false));}

inline void wt(const char c) { cout << c; }
inline void wt(const string s) { cout << s; }
inline void wt(const char *s) { cout << s; }

template <typename T>
void wt_integer(T x) {
  cout << (x);
}
template <typename T>
void wt_real(T x) {
  cout << fixed << setprecision(15) << (long double)(x);
}
template <typename T>
void wt_integer128(T x) {
  char buf[64];
  char *d = end(buf);
  d--; *d = '\0';
  __uint128_t tmp = ((x < 0)? -x : x);
  do {
    d--; *d = char(tmp%10 + '0'); tmp /= 10;
  } while (tmp);
  if (x < 0) {
    d--; *d = '-';
  }
  cout << d;
}

inline void wt(int x) { wt_integer(x); }
inline void wt(ll x) { wt_integer(x); }
inline void wt(i128 x) { wt_integer128(x); }
inline void wt(u32 x) { wt_integer(x); }
inline void wt(u64 x) { wt_integer(x); }
inline void wt(u128 x) { wt_integer128(x); }
inline void wt(double x) { wt_real(x); }
inline void wt(long double x) { wt_real(x); }
inline void wt(f128 x) { wt_real(x); }

template <class T, class U>
void wt(const pair<T, U> val) {
  wt(val.first); wt(' '); wt(val.second);
}
template <size_t N = 0, typename T>
void wt_tuple(const T t) {
  if constexpr (N < std::tuple_size<T>::value) {
    if constexpr (N > 0) { wt(' '); }
    const auto x = std::get<N>(t);
    wt(x);
    wt_tuple<N + 1>(t);
  }
}
template <class... T>
void wt(tuple<T...> tpl) {
  wt_tuple(tpl);
}
template <class T, size_t S>
void wt(const array<T, S> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}
template <class T>
void wt(const vector<T> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}

void print() { wt('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
  wt(head);
  if (sizeof...(Tail)) wt(' ');
  print(forward<Tail>(tail)...);
}

void YES(bool t = 1) { print(t ? "YES" : "NO"); }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { print(t ? "Yes" : "No"); }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { print(t ? "yes" : "no"); }
void no(bool t = 1) { yes(!t); }
void onez(bool t = 1) { print(t ? 1 : 0); }
#define endl '\n'
#define dump(x) {cerr << #x " = " << x << '\n';}
#line 4 "main.cpp"

int solve();
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  while (!solve()) {break;}
  return 0;
}

int solve() {
  int H, W, N;
  cin >> H >> W >> N;
  vc<string> s(H);
  rep(i, H) {cin >> s[i];}
  vc<int> r(N), c(N);
  rep(i, N) {
    cin >> r[i] >> c[i];
    --r[i]; --c[i];
  }
  vvv(int, d, N, H, W, infty<int>);
  rep(k, N) {
    pqg<tuple<int,int,int>> Q;
    int t = (s[r[k]][c[k]] == '@');
    Q.emplace(mt(t, r[k], c[k]));
    d[k][r[k]][c[k]] = t;
    while (!Q.empty()) {
      auto [t, y, x] = POP(Q);
      static vc<int> dy = {0, 1, 0, -1};
      static vc<int> dx = {1, 0, -1, 0};
      rep(i, len(dy)) {
        int ny = y + dy[i];
        int nx = x + dx[i];
        if (ny < 0 or ny >= H or nx < 0 or nx >= W) {continue;}
        if (s[ny][nx] == '#') {continue;}
        int nt = t + (s[ny][nx] == '@');
        if (chmin(d[k][ny][nx], nt)) {
          Q.emplace(mt(nt, ny, nx));
        }
      }
    }
  }
  int ans = infty<int>;
  int ya=-1, xa=-1;
  rep(y, H) rep(x, W) {
    if (s[y][x] != '.') {continue;}
    auto check = [&]() -> int {
      int dmax = 0;
      rep(k, N) {
        if (d[k][y][x] == inf) {return infty<int>;}
        chmax(dmax, d[k][y][x]);
      }
      return dmax;
    };
    if (chmin(ans, check())) {
      ya = y+1;
      xa = x+1;
    }
  }
  print(ya, xa);
  return 0;
}
accuracy: 1
time_ms: 2700
memory_kb: 112544
final_rank: 16