#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long b, long long md) {
  return (!b ? 1
             : (b & 1 ? a * power(a * a % md, b / 2, md) % md
                      : power(a * a % md, b / 2, md) % md));
}
const int xn = 2e2 + 10;
const int xm = -20 + 10;
const int sq = 320;
const int inf = 1e9 + 10;
const long long INF = 1e18 + 10;
const int mod = 998244353;
const int base = 257;
int n, m, k, a[xn], dp[1 << 7][xn];
bitset<xn> mark[1 << 7][xn];
priority_queue<pair<int, int>, vector<pair<int, int> >,
               greater<pair<int, int> > >
    pq;
void upd(int mask, int u, int v) {
  if (dp[mask][u] + a[v] < dp[mask][v]) {
    dp[mask][v] = dp[mask][u] + a[v];
    mark[mask][v] = mark[mask][u];
    mark[mask][v][v] = 1;
    pq.push({dp[mask][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;
    --x, --y;
    int v = x * m + y;
    dp[1 << i][v] = a[v];
    mark[1 << i][v][v] = 1;
  }
  for (int mask = 1; mask < (1 << k); ++mask) {
    for (int v = 0; v < n * m; ++v) {
      for (int sub = (mask - 1) & mask; sub > 0; sub = (sub - 1) & mask) {
        int d = dp[sub][v] + dp[mask ^ sub][v] - a[v];
        if (d < dp[mask][v])
          dp[mask][v] = d, mark[mask][v] = mark[sub][v] | mark[mask ^ sub][v];
      }
      pq.push({dp[mask][v], v});
    }
    while (!pq.empty()) {
      int v = pq.top().second, d = pq.top().first;
      pq.pop();
      if (dp[mask][v] != d) continue;
      if (0 <= v - m) upd(mask, v, v - m);
      if (v + m < n * m) upd(mask, v, v + m);
      if (0 < v % m) upd(mask, v, v - 1);
      if (v % m < m - 1) upd(mask, 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] << '\n';
  for (int i = 0; i < n; i++, cout << '\n')
    for (int j = 0; j < m; j++)
      cout << (mark[(1 << k) - 1][v][i * m + j] ? 'X' : '.');
  return 0;
}
