[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]:
define the main function
    create integers n, m
    read n read m
    create 2d character array a of size n by m
    for i = 0 to n exclusive
        for j = 0 to m exclusive, read a[i][j]
    end of the block
    for i = 0 to n exclusive
        if not count of '1' from a[i] to a[i] + m
            print -1 print '\n'
            return to indicate successful execution
        end of the block
    end of the block
    create 2d integer arrays nearest, l, r, with nearest size n by m, l size n by m, r size n by m
    for i = 0 to n exclusive
        create integer lst with lst = -1
        for j = 0 to m exclusive
            if a[i][j] is '1', set lst to j
        end of the block
        create boolean c with c = true
        for j = 0 to m exclusive
            set lst to j if a[i][j] is '1', else lst
            set c to (c bitwise and a[i][j]) is 0
            set l[i][j] to m - lst + j if c is true, else absolute value of j - lst
        end of the block
        set lst to -1
        set c to true
        for j = 0 to m exclusive
            if a[i][j] is '1'
                set lst to j
                break loop
            end of the block
        end of the block
        for j = m - 1 to 0 inclusive, decrementing j
            set lst to j if a[i][j] is '1', else lst
            set c to (c bitwise and a[i][j]) is 0
            set r[i][j] to lst + 1 + m - ( j + 1 ) if c is true, else absolute value of j - lst
        end of the block
    end of the block
    for i = 0 to n exclusive
        for j = 0 to m exclusive, set nearest[i][j] to minimum of r[i][j] and l[i][j]
    end of the block
    create integer ans with ans = 0x3f3f3f3f
    for j = 0 to m exclusive
        create integer cur with cur = 0
        for i = 0 to n exclusive, increment cur by nearest[i][j]
        set ans to minimum of ans and cur
    end of the block
    print ans print '\n'
end of the block

[CODE]:

int main() {
    int n, m;
    cin >> n >> m;
    char a[n][m];
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) { cin >> a[i][j]; }
    }
    for (int i = 0; i < n; ++i) {
        if (!count(a[i], a[i] + m, '1')) {
            cout << -1 << '\n';
            return 0;
        }
    }
    int nearest[n][m], l[n][m], r[n][m];
    for (int i = 0; i < n; ++i) {
        int lst = -1;
        for (int j = 0; j < m; ++j) {
            if (a[i][j] == '1') lst = j;
        }
        bool c = true;
        for (int j = 0; j < m; ++j) {
            lst = (a[i][j] == '1' ? j : lst);
            c &= a[i][j] == '0';
            l[i][j] = (c ? m - lst + j : abs(j - lst));
        }
        lst = -1;
        c = true;
        for (int j = 0; j < m; ++j) {
            if (a[i][j] == '1') {
                lst = j;
                break;
            }
        }
        for (int j = m - 1; j >= 0; --j) {
            lst = (a[i][j] == '1' ? j : lst);
            c &= a[i][j] == '0';
            r[i][j] = (c ? lst + 1 + m - (j + 1) : abs(j - lst));
        }
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) { nearest[i][j] = min(r[i][j], l[i][j]); }
    }
    int ans = 0x3f3f3f3f;
    for (int j = 0; j < m; ++j) {
        int cur = 0;
        for (int i = 0; i < n; ++i) { cur += nearest[i][j]; }
        ans = min(ans, cur);
    }
    cout << ans << '\n';
}