文件目录

#include <bits/stdc++.h>
using namespace std;
int m, n, a, b, c, d;
//(x,y) == x*m+y
int fat[1000050];
int findf(int x)
{
    if (fat[x] != x)
        fat[x] = findf(fat[x]);
    return fat[x];
}
bool uset(int a, int b)
{
    a = findf(a);
    b = findf(b);
    if (a != b)
    {
        fat[b] = a;
        return true;
    }
    return false;
}
int main()
{
    for (int i = 0; i < 1000050; i++)
        fat[i] = i;
    cin >> m >> n;
    while (cin >> a >> b >> c >> d)
        uset((a - 1) * n + b, (c - 1) * n + d);
    int ans = 0;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j < m; j++)
            if (uset((j - 1) * n + i, j * n + i))
                ans++; // 行
    for (int i = 1; i <= m; i++)
        for (int j = 1; j < n; j++)
            if (uset((i - 1) * n + j, (i - 1) * n + j + 1))
                ans += 2; // 列
    cout << ans;
    return 0;
}