#include <bits/stdc++.h>
using namespace std;
const int maxn = 210, INF = 1 * 1000 * 1000 * 1000 + 10, maxm = 150, maxk = 10;
int n, m, k, sar, now, par[maxn], x[maxk], c[maxn], d[maxn], f[maxn][maxn],
    dp[maxm][maxn], dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1}, cur, X, Y,
                    save;
vector<int> t[maxm][maxn], path[maxn][maxn];
queue<int> q;
bool mark[maxn];
char ans[maxn][maxn];
inline bool valid(int &a, int &b) { return a >= 0 && a < n && b >= 0 && b < m; }
inline void SPFA() {
  while (!q.empty()) {
    sar = q.front();
    X = sar / m;
    Y = sar % m;
    q.pop();
    mark[sar] = 0;
    for (int i = 0; i < 4; i++) {
      X += dx[i];
      Y += dy[i];
      save = X * m + Y;
      if (valid(X, Y) && d[save] > d[sar] + c[save]) {
        d[save] = d[sar] + c[save];
        par[save] = sar;
        if (!mark[save]) {
          q.push(save);
          mark[save] = 1;
        }
      }
      X -= dx[i];
      Y -= dy[i];
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin >> n >> m >> k;
  for (int i = 0; i < n * m; i++) cin >> c[i];
  for (int i = 0; i < n * m; i++) {
    for (int j = 0; j < n * m; j++) d[j] = INF;
    d[i] = 0;
    par[i] = -1;
    mark[i] = 1;
    q.push(i);
    SPFA();
    for (int j = 0; j < n * m; j++) {
      f[i][j] = d[j];
      now = j;
      while (par[now] != -1) {
        path[i][j].push_back(now);
        now = par[now];
      }
    }
  }
  memset(dp, 63, sizeof dp);
  for (int i = 0; i < k; i++) {
    cin >> X >> Y;
    X--;
    Y--;
    x[i] = X * m + Y;
    for (int j = 0; j < n * m; j++) {
      dp[1 << i][j] = f[x[i]][j] + c[x[i]];
      t[1 << i][j] = path[x[i]][j];
      t[1 << i][j].push_back(x[i]);
    }
  }
  for (int i = 3; i < (1 << k); i++) {
    if (__builtin_popcount(i) > 1) {
      for (int j = 0; j < n * m; j++) {
        for (int sub = i; sub; sub = (sub - 1) & i) {
          if (sub == i) continue;
          cur = i ^ sub;
          for (int p = 0; p < n * m; p++) {
            if (dp[sub][p] + dp[cur][p] + f[p][j] - c[p] < dp[i][j]) {
              dp[i][j] = dp[sub][p] + dp[cur][p] + f[p][j] - c[p];
              save = sub;
              X = p;
            }
          }
        }
        cur = i ^ save;
        t[i][j] = path[X][j];
        for (int p = 0; p < ((int(t[save][X].size()))); p++)
          t[i][j].push_back(t[save][X][p]);
        for (int p = 0; p < ((int(t[cur][X].size()))); p++) {
          if (t[cur][X][p] != X) t[i][j].push_back(t[cur][X][p]);
        }
      }
    }
  }
  cout << dp[(1 << k) - 1][x[0]];
  for (int i = 0; i < ((int(t[(1 << k) - 1][x[0]].size()))); i++)
    ans[t[(1 << k) - 1][x[0]][i] / m][t[(1 << k) - 1][x[0]][i] % m] = 'X';
  for (int i = 0; i < n; i++) {
    cout << endl;
    for (int j = 0; j < m; j++) {
      if (ans[i][j] == 'X')
        cout << ans[i][j];
      else
        cout << '.';
    }
  }
  return 0;
}
