You are given an *n* × *m* rectangular table consisting of lower case English letters. In one operation you can completely remove one column from the table. The remaining parts are combined forming a new table. For example, after removing the second column from the table

 

we obtain the table:

 

A table is called good if its rows are ordered from top to bottom lexicographically, i.e. each row is lexicographically no larger than the following one. Determine the minimum number of operations of removing a column needed to make a given table good.
The first line contains two integers  — *n* and *m* (1 ≤ *n*, *m* ≤ 100).

Next *n* lines contain *m* small English letters each — the characters of the table.
import sys


def main() -> None:
    read = sys.stdin.readline
    m, n = (int(i) for i in read().split())
    words_matrix = []
    for _ in range(m):
        words_matrix.append(read().strip())

    removed = 0
    # Find first sorted col
    found = -1
    for col in range(n):
        if found > -1:
            break
        valid = True
        for row in range(1, m):
            # If current letter smaller than the one above, then the current col is not sorted
            if words_matrix[row][col] < words_matrix[row - 1][col]:
                removed += 1
                valid = False
                break
        if valid:
            found = col
            break
    if found < 0:
        print(removed)
    else:
        curr_valid_col = found
        for col in range(curr_valid_col, n - 1):
            is_valid_col = True
            for row in range(m -1):
                # Check every 2 rows
                r1c1, r1c2 =  words_matrix[row][curr_valid_col],  words_matrix[row][col + 1]
                r2c1, r2c2 =  words_matrix[row + 1][curr_valid_col],  words_matrix[row + 1][col + 1]
                if r1c1 == r2c1:
                    if r1c2 > r2c2:
                        removed += 1
                        is_valid_col = False
                        break
                elif r1c1 > r2c1:
                    removed += 1
                    is_valid_col = False
                    break
            if is_valid_col:
                curr_valid_col = col
        print(removed)







if __name__ == '__main__':
    main()