[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
    let n and m be integers
    declare string called st
    read n and m
    res is an integer array with size m
    fill res with 0
    for i = 0 to n exclusive
        read st
        if there is no character "1" in st
            print -1 and a new line
            return to indicate successful execution
        end of the block
        cur is an integer array with 3 * m elements
        curAns is an array of integers with size m
        for j = 0 to 3 * m exclusive set cur[j] to st[j % m] - '0'
        declare integer called counter = 0
        for a = m - 1 to 0 inclusive counting down
            if cur[a] = 1
                break the loop
            else
                increase counter
        end of the block
        for k = m to 2 * m exclusive
            if cur[k] = 1
                curAns[k % m] = 0
                set counter to 0
            else
                increment counter and set curAns[k % m] = counter
        end of the block
        assign value of 0 to counter
        for a = 2 * m to 3 * m exclusive
            if cur[a] = 1
                stop the loop
            else
                increment counter
        end of the block
        for integer k = 2 * m - 1 to m inclusive counting down
            if cur[k] = 1
                curAns[k % m] = 0
                set value of counter to 0
            else
                increment counter
                curAns[k % m] = min of curAns[k % m] and counter
            end of the block
        end of the block
        for integer j = 0 to m exclusive increase res[j] by curAns[j]
    end of the block
    declare integer variable called min = INT_MAX
    for integer i = 0 to m exclusive
        if min is greater than res[i], set min to res[i]
    end of the block
    print min and a new line
    return to indicate successful execution
end of the block

[CODE]:

int main() {
    int n, m;
    string st;
    cin >> n >> m;
    int res[m];
    for (int i = 0; i < m; i++) { res[i] = 0; }
    for (int i = 0; i < n; i++) {
        cin >> st;
        if (st.find('1') == string::npos) {
            cout << -1 << endl;
            return 0;
        }
        int cur[3 * m];
        int curAns[m];
        for (int j = 0; j < 3 * m; j++) { cur[j] = st[j % m] - '0'; }
        int counter = 0;
        for (int a = m - 1; a >= 0; a--) {
            if (cur[a] == 1)
                break;
            else
                counter++;
        }
        for (int k = m; k < 2 * m; k++) {
            if (cur[k] == 1) {
                curAns[k % m] = 0;
                counter = 0;
            } else
                curAns[k % m] = ++counter;
        }
        counter = 0;
        for (int a = 2 * m; a < 3 * m; a++) {
            if (cur[a] == 1)
                break;
            else
                counter++;
        }
        for (int k = 2 * m - 1; k >= m; k--) {
            if (cur[k] == 1) {
                curAns[k % m] = 0;
                counter = 0;
            } else {
                counter++;
                curAns[k % m] = min(curAns[k % m], counter);
            }
        }
        for (int j = 0; j < m; j++) { res[j] += curAns[j]; }
    }
    int min = INT_MAX;
    for (int i = 0; i < m; i++) {
        if (min > res[i]) min = res[i];
    }
    cout << min << endl;
    return 0;
}