[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]:
integer constant INF = 1 << 29
define the main function
    declare integer variables n and m
    read n and m
    a is an array of strings with size n
    read n elements into a
    ans is an array of integers with size m
    set m elements of ans to 0
    chk is integer
    for i = 0 to n exclusive
        set value of chk to 0
        for integer j = 0 to m exclusive, increment chk by 1 if a[i][j] = '1'
        if chk = 0
            print "-1\n"
            return to indicate successful execution
        end of the block
    end of the block
    for i = 0 to n exclusive
        declare integer called last
        declare boolean variable called found = 0
        for j = m - 1 to 0 inclusive counting down
            if a[i][j] = '1'
                set value of last to j
                stop the loop
            end of the block
        end of the block
        , curr is an array of integers with size m
        for j = 0 to m exclusive, curr[j] = INT_MAX
        for j = 0 to m exclusive
            if a[i][j] = '1'
                set value of last to j
                found = 1
            end of the block
            if found is 0
                curr[j] = min(curr[j], (m - last) + j)
            else
                curr[j] = min of curr[j] and abs(j - last)
            end of the block
        end of the block
        found = 0
        for j = 0 to m exclusive
            if a[i][j] = '1'
                last = j
                break the loop
            end of the block
        end of the block
        for integer j = m - 1 to 0 inclusive counting down
            if a[i][j] = '1'
                set value of last to j
                assign value of 1 to found
            end of the block
            if found is 0
                curr[j] = min(curr[j], (last + 1) + (m - 1 - j))
            else
                curr[j] = min(curr[j], abs(j - last))
            end of the block
        end of the block
        for integer j = 0 to m exclusive increment ans[j] by curr[j]
    end of the block
    res is integer = INT_MAX
    for i = 0 to m exclusive, set res to min(res, ans[i])
    print res and a new line
    return to indicate successful execution
end of the block

[CODE]:

const int INF = 1 << 29;
int main() {
    int n, m;
    cin >> n >> m;
    string a[n];
    for (int i = 0; i < (n); i++) cin >> a[i];
    int ans[m];
    for (int i = 0; i < (m); i++) ans[i] = 0;
    int chk;
    for (int i = 0; i < (n); i++) {
        chk = 0;
        for (int j = 0; j < (m); j++) { chk += (a[i][j] == '1'); }
        if (chk == 0) {
            cout << "-1\n";
            return 0;
        }
    }
    for (int i = 0; i < (n); i++) {
        int last;
        bool found = 0;
        for (int j = m - 1; j >= 0; --j) {
            if (a[i][j] == '1') {
                last = j;
                break;
            }
        }
        int curr[m];
        for (int j = 0; j < (m); j++) curr[j] = INT_MAX;
        for (int j = 0; j < (m); j++) {
            if (a[i][j] == '1') {
                last = j;
                found = 1;
            }
            if (!found) {
                curr[j] = min(curr[j], (m - last) + j);
            } else {
                curr[j] = min(curr[j], abs(j - last));
            }
        }
        found = 0;
        for (int j = 0; j < m; ++j) {
            if (a[i][j] == '1') {
                last = j;
                break;
            }
        }
        for (int j = m - 1; j >= 0; --j) {
            if (a[i][j] == '1') {
                last = j;
                found = 1;
            }
            if (!found) {
                curr[j] = min(curr[j], (last + 1) + (m - 1 - j));
            } else {
                curr[j] = min(curr[j], abs(j - last));
            }
        }
        for (int j = 0; j < (m); j++) ans[j] += curr[j];
    }
    int res = INT_MAX;
    for (int i = 0; i < (m); i++) res = min(res, ans[i]);
    cout << res << endl;
    return 0;
}