#include #include using namespace std; int main(){ int H, W; cin >> H >> W; assert(1 <= H <= 500); assert(1 <= W <= 500); vector A(H + 2); for(int i = 1; i <= H; i++){ cin >> A[i]; assert(A[i].size() == W); for(auto c : A[i]){ assert(c == '0' || c == '1'); } } A[0] = string(W, '0'); A[H + 1] = string(W, '1'); sort(A.begin(), A.end()); vectordp(H + 2, 1e9); dp[0] = 0; for(int i = 0; i < H + 2; i++){ for(int j = i + 1; j < H + 2; j++){ bool ok = true; for(int k = 0; k < W; k++){ if(A[i][k] == '1' && A[j][k] == '0'){ ok = false; break; } } if(ok){ dp[j] = min(dp[j], dp[i] + (j - i - 1)); } } } cout << dp[H + 1] << endl; }