We are given an array A of N lowercase letter strings, all of the same length.
Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices.
For example, if we have an array A = ["babca","bbazb"] and deletion indices {0, 1, 4}, then the final array after deletions is ["bc","az"].
Suppose we chose a set of deletion indices D such that after deletions, the final array has every element (row) in lexicographic order.
For clarity, A[0] is in lexicographic order (ie. A[0][0] <= A[0][1] <= ... <= A[0][A[0].length - 1]), A[1] is in lexicographic order (ie. A[1][0] <= A[1][1] <= ... <= A[1][A[1].length - 1]), and so on.
Return the minimum possible value of D.length.
 

Example 1:
Input: ["babca","bbazb"]
Output: 3
Explanation: After deleting columns 0, 1, and 4, the final array is A = ["bc", "az"].
Both these rows are individually in lexicographic order (ie. A[0][0] <= A[0][1] and A[1][0] <= A[1][1]).
Note that A[0] > A[1] - the array A isn't necessarily in lexicographic order.


Example 2:
Input: ["edcba"]
Output: 4
Explanation: If we delete less than 4 columns, the only row won't be lexicographically sorted.


Example 3:
Input: ["ghi","def","abc"]
Output: 0
Explanation: All rows are already lexicographically sorted.

 



Note:

1 <= A.length <= 100
1 <= A[i].length <= 100

```python

USER_FILENAME = "<user_code>"
import sys, json, builtins
import math, heapq, bisect, itertools, functools, collections, statistics, fractions, decimal, operator, array, random, re, string, os, pathlib
try:
    import resource as _res
except Exception:
    _res = None
from collections import Counter, defaultdict, deque, OrderedDict
from functools import lru_cache, reduce
from itertools import accumulate, permutations, combinations, product
from math import gcd, ceil, floor, sqrt
from decimal import Decimal
try:
    from typing import *
except Exception:
    pass
try:
    sys.setrecursionlimit(1_000_000)
except Exception:
    pass
try:
    import site as _site
    try:
        _sys_sites = _site.getsitepackages() if hasattr(_site, "getsitepackages") else []
        for _p in _sys_sites:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
    try:
        _usp = _site.getusersitepackages()
        if isinstance(_usp, str):
            _usp = [_usp]
        for _p in _usp:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
except Exception:
    pass

def _disabled_input(*args, **kwargs):
    raise RuntimeError("input() is disabled in callable mode")
builtins.input = _disabled_input

ns = {"__name__": "__judge__"}  # prevent if __name__ == "__main__" blocks
for _k, _v in list(globals().items()):
    if not _k.startswith("_"):
        ns[_k] = _v
_CODE = '#\n# @lc app=leetcode id=960 lang=python3\n#\n# [960] Delete Columns to Make Sorted III\n#\n# https://leetcode.com/problems/delete-columns-to-make-sorted-iii/description/\n#\n# algorithms\n# Hard (53.38%)\n# Likes:    171\n# Dislikes: 5\n# Total Accepted:    4.5K\n# Total Submissions: 8.5K\n# Testcase Example:  \'[\\"babca\\",\\"bbazb\\"]\'\n#\n# We are given an array\xa0A of N lowercase letter strings, all of the same\n# length.\n# \n# Now, we may choose any set of deletion indices, and for each string, we\n# delete all the characters in those indices.\n# \n# For example, if we have an array A = [\\"babca\\",\\"bbazb\\"] and deletion indices\n# {0, 1, 4}, then the final array after deletions is [\\"bc\\",\\"az\\"].\n# \n# Suppose we chose a set of deletion indices D such that after deletions, the\n# final array has every element (row) in\xa0lexicographic order.\n# \n# For clarity, A[0] is in lexicographic order (ie. A[0][0] <= A[0][1] <= ... <=\n# A[0][A[0].length - 1]), A[1] is in lexicographic order (ie. A[1][0] <=\n# A[1][1] <= ... <= A[1][A[1].length - 1]), and so on.\n# \n# Return the minimum possible value of D.length.\n# \n# \n# \n# \n# Example 1:\n# \n# \n# Input: [\\"babca\\",\\"bbazb\\"]\n# Output: 3\n# Explanation: After deleting columns 0, 1, and 4, the final array is A =\n# [\\"bc\\", \\"az\\"].\n# Both these rows are individually in lexicographic order (ie. A[0][0] <=\n# A[0][1] and A[1][0] <= A[1][1]).\n# Note that A[0] > A[1] - the array A isn\'t necessarily in lexicographic\n# order.\n# \n# \n# \n# Example 2:\n# \n# \n# Input: [\\"edcba\\"]\n# Output: 4\n# Explanation: If we delete less than 4 columns, the only row won\'t be\n# lexicographically sorted.\n# \n# \n# \n# Example 3:\n# \n# \n# Input: [\\"ghi\\",\\"def\\",\\"abc\\"]\n# Output: 0\n# Explanation: All rows are already lexicographically sorted.\n# \n# \n# \n# \n# \n# \n# \n# Note:\n# \n# \n# 1 <= A.length <= 100\n# 1 <= A[i].length <= 100\n# \n#\nclass Solution:\n    def minDeletionSize(self, A: List[str]) -> int:\n\n        # Dynamic Programming\n        # Let dp[k] be the number of columns that are kept in answering the question for input [row[k:] for row in A]. The above gives a simple recursion for dp[k].\n        # Time  complexity: O(N x W^2) where N is the length of A, and W is the length of each word in A.\n        # Space complexity: O(W)\n        W = len(A[0])\n        dp = [1] * W\n        for i in range(W - 2, -1, -1):\n            for j in range(i + 1, W):\n                if all(row[i] <= row[j] for row in A):\n                    dp[i] = max(dp[i], 1 + dp[j])\n\n        return W - max(dp)\n\n        \n\n'
_co = compile(_CODE, USER_FILENAME, "exec")
exec(_co, ns, ns)

_target = None
_fn_name = 'minDeletionSize'
if _fn_name:
    if "Solution" in ns:
        try:
            _obj = ns["Solution"]()
            if hasattr(_obj, _fn_name):
                _target = getattr(_obj, _fn_name)
        except Exception:
            _target = None
    if _target is None and _fn_name in ns and callable(ns[_fn_name]):
        _target = ns[_fn_name]
# Fallback: if there's exactly one user-defined function, use it
if _target is None:
    _cands = [v for k, v in ns.items() if callable(v) and getattr(v, "__module__", "") in ("__judge__", "__main__")]
    _user_funcs = [c for c in _cands if getattr(c, "__name__", "").startswith("_") is False]
    if len(_user_funcs) == 1:
        _target = _user_funcs[0]
if _target is None:
    raise RuntimeError(f"Could not resolve callable '{_fn_name}'")

_raw = sys.stdin.read().strip()
if _raw:
    _parsed = json.loads(_raw)
    if isinstance(_parsed, dict):
        _kwargs = _parsed.get("kwargs", {})
        _args = _parsed.get("args", [])
    else:
        _args, _kwargs = _parsed, {}
else:
    _args, _kwargs = [], {}
if not isinstance(_args, (list, tuple)):
    _args = [_args]
_res = _target(*_args, **_kwargs)
try:
    sys.stdout.write(repr(_res) + "\n")
except Exception:
    pass

```

Measured memory_bytes: 6230