#include <bits/stdc++.h>
using namespace std;
const long long MXN = (1LL << 8) + 10;
long long n, m, k, trn = 1, ans = 1e16, id = -1;
long long dp[MXN][MXN], A[MXN], mp[MXN][MXN];
vector<pair<long long, long long>> adj[MXN];
priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
               greater<pair<long long, long long>>>
    pq;
bitset<MXN> bs[MXN][MXN];
bool vis[MXN];
bool check(long long x, long long y) {
  return (1 <= x && x <= n && 1 <= y && y <= m);
}
void Dij(long long mask) {
  for (int u = 1; u < trn; u++) pq.push({dp[u][mask], u}), vis[u] = 0;
  while (!pq.empty()) {
    long long val = pq.top().first, u = pq.top().second;
    pq.pop();
    if (vis[u]) continue;
    vis[u] = 1;
    for (auto Pr : adj[u]) {
      long long v = Pr.first, w = Pr.second;
      if (dp[v][mask] > dp[u][mask] + w) {
        dp[v][mask] = dp[u][mask] + w;
        bs[v][mask] = bs[u][mask];
        bs[v][mask][v] = 1;
        pq.push({dp[v][mask], v});
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  memset(dp, 63, sizeof dp);
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> A[trn];
      mp[i][j] = trn, trn++;
    }
  }
  for (int i = 0; i < k; i++) {
    long long x, y;
    cin >> x >> y;
    dp[mp[x][y]][1 << i] = A[mp[x][y]];
    bs[mp[x][y]][1 << i][mp[x][y]] = 1;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (check(i - 1, j))
        adj[mp[i][j]].emplace_back(mp[i - 1][j], A[mp[i - 1][j]]);
      if (check(i + 1, j))
        adj[mp[i][j]].emplace_back(mp[i + 1][j], A[mp[i + 1][j]]);
      if (check(i, j - 1))
        adj[mp[i][j]].emplace_back(mp[i][j - 1], A[mp[i][j - 1]]);
      if (check(i, j + 1))
        adj[mp[i][j]].emplace_back(mp[i][j + 1], A[mp[i][j + 1]]);
    }
  }
  for (int mask = 1; mask < (1LL << k); mask++) {
    for (int v = 1; v < trn; v++) {
      for (int sub = mask;; sub = (sub - 1) & mask) {
        dp[v][mask] = min(dp[v][mask], dp[v][sub] + dp[v][mask ^ sub] - A[v]);
        if (dp[v][mask] == dp[v][sub] + dp[v][mask ^ sub] - A[v])
          bs[v][mask] = bs[v][sub] | bs[v][mask ^ sub];
        if (sub == 0) break;
      }
    }
    Dij(mask);
  }
  for (int i = 1; i < trn; i++) {
    if (dp[i][(1LL << k) - 1] < ans) {
      ans = dp[i][(1LL << k) - 1], id = i;
    }
  }
  cout << ans << '\n';
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cout << (bs[id][(1LL << k) - 1][mp[i][j]] ? 'X' : '.');
    }
    cout << '\n';
  }
  return 0;
}
