#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
template <class S>
inline void read(S& arg) {
  cin >> arg;
}
template <class S>
inline void readA(S Lptr, S Rptr) {
  while (Lptr != Rptr) {
    read(*Lptr);
    Lptr++;
  }
}
template <class S, class... T>
inline void read(S& arg, T&... rest) {
  read(arg);
  read(rest...);
}
char delimiter = ' ';
template <class S>
inline void write(S arg) {
  cout << arg << '\n';
}
template <class S, class... T>
inline void write(S arg, T... rest) {
  cout << arg << delimiter;
  write(rest...);
}
template <class S>
inline void writeA(S Lptr, S Rptr) {
  while (Lptr != Rptr) {
    cout << *Lptr;
    Lptr++;
    if (Lptr != Rptr) cout << delimiter;
  }
  cout << '\n';
}
template <class T, class S>
inline bool smin(T& a, S b) {
  return (T)b < a ? a = b, 1 : 0;
}
template <class T, class S>
inline bool smax(T& a, S b) {
  return a < (T)b ? a = b, 1 : 0;
}
constexpr int MOD = 1e9 + 9;
constexpr int N = 200 + 10;
template <typename T>
inline T mod(T& v) {
  return v = (v % MOD + MOD) % MOD;
}
template <typename T>
inline long long dom(T v) {
  return (v % MOD + MOD) % MOD;
}
template <typename S, typename T>
inline S add(S& l, T r) {
  return mod(l += r);
}
long long po(long long v, long long u) {
  return u ? po(v * v % MOD, u >> 1) * (u & 1 ? v : 1) % MOD : 1;
}
int n, m, k;
int a[N][N], pos[N];
int dist[N][N];
bitset<N> path[N][N];
int dp[1 << 7][N];
bitset<N> pd[1 << 7][N];
inline int Get(int x, int y) { return x * m + y; }
inline void Add(int i, int j, int x, int y, int d) {
  i = Get(i, j);
  x = Get(x, y);
  dist[i][x] = d;
  path[i][x][i] = true;
  path[i][x][x] = true;
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  memset(dist, 63, sizeof(dist));
  read(n, m, k);
  for (int i = 0; i < (int)n; i++) {
    for (int j = 0; j < (int)m; j++) {
      read(a[i][j]);
      Add(i, j, i, j, a[i][j]);
      if (i) {
        Add(i, j, i - 1, j, a[i - 1][j] + a[i][j]);
        Add(i - 1, j, i, j, a[i][j] + a[i - 1][j]);
      }
      if (j) {
        Add(i, j, i, j - 1, a[i][j - 1] + a[i][j]);
        Add(i, j - 1, i, j, a[i][j] + a[i][j - 1]);
      }
    }
  }
  for (int i = 0; i < (int)k; i++) {
    int x, y;
    read(x, y);
    x--, y--;
    pos[i] = Get(x, y);
  }
  int M = n * m;
  for (int k = 0; k < (int)M; k++)
    for (int i = 0; i < (int)M; i++)
      for (int j = 0; j < (int)M; j++)
        if (smin(dist[i][j], dist[i][k] + dist[k][j] - a[k / m][k % m]))
          path[i][j] = path[i][k] | path[k][j];
  for (int i = 0; i < (int)M; i++) {
    dp[0][i] = dist[i][i];
    pd[0][i] = path[i][i];
  }
  for (int mask = 1; mask < (int)(1 << k); mask++) {
    if (__builtin_popcount(mask) == 1) {
      int id = __builtin_ctz(mask);
      for (int i = 0; i < (int)M; i++) {
        dp[mask][i] = dist[pos[id]][i];
        pd[mask][i] = path[pos[id]][i];
      }
      continue;
    }
    for (int i = 0; i < (int)M; i++) {
      dp[mask][i] = 1e9;
      for (int sub = mask; sub; sub = (sub - 1) & mask)
        if (smin(dp[mask][i], dp[mask ^ sub][i] + dp[sub][i] - dist[i][i]))
          pd[mask][i] = pd[mask ^ sub][i] | pd[sub][i];
    }
    for (int i = 0; i < (int)M; i++)
      for (int j = 0; j < (int)M; j++)
        if (smin(dp[mask][i], dp[mask][j] + dist[j][i] - dist[j][j]))
          pd[mask][i] = pd[mask][j] | path[j][i];
  }
  int id = 0;
  for (int i = 0; i < (int)M; i++)
    if (dp[(1 << k) - 1][id] > dp[(1 << k) - 1][i]) id = i;
  cout << dp[(1 << k) - 1][id] << endl;
  for (int i = 0; i < (int)n; i++) {
    for (int j = 0; j < (int)m; j++) {
      if (pd[(1 << k) - 1][id][Get(i, j)])
        cout << 'X';
      else
        cout << '.';
    }
    cout << '\n';
  }
  return 0;
}
