#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
const int N = 2e2 + 10;
const long long mod = 1e9 + 7;
const long long mod2 = 998244353;
const long long inf = 1e18;
const int LOG = 8;
long long pw(long long a, long long b, long long M) {
  return (!b ? 1
             : (b & 1 ? (a * pw(a * a % M, b / 2, M)) % M
                      : pw(a * a % M, b / 2, M)));
}
struct node {
  int n, m, k, A[N][N], V[LOG], B[N];
  long long dis[LOG][N], dp[1 << LOG][N];
  vector<int> G[N];
  int id(int i, int j) { return (i - 1) * m + j; }
  void dij(int id, int St) {
    for (int i = 1; i <= n * m; i++) {
      dis[id][i] = inf;
    }
    dis[id][St] = B[St];
    set<pair<long long, long long> > st;
    st.insert(make_pair(dis[id][St], St));
    while ((int)st.size()) {
      int v = st.begin()->second;
      st.erase(st.begin());
      for (auto u : G[v]) {
        if (dis[id][u] > dis[id][v] + B[u]) {
          dis[id][u] = dis[id][v] + B[u];
          st.insert(make_pair(dis[id][u], u));
        }
      }
    }
  }
  node(int _n, int _m, int _k, int B2[], pair<int, int> E[]) {
    n = _n, m = _m, k = _k;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        B[id(i, j)] = B2[(i - 1) * m + j - 1];
        if (i < n) {
          G[id(i, j)].push_back(id(i + 1, j));
          G[id(i + 1, j)].push_back(id(i, j));
        }
        if (j < m) {
          G[id(i, j)].push_back(id(i, j + 1));
          G[id(i, j + 1)].push_back(id(i, j));
        }
      }
    }
    for (int i = 0; i < k; i++) {
      int x, y;
      x = E[i].first, y = E[i].second;
      V[i] = id(x, y);
      dij(i, V[i]);
    }
    for (int i = 1; i <= n * m; i++) {
      dp[0][i] = B[i];
    }
    long long tot = inf;
    for (int mask = 1; mask < 1 << k; mask++) {
      for (int i = 1; i <= n * m; i++) {
        dp[mask][i] = inf;
      }
      set<pair<long long, long long> > st;
      for (int i = 1; i <= n * m; i++) {
        for (int j = 0; j < k; j++) {
          if (mask >> j & 1) {
            dp[mask][i] =
                min(dp[mask][i], dp[mask ^ (1 << j)][i] +
                                     (i == V[j] ? 0 : dis[j][i] - B[i]));
          }
        }
        st.insert(make_pair(dp[mask][i], i));
      }
      while ((int)st.size()) {
        int v = st.begin()->first;
        st.erase(st.begin());
        for (auto u : G[v]) {
          if (dp[mask][u] > dp[mask][v] + B[u]) {
            dp[mask][u] = dp[mask][v] + B[u];
            st.insert(make_pair(dp[mask][u], u));
          }
        }
      }
      if (mask + 1 == (1 << k)) {
        for (int i = 1; i <= n * m; i++) {
          tot = min(tot, dp[mask][i]);
        }
      }
    }
    cout << tot << endl;
  }
};
const int K = 7;
int a[N], dp[1 << K][N];
bitset<N> mark[1 << K][N];
priority_queue<pair<int, int>, vector<pair<int, int> >,
               greater<pair<int, int> > >
    pq;
pair<int, int> Aria[N];
void upd(int msk, int u, int v) {
  if (dp[msk][u] + a[v] < dp[msk][v]) {
    dp[msk][v] = dp[msk][u] + a[v];
    mark[msk][v] = mark[msk][u];
    mark[msk][v][v] = 1;
    pq.push(make_pair(dp[msk][v], v));
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m, k;
  cin >> n >> m >> k;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) cin >> a[i * m + j];
  memset(dp, 63, sizeof dp);
  for (int i = 0; i < k; i++) {
    int x, y;
    cin >> x >> y;
    Aria[i] = make_pair(x, y);
    x--;
    y--;
    int v = x * m + y;
    dp[1 << i][v] = a[v];
    mark[1 << i][v][v] = 1;
  }
  for (int msk = 1; msk < (1 << k); msk++) {
    for (int v = 0; v < n * m; v++) {
      for (int sub = (msk - 1) & msk; sub; sub = (sub - 1) & msk) {
        int d = dp[sub][v] + dp[msk ^ sub][v] - a[v];
        if (d < dp[msk][v]) {
          dp[msk][v] = d;
          mark[msk][v] = mark[sub][v] | mark[msk ^ sub][v];
        }
      }
      pq.push(make_pair(dp[msk][v], v));
    }
    while (!pq.empty()) {
      int v = pq.top().second, d = pq.top().first;
      pq.pop();
      if (dp[msk][v] != d) continue;
      if (0 <= v - m) upd(msk, v, v - m);
      if (v + m < n * m) upd(msk, v, v + m);
      if (0 < v % m) upd(msk, v, v - 1);
      if (v % m < m - 1) upd(msk, v, v + 1);
    }
  }
  int v = 0;
  for (int i = 0; i < n * m; i++)
    if (dp[(1 << k) - 1][i] < dp[(1 << k) - 1][v]) v = i;
  cout << dp[(1 << k) - 1][v] << endl;
  for (int i = 0; i < n; i++, cout << endl)
    for (int j = 0; j < m; j++)
      cout << (mark[(1 << k) - 1][v][i * m + j] ? 'X' : '.');
  return 0;
}
