func_code_string: #include <bits/stdc++.h>   using namespace std;   int cnt, dfn[800005], ed[800005], tot, head[800005], tr[800005 * 4],       sum[800005 * 4], fuckljx, n, m, vis[800005], de[800005];   char c[800005];   vector<char> mp[800005];   struct edge {     int to, next;   } data[800005 * 2];   struct hh {     long long x, a, b, val;   } edge[800005 * 2];   bool operator<(hh A, hh B) { return A.x < B.x; }   void add_edge(int x, int y) {     if (!x || !y) return;     de[y]++;     data[++tot].next = head[x];     data[tot].to = y;     head[x] = tot;   }   int id(int x, int y) {     if (x < 1 || x > n || y < 1 || y > m) return 0;     return (x - 1) * m + y;   }   void dfs(int x, int f) {     vis[x] = 1;     dfn[x] = ++cnt;     for (int i = head[x]; i; i = data[i].next) {       int t = data[i].to;       if (t == f) continue;       dfs(t, x);     }     ed[x] = cnt;   }   void add(int x, int l, int r, int s, int t, int val) {     if (l > t || r < s) return;     if (s <= l && r <= t) {       if (val == 1) {         sum[x]++;         tr[x] = (r - l + 1);       } else {         sum[x]--;         if (!sum[x]) tr[x] = tr[x * 2] + tr[x * 2 + 1];       }       return;     }     int mid = (l + r) / 2;     add(x * 2, l, mid, s, t, val);     add(x * 2 + 1, mid + 1, r, s, t, val);     if (!sum[x])       tr[x] = tr[x * 2] + tr[x * 2 + 1];     else       tr[x] = (r - l + 1);   }   int main() {     cin >> n >> m;     for (int i = 1; i <= n; i++) {       scanf( %s , c + 1);       for (int j = 1; j <= m; j++) {         if (c[j] ==  U ) add_edge(id(i - 1, j), id(i + 1, j));         if (c[j] ==  D ) add_edge(id(i + 1, j), id(i - 1, j));         if (c[j] ==  L ) add_edge(id(i, j - 1), id(i, j + 1));         if (c[j] ==  R ) add_edge(id(i, j + 1), id(i, j - 1));         mp[i].push_back(c[j]);       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         if (((i + j) & 1) && !de[id(i, j)]) dfs(id(i, j), 0);       }     }     cnt = 0;     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         if (!((i + j) & 1) && !de[id(i, j)]) dfs(id(i, j), 0);       }     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         if (mp[i][j - 1] ==  U ) {           if ((i + j) & 1) {             edge[++fuckljx] =                 (hh){dfn[id(i, j)], dfn[(id(i + 1, j))], ed[(id(i + 1, j))], 1};             edge[++fuckljx] = (hh){ed[id(i, j)] + 1, dfn[(id(i + 1, j))],                                    ed[(id(i + 1, j))], -1};           } else {             edge[++fuckljx] =                 (hh){dfn[id(i + 1, j)], dfn[(id(i, j))], ed[(id(i, j))], 1};             edge[++fuckljx] =                 (hh){ed[id(i + 1, j)] + 1, dfn[(id(i, j))], ed[(id(i, j))], -1};           }         }         if (mp[i][j - 1] ==  L ) {           if ((i + j) & 1) {             edge[++fuckljx] =                 (hh){dfn[id(i, j)], dfn[(id(i, j + 1))], ed[(id(i, j + 1))], 1};             edge[++fuckljx] = (hh){ed[id(i, j)] + 1, dfn[(id(i, j + 1))],                                    ed[(id(i, j + 1))], -1};           } else {             edge[++fuckljx] =                 (hh){dfn[id(i, j + 1)], dfn[(id(i, j))], ed[(id(i, j))], 1};             edge[++fuckljx] =                 (hh){ed[id(i, j + 1)] + 1, dfn[(id(i, j))], ed[(id(i, j))], -1};           }         }       }     }     sort(edge + 1, edge + 1 + fuckljx);     long long last = 0, ans = 0;     for (int i = 1; i <= fuckljx; i++) {       ans += (long long)tr[1] * (edge[i].x - last);       last = edge[i].x;       add(1, 1, cnt + 3, edge[i].a, edge[i].b, edge[i].val);     }     cout << ans;   }   