This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
int hr[2][2] = {{1, 0}, {-1, 0}};
int vr[2][2] = {{0, 1}, {0, -1}};
int n, m;
bool vis[101][101];
bool valid(int x, int y) {return (min(x, y) >= 1 && x <= n && y <= m && !vis[x][y]);}
queue<pair<int, int> > q;
int cnt, x, y;
int main()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
        {
            char c;
            cin >> c;
            if (c == '.')
                vis[i][j] = true;
        }
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            if (!vis[i][j])
            {
                q.push({i, j});
                vis[i][j] = true;
                int skok = 0;
                while (!q.empty())
                {
                    skok++;
                    x = q.front().F;
                    y = q.front().S;
                    q.pop();
                    for (int i = 0; i < 2; i++)
                    {
                        int cx = x + hr[i][0];
                        int cy = y + hr[i][1];
                        if (valid(cx, cy))
                        {
                            q.push({cx, cy});
                            vis[cx][cy] = true;
                        }
                    }
                }
                if (skok > 1)
                    cnt++;
                while (!q.empty())
                    q.pop();
                q.push({i, j});
                skok = 0;
                while (!q.empty())
                {
                    x = q.front().F;
                    y = q.front().S;
                    q.pop();
                    skok++;
                    for (int i = 0; i < 2; i++)
                    {
                        int cx = x + vr[i][0];
                        int cy = y + vr[i][1];
                        if (valid(cx, cy))
                        {
                            q.push({cx, cy});
                            vis[cx][cy] = true;
                        }
                    }
                }
                if (skok > 1)
                    cnt++;
            }
    cout << cnt + 1;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |