[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]:
dist = array of 2 by 10004 int
define the main function
    n, m = int
    read n and m
    for i = 0 to n exclusive
        str = string
        read str
        firstone = -1
        ff = 0
        while ff < m and str[ff] is not '1' increment ff
        if ff is m
            print -1
            return to indicate successful execution
        end of the block
        firstone = ff
        dist[0][ff] = 0
        j = ff+1 modulo m
        while true
            if str[j] is '1'
                dist[0][j] = 0
                k = j
                while dist[0][k] < dist[0][k-1+m modulo m
                    k = k-1+m modulo m
                    dist[0][k] = dist[0][k+1 modulo m]+1
                end of the block
                if j is firstone break
            else
                dist[0][j] = dist[0][j+m-1 modulo m]+1
            end of the block
            j = j+1 modulo m
        end of the block
        for z = 0 to m exclusive
            increment dist[1][z] by dist[0][z]
            dist[0][z] = 0
        end of the block
    end of the block
    minn = dist[1][0]
    for h = 1 to m exclusive
        if dist[1][h] < minn minn = dist[1][h]
    end of the block
    print minn
    return to indicate successful execution
end of the block

[CODE]:

int dist[2][10004];
int main() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        string str;
        cin >> str;
        int firstone = -1;
        int ff = 0;
        while (ff < m && str[ff] != '1') ff++;
        if (ff == m) {
            cout << -1 << endl;
            return 0;
        }
        firstone = ff;
        dist[0][ff] = 0;
        int j = (ff + 1) % m;
        while (true) {
            if (str[j] == '1') {
                dist[0][j] = 0;
                int k = j;
                while (dist[0][k] < dist[0][(k - 1 + m) % m]) {
                    k = (k - 1 + m) % m;
                    dist[0][k] = dist[0][(k + 1) % m] + 1;
                }
                if (j == firstone) break;
            } else {
                dist[0][j] = dist[0][(j + m - 1) % m] + 1;
            }
            j = (j + 1) % m;
        }
        for (int z = 0; z < m; z++) {
            dist[1][z] += dist[0][z];
            dist[0][z] = 0;
        }
    }
    int minn = dist[1][0];
    for (int h = 1; h < m; h++) {
        if (dist[1][h] < minn) { minn = dist[1][h]; }
    }
    cout << minn << endl;
    return 0;
}