func_code_string: #include <bits/stdc++.h>   using namespace std;   long long i, j, k, l, n, m, s, sum, K;   double an;   const long long N = 1020;   long long f[N][N], lu[N][N], ld[N][N], ru[N][N], rd[N][N];   long long Lu[N][N], Ld[N][N], Ru[N][N], Rd[N][N];   int hang[N], lie[N];   char a[N][N];   void work(long long x, long long y) {     long long s = 0;     long long sum = 0;     for (long long i = 1; i < y; i++) {       sum += s;       f[x][i] += sum;       s++;     }     sum += 3 * s;     for (long long i = y + 1; i <= m; i++) {       sum += s;       f[x][i] += sum;       s++;     }     s = 0;     sum = 0;     for (long long i = m; i > y; i--) {       sum += s;       f[x][i] += sum;       s++;     }     sum += 3 * s;     for (long long i = y - 1; i >= 1; i--) {       sum += s;       f[x][i] += sum;       s++;     }     s = 0;     sum = 0;     for (long long i = 1; i < x; i++) {       sum += s;       f[i][y] += sum;       s++;     }     sum += 3 * s;     for (long long i = x + 1; i <= n; i++) {       sum += s;       f[i][y] += sum;       s++;     }     s = 0;     sum = 0;     for (long long i = n; i > x; i--) {       sum += s;       f[i][y] += sum;       s++;     }     sum += 3 * s;     for (long long i = x - 1; i >= 1; i--) {       sum += s;       f[i][y] += sum;       s++;     }   }   int main() {     scanf( %I64d%I64d , &n, &m);     for (long long i = 1; i <= n; i++) {       scanf( %s , a[i] + 1);       for (long long j = 1; j <= m; j++)         if (a[i][j] ==  X ) {           hang[i] = j;           lie[j] = i;           break;         }     }     for (long long i = 1; i <= n; i++) {       long long s = 0;       long long sum = 0;       for (long long j = 1; j <= m; j++) {         sum += s;         lu[i][j] = lu[i - 1][j] + sum + Lu[i - 1][j] * 1;         if (a[i][j] !=  X ) s++;         Lu[i][j] = Lu[i - 1][j] + s;       }     }     for (long long i = 1; i <= n; i++) {       long long s = 0;       long long sum = 0;       for (long long j = m; j >= 1; j--) {         sum += s;         ru[i][j] = ru[i - 1][j] + sum + Ru[i - 1][j] * 1;         if (a[i][j] !=  X ) s++;         Ru[i][j] = Ru[i - 1][j] + s;       }     }     for (long long i = n; i >= 1; i--) {       long long s = 0;       long long sum = 0;       for (long long j = 1; j <= m; j++) {         sum += s;         ld[i][j] = ld[i + 1][j] + sum + Ld[i + 1][j] * 1;         if (a[i][j] !=  X ) s++;         Ld[i][j] = Ld[i + 1][j] + s;       }     }     for (long long i = n; i >= 1; i--) {       long long s = 0;       long long sum = 0;       for (long long j = m; j >= 1; j--) {         sum += s;         rd[i][j] = rd[i + 1][j] + sum + Rd[i + 1][j] * 1;         if (a[i][j] !=  X ) s++;         Rd[i][j] = Rd[i + 1][j] + s;       }     }     long long sx = 0;     for (long long i = 1; i <= n; i++) {       bool ok = false;       for (long long j = 1; j <= m; j++)         if (a[i][j] ==  X ) {           sx++;           work(i, j);           ok = true;           break;         }       if (!ok) work(i, 0);     }     for (long long j = 1; j <= m; j++) {       bool ok = false;       for (long long i = 1; i <= n; i++)         if (a[i][j] ==  X ) {           ok = true;           break;         }       if (!ok) work(0, j);     }     for (long long i = 1; i <= n; i++)       for (long long j = 1; j <= m; j++)         if (a[i][j] !=  X ) {           an += lu[i - 1][j - 1] + ru[i - 1][j + 1] + ld[i + 1][j - 1] +                 rd[i + 1][j + 1];           an += (Lu[i - 1][j - 1] + Ru[i - 1][j + 1] + Ld[i + 1][j - 1] +                  Rd[i + 1][j + 1]) *                 2;           an += f[i][j];         }     for (i = 1; i < n; i++) {       int j = i;       while (hang[j] && hang[j + 1] && (hang[j + 1] > hang[j])) j++;       for (int x = i; x < j; x++)         for (int y = x + 1; y <= j; y++) {           if (hang[x] < hang[y])             an += 4 * (hang[x] - 1) * (m - hang[y]);           else             an += 4 * (hang[y] - 1) * (m - hang[x]);         }       i = j;     }     for (i = 1; i < n; i++) {       int j = i;       while (hang[j] && hang[j + 1] && (hang[j + 1] < hang[j])) j++;       for (int x = i; x < j; x++)         for (int y = x + 1; y <= j; y++) {           if (hang[x] < hang[y])             an += 4 * (hang[x] - 1) * (m - hang[y]);           else             an += 4 * (hang[y] - 1) * (m - hang[x]);         }       i = j;     }     for (i = 1; i < m; i++) {       int j = i;       while (lie[j] && lie[j + 1] && (lie[j + 1] > lie[j])) j++;       for (int x = i; x < j; x++)         for (int y = x + 1; y <= j; y++) {           if (lie[x] < lie[y])             an += 4 * (lie[x] - 1) * (n - lie[y]);           else             an += 4 * (lie[y] - 1) * (n - lie[x]);         }       i = j;     }     for (i = 1; i < m; i++) {       int j = i;       while (lie[j] && lie[j + 1] && (lie[j + 1] < lie[j])) j++;       for (int x = i; x < j; x++)         for (int y = x + 1; y <= j; y++) {           if (lie[x] < lie[y])             an += 4 * (lie[x] - 1) * (n - lie[y]);           else             an += 4 * (lie[y] - 1) * (n - lie[x]);         }       i = j;     }     an = an / ((n * m - sx) * (n * m - sx));     printf( %.8lf , an);     return 0;   }   