#include <bits/stdc++.h>
using namespace std;
const int P = 1e9 + 7;
int add(int a, int b) {
  if ((a += b) >= P) a -= P;
  return a;
}
int sub(int a, int b) {
  if ((a -= b) < 0) a += P;
  return a;
}
int mul(int a, int b) { return 1ll * a * b % P; }
int kpow(int a, int b) {
  int r = 1;
  for (; b; b >>= 1, a = mul(a, a)) {
    if (b & 1) r = mul(r, a);
  }
  return r;
}
const int N = 105;
int n, m, k, a[N][N], st[N][N], dp[N][N][300], vis[N][N][300], u, v, S, y, x,
    ans, use[N][N];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
pair<pair<pair<int, int>, int>, int> fa[N][N][300];
queue<pair<int, int> > q;
pair<pair<int, int>, int> now;
void spfa(int mask) {
  while (!q.empty()) {
    pair<int, int> u = q.front();
    q.pop();
    vis[u.first][u.second][mask] = 0;
    for (int i = (0); i < (4); i++) {
      int x = u.first + dx[i], y = u.second + dy[i];
      if (x > n || x < 1 || y > m || y < 1) continue;
      int &z = dp[x][y][mask | st[x][y]];
      if (z == -1 || z > dp[u.first][u.second][mask] + a[x][y]) {
        z = dp[u.first][u.second][mask] + a[x][y];
        fa[x][y][mask | st[x][y]] = make_pair(make_pair(u, mask), -1);
        if ((st[x][y] | mask != mask) || vis[x][y][mask]) continue;
        vis[x][y][mask] = 1;
        q.push(make_pair(x, y));
      }
    }
  }
}
void dfs(pair<pair<int, int>, int> now) {
  auto t = fa[now.first.first][now.first.second][now.second];
  int x = t.first.first.first, y = t.first.first.second;
  int m1 = t.first.second, m2 = t.second;
  if (x == 0) {
    use[now.first.first][now.first.second] = 1;
    return;
  }
  dfs(make_pair(make_pair(x, y), m1));
  if (m2 == -1) {
    use[now.first.first][now.first.second] = 1;
  } else
    dfs(make_pair(make_pair(x, y), m2));
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  cin >> n >> m >> k;
  for (int i = (1); i < (n + 1); i++)
    for (int j = (1); j < (m + 1); j++) cin >> a[i][j];
  memset(dp, -1, sizeof(dp));
  for (int i = (0); i < (k); i++) {
    cin >> u >> v;
    st[u][v] = (1ll << (i));
    dp[u][v][(1ll << (i))] = a[u][v];
  }
  S = (1ll << (k));
  for (int mask = (1); mask < (S); mask++) {
    for (int i = (1); i < (n + 1); i++)
      for (int j = (1); j < (m + 1); j++) {
        if (st[i][j] && !(st[i][j] & mask)) continue;
        int &z = dp[i][j][mask];
        for (int t = mask & (mask - 1); t > 0; t = (t - 1) & mask) {
          int &x = dp[i][j][t | st[i][j]], &y = dp[i][j][mask ^ t | st[i][j]];
          if (x != -1 && y != -1) {
            if (z == -1 || z > x + y - a[i][j]) {
              z = x + y - a[i][j];
              fa[i][j][mask] =
                  make_pair(make_pair(make_pair(i, j), t | st[i][j]),
                            mask ^ t | st[i][j]);
            }
          }
        }
        if (z != -1) q.push(make_pair(i, j)), vis[i][j][mask] = 1;
      }
    spfa(mask);
  }
  ans = (1ll << (30));
  for (int i = (1); i < (n + 1); i++)
    for (int j = (1); j < (m + 1); j++)
      if (dp[i][j][S - 1] != -1 && ans > dp[i][j][S - 1])
        ans = dp[i][j][S - 1], now = make_pair(make_pair(i, j), S - 1);
  cout << ans << "\n";
  dfs(now);
  for (int i = (1); i < (n + 1); i++) {
    for (int j = (1); j < (m + 1); j++)
      if (use[i][j])
        cout << "X";
      else
        cout << ".";
    cout << "\n";
  }
  return 0;
}
