[TASK]:
You will be given lines of pseudocode, your task is to write the corresponding C++ code. The pseudocode will provide detailed description of the c++ code line by line. The pseudocode is garanteed to be correct and complete.

[INSTRUCTION]:
The following libraries are already included in the code.
```cpp
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <cstring>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <fstream>
#include <climits>
#include <cassert>
#include <iomanip>
#include <sstream>
#include <bitset>
using namespace std;
```
Do not include them in your code again. Please surround your code with ```cpp and ``` markers. Note that the code should correspond to the pseudocode line by line.

[PSEUDOCODE]:
create new constant integer MAX_N = 50
new integer constant MAX_M with value 50
define new 2d array of integers v with size MAX_N by MAX_M
define new 2d array of integers u MAX_N by MAX_M elements
define new integers n and m
void function dfs with int arguments x, y and first
    if first is false, assign the new value = 1 to u[y][x]
    if x > 0 and v[y][x - 1] is true and u[y][x - 1] is false, call dfs(x - 1, y)
    call dfs(x + 1, y) if x < m - 1 and v[y][x + 1] is true and u[y][x + 1] is false
    if y > 0 and v[y - 1][x] is true and u[y - 1][x] is false, run function dfs(x, y - 1)
    if y < n - 1 and v[y + 1][x] is true and u[y + 1][x] is false, call method dfs(x, y + 1)
end of the block
define the main function
    read n and m from the user input
    create new integer called k = 0
    for i from 0 to n exclusive
        new string variable s
        read variable s from the input
        start for loop from j = 0 to m exclusive
            assign the new value = (true if s[j]='#' or false otherwise), to v[i][j]
            add v[i][j] to k
        end of the block
    end of the block
    if k <= 2
        print - 1
        return to indicate successful execution
    end of the block
    define boolean f = false
    start for loop from i = 0 to n exclusive
        increment j in a loop from 0 to m exclusive
            if v[i][j] is false, skip the rest of the loop
            set first MAX_N * MAX_M *sizeof(int) bytes at the pointer u to 0
            assign 0 to v[i][j]
            create boolean variable with name f = false
            for integer ii = 0 to n exclusive incrementing ii, while f is false
                in a for loop, change jj from 0 to m exclusive, while f is false
                    if v[ii][jj] != 0
                        call dfs(jj, ii)
                        assign true to f
                    end of the block
                end of the block
            end of the block
            start for loop from ii = 0 to n exclusive
                loop through jj from 0 to m exclusive incrementing by 1
                    if i = ii and j = jj, go to the start of the loop
                    if v[ii][jj] != u[ii][jj]
                        print 1 and a new line
                        return to indicate successful execution
                    end of the block
                end of the block
            end of the block
            change the value of v[i][j] to 1
        end of the block
    end of the block
    print 2
    return to indicate successful execution
end of the block

[CODE]:

const int MAX_N = 50;
const int MAX_M = 50;
int v[MAX_N][MAX_M];
int u[MAX_N][MAX_M];
int n, m;
void dfs(int x, int y, int first = 0) {
    if (!first) u[y][x] = 1;
    if (x > 0 && v[y][x - 1] && !u[y][x - 1]) dfs(x - 1, y);
    if (x < m - 1 && v[y][x + 1] && !u[y][x + 1]) dfs(x + 1, y);
    if (y > 0 && v[y - 1][x] && !u[y - 1][x]) dfs(x, y - 1);
    if (y < n - 1 && v[y + 1][x] && !u[y + 1][x]) dfs(x, y + 1);
}
int main() {
    cin >> n >> m;
    int k = 0;
    for (int i = 0; i < n; ++i) {
        string s;
        cin >> s;
        for (int j = 0; j < m; ++j) {
            v[i][j] = (s[j] == '#');
            k += v[i][j];
        }
    }
    if (k <= 2) {
        cout << -1 << endl;
        return 0;
    }
    bool f = false;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if (!v[i][j]) continue;
            memset(u, 0, MAX_N * MAX_M * sizeof(int));
            v[i][j] = 0;
            bool f = false;
            for (int ii = 0; ii < n && !f; ++ii) {
                for (int jj = 0; jj < m && !f; ++jj) {
                    if (v[ii][jj] != 0) {
                        dfs(jj, ii);
                        f = true;
                    }
                }
            }
            for (int ii = 0; ii < n; ++ii) {
                for (int jj = 0; jj < m; ++jj) {
                    if (i == ii && j == jj) continue;
                    if (v[ii][jj] != u[ii][jj]) {
                        cout << 1 << endl;
                        return 0;
                    }
                }
            }
            v[i][j] = 1;
        }
    }
    cout << 2 << endl;
    return 0;
}