Language: C++

//
//  main.cpp
//

#include <iostream>
#include <algorithm>
#include <vector>
#include <limits>
#include <math.h>
#include <queue>
#include <tuple>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include "string.h"
#include <unordered_map>
#include <unordered_set>
#include <array>
#include <set>

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0118

using namespace std;

// #define TEST

#ifdef TEST
static const string g_SAMPLE = R"(10 10
####*****@
@#@@@@#*#*
@##***@@@*
#****#*@**
##@*#@@*##
*@@@@*@@@#
***#@*@##*
*@@@*@@##@
*@*#*@##**
@****#@@#@
0 0)";
static FILE* g_IN_FP;
static const char* g_IN_FN = "in.txt";
#define scanf(...) fscanf(g_IN_FP, __VA_ARGS__)
#endif // TEST

int main() {
#ifdef TEST
    g_IN_FP = fopen(g_IN_FN, "w");
    fwrite(g_SAMPLE.c_str(), 1, g_SAMPLE.size(), g_IN_FP);
    fclose(g_IN_FP);
    g_IN_FP = fopen(g_IN_FN, "r");
#endif // TEST
    while (true) {
        int W, H;
        scanf("%d %d", &W, &H);
        if (W == 0 && H == 0) {
            return 0;
        }
        unique_ptr<char[]> field(new char[W * H]);
        for (int i = 0; i < H; i++) {
            scanf("%s", field.get()+(W*i));
        }
        vector<int> stack;
        stack.reserve(W*H);
        int count = 0;
        for (int x = 0; x < W; x++) {
            for (int y = 0; y < H; y++) {
                if (field[x+y*W] == 'X') {
                    continue;
                }
                count++;
                char fruit = field[x+y*W];
                stack.push_back(x+y*W);
                field[x+y*W] = 'X';
                while (!stack.empty()) {
                    int idx = stack.back();
                    stack.pop_back();
                    int cx = idx % W;
                    int cy = idx / W;
                    for (int dx = 0; dx < 2; dx++) {
                        for (int dy = 0; dy < 2; dy++) {
                            int nx = cx + -1 + 1*dx + 1*dy;
                            int ny = cy + 0 + (-1)*dx + 1*dy;
                            if (nx < 0 || nx >= W || ny < 0 || ny >= H) {
                                continue;
                            }
                            if (field[nx+ny*W] != fruit) {
                                continue;
                            }
                            stack.push_back(nx+ny*W);
                            field[nx+ny*W] = 'X';
                        }
                    }
                }
#ifdef TEST
                cout << "=====" << endl;
                for (int y = 0; y < H; y++) {
                    for (int x = 0; x < W; x++) {
                        cout << field[x+y*W];
                    }
                    cout << endl;
                }
                cout << "=====" << endl;
#endif // TEST
            }
        }
        cout << count << endl;
    }
    return 0;
}



Compiler output:
a.cc: In function 'int main()':
a.cc:58:9: error: 'unique_ptr' was not declared in this scope
   58 |         unique_ptr<char[]> field(new char[W * H]);
      |         ^~~~~~~~~~
a.cc:20:1: note: 'std::unique_ptr' is defined in header '<memory>'; this is probably fixable by adding '#include <memory>'
   19 | #include <set>
  +++ |+#include <memory>
   20 | 
a.cc:58:20: error: expected primary-expression before 'char'
   58 |         unique_ptr<char[]> field(new char[W * H]);
      |                    ^~~~
a.cc:60:25: error: 'field' was not declared in this scope
   60 |             scanf("%s", field.get()+(W*i));
      |                         ^~~~~
a.cc:67:21: error: 'field' was not declared in this scope
   67 |                 if (field[x+y*W] == 'X') {
      |                     ^~~~~
a.cc:71:30: error: 'field' was not declared in this scope
   71 |                 char fruit = field[x+y*W];
      |                              ^~~~~