#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N = 1e3 + 5;
const int dx[] = {0, 0, -1, 1};
const int dy[] = {1, -1, 0, 0};
char grid[N][N];
bool vis[N][N];
int dis[N][N];
map<pair<int, int>, int> idx;
void solve() {
memset(dis, 0x3f, sizeof dis);
int n, m, k;
cin >> n >> m >> k;
int sx, sy;
for (int i = 1; i <= k; i++) {
int x, y;
cin >> x >> y;
idx[{x, y}] = i;
if (i == 1) {
sx = x;
sy = y;
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> grid[i][j];
}
}
auto invalid = [&](int x, int y) {
return x < 1 || x > n || y < 1 || y > m;
}
priority_queue<tuple<int, int, int, int>> q;
q.emplace(0, sx, sy, k);
while (!q.empty()) {
auto [d, x, y, len] = q.top(); q.pop();
if (vis[x][y])
continue;
for (int i = 0; i < 4; i++) {
int xx = x + dx[i];
int yy = y + dy[i];
if (invalid(xx, yy) || grid[xx][yy] == '#')
continue;
if (idx.count({xx, yy})) {
int d = idx[{xx, yy}];
if (dis[xx][yy] > dis[x][y] + len - d + 1) {
dis[xx][yy] = dis[x][y] + 1;
q.emplace(-dis[xx][yy], xx, yy, max(0ll, len - d));
}
}
else if (dis[xx][yy] > dis[x][y] + 1) {
dis[xx][yy] = dis[x][y] + 1;
q.emplace(-dis[xx][yy], xx, yy, max(0ll, len - 1));
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cout << dis[i][j] << " ";
}
cout << endl;
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t = 1;
// cin >> t;
while (t--) solve();
return 0;
}